import 'package:electronic_assistant/base/base_controller.dart';
import 'package:electronic_assistant/data/bean/template_bean.dart';
import 'package:electronic_assistant/module/template/template_edit/view.dart';
import 'package:get/get.dart';
import 'package:get/get_core/src/get_main.dart';

class TemplateDetailController extends BaseController {
  final Rxn<TemplateBean> _templateBean = Rxn<TemplateBean>();

  final RxList<String> templateTitle = RxList();

  TemplateBean? get templateBean => _templateBean.value;

  dynamic result;

  @override
  void onInit() {
    super.onInit();
    _getArguments();
  }

  void _getArguments() {
    var arguments = Get.arguments;
    if (arguments is TemplateBean) {
      _templateBean.value = arguments;
      _combinationTemplateList(arguments);
    }
  }

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

  void onUpdateTemplate() async {
    final templateBean = _templateBean.value;
    if (templateBean == null || templateBean.isDefaultTemp) {
      return;
    }
    dynamic isSuccess = await TemplateEditPage.editDetail(templateBean);
    if (isSuccess == true) {
      _templateBean.refresh();
      if (_templateBean.value != null) {
        _combinationTemplateList(_templateBean.value!);
      }
    }
    result = isSuccess;
  }

  void _combinationTemplateList(TemplateBean bean) {
    templateTitle.clear();
    if (bean.titles != null) {
      templateTitle.addAll(bean.titles!);
    }
    if (bean.defaultTitle?.isNotEmpty == true) {
      templateTitle.add(bean.defaultTitle!);
    }
  }
}
