import 'package:flutter/cupertino.dart';
import 'package:injectable/injectable.dart';
import 'package:keyboard/base/base_controller.dart';
import 'package:keyboard/resource/string.gen.dart';
import 'package:keyboard/utils/toast_util.dart';
import 'intimacy_generate_character_edit_view.dart';

/// 生成亲密度人设（生成键盘）的编辑弹窗的Controller
@injectable
class IntimacyGenerateCharacterEditController extends BaseController {
  /// TextField操作控制器
  final TextEditingController editingController = TextEditingController();

  /// 输入框焦点
  final FocusNode inputFocusNode = FocusNode();

  IntimacyGenerateCharacterEditController();

  @override
  void onClose() {
    inputFocusNode.dispose();
    editingController.dispose();
    super.onClose();
  }

  /// 保存
  void doOnSave(OnSaveCallback onSaveCallback) {
    // 必须输入内容
    String userInput = editingController.text.trim();

    if (userInput.isEmpty) {
      ToastUtil.show(StringName.intimacyGenerateCharacterInputTip);
      return;
    }

    onSaveCallback(userInput);

    // 清空输入框
    editingController.text = "";
  }

  /// 关闭
  void doClose(OnCloseCallback onCloseCallback) {
    // 清空输入框
    editingController.text = "";
    onCloseCallback();
  }

  /// 让输入框获取焦点
  void doRequestInputFocus() {
    Future.delayed(const Duration(milliseconds: 300), () {
      inputFocusNode.requestFocus();
    });
  }

  /// 让输入框失去焦点
  void doInputUnFocus() {
    inputFocusNode.unfocus();
  }
}
