import 'package:electronic_assistant/base/base_controller.dart';
import 'package:electronic_assistant/data/bean/template_bean.dart';
import 'package:electronic_assistant/data/repositories/template_repository.dart';
import 'package:electronic_assistant/resource/string.gen.dart';
import 'package:electronic_assistant/utils/error_handler.dart';
import 'package:electronic_assistant/utils/toast_util.dart';
import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:get/get_core/src/get_main.dart';

import '../../../dialog/rename_dialog.dart';

class TemplateEditController extends BaseController {
  int? templateId;

  final titleController = TextEditingController();
  final focusNode = FocusNode();

  TemplateBean? targetTemplateBean;

  final Rxn<String> _defaultTemplate = Rxn();

  String? get defaultTemplate => _defaultTemplate.value;

  final RxList<String> templateCustomTitle = RxList();

  @override
  void onInit() {
    super.onInit();
    var arguments = Get.arguments;
    if (arguments is TemplateBean) {
      targetTemplateBean = arguments;
      templateId = arguments.id;
      titleController.text = arguments.name ?? '';
      templateCustomTitle.assignAll(arguments.titles ?? []);
      _defaultTemplate.value = arguments.defaultTitle;
    }
    if (templateId == null) {
      _getDefaultTitle();
    }
  }

  void _getDefaultTitle() {
    templateRepository.defaultInfo().then((data) {
      _defaultTemplate.value = data;
    }).catchError((error) {
      ErrorHandler.toastError(error);
    });
  }

  @override
  void onReady() {
    super.onReady();
  }

  void onBack() {
    Get.back();
  }

  void onCancel() {
    Get.back();
  }

  void onSaveTemplate() {
    if (titleController.text.isEmpty) {
      ToastUtil.showToast(StringName.templateEditNameHint.tr);
      return;
    }
    if (templateCustomTitle.isEmpty) {
      ToastUtil.showToast(StringName.templateTitleNotFail.tr);
      return;
    }
    templateRepository
        .templateUpdate(templateId, titleController.text, templateCustomTitle)
        .then((data) async {
      if (templateId == null) {
        ToastUtil.showToast(StringName.templateAddSuccess.tr);
      } else {
        targetTemplateBean?.name = titleController.text;
        targetTemplateBean?.titles = templateCustomTitle;
        ToastUtil.showToast(StringName.templateUpdateSuccess.tr);
      }
      await Future.delayed(const Duration(milliseconds: 400));
      Get.back(result: true);
    }).catchError((error) {
      ErrorHandler.toastError(error);
    });
  }

  void clearTextFieldSelection() {
    focusNode.unfocus();
  }

  void onAddTemplate() {
    clearTextFieldSelection();
    reNameDialog(StringName.templateDialogAddTitle.tr, null,
        hintTxt: StringName.templateDialogAddTitleHint.tr,
        maxLength: 30, returnBuilder: (newValue) {
      templateCustomTitle.add(newValue);
    });
  }

  void onDeleteTemplate(int index) {
    templateCustomTitle.removeAt(index);
  }

  void onUpdateTemplate(int index, String txt) {
    reNameDialog(StringName.templateDialogUpdateTitle.tr, txt,
        hintTxt: StringName.templateDialogAddTitleHint.tr,
        maxLength: 30, returnBuilder: (newValue) {
      templateCustomTitle[index] = newValue;
    });
  }
}
