import 'package:electronic_assistant/base/base_controller.dart';
import 'package:electronic_assistant/data/bean/template_bean.dart';
import 'package:electronic_assistant/module/template/template_detail/view.dart';
import 'package:electronic_assistant/module/template/template_edit/view.dart';
import 'package:electronic_assistant/resource/colors.gen.dart';
import 'package:electronic_assistant/resource/string.gen.dart';
import 'package:electronic_assistant/utils/error_handler.dart';
import 'package:electronic_assistant/utils/expand.dart';
import 'package:electronic_assistant/widget/alert_dialog.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:get/get_core/src/get_main.dart';

import '../../../data/repositories/template_repository.dart';

class TemplateListController extends BaseController {
  final RxList<TemplateBean> templateList = RxList();

  int? maxTemplateCount;

  bool? result;

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

  void refreshTemplateList() {
    templateRepository.getTemplateList().then((data) {
      templateList.clear();
      var defaultTemp = data.defaultTemplate;
      if (defaultTemp != null) {
        defaultTemp.setDefaultTemp(true);
        templateList.add(defaultTemp);
      }
      var customTemp = data.templateList;
      if (customTemp != null) {
        templateList.addAll(customTemp);
      }
      maxTemplateCount = data.maxTemplateCount;
    }).catchError((error) {
      debugPrint('error: $error');
      ErrorHandler.toastError(error);
    });
  }

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

  void onAddTemplate() async {
    dynamic isSuccess = await TemplateEditPage.addStart();
    if (isSuccess is bool && isSuccess) {
      result = true;
      refreshTemplateList();
    }
  }

  void onDetailClick(TemplateBean bean) async {
    dynamic isSuccess = await TemplateDetailPage.start(bean);
    if (isSuccess == true) {
      result = true;
      refreshTemplateList();
    }
  }

  void templateEdit(TemplateBean item) async {
    dynamic isSuccess = await TemplateEditPage.editDetail(item);
    if (isSuccess == true) {
      result = true;
      templateList.refresh();
    }
  }

  void onDeleteTemplate(TemplateBean item) {
    EAAlertDialog.show(
        contentWidget: Padding(
          padding: EdgeInsets.only(top: 8.w),
          child: Text(
            StringName.templateDialogDeleteTitle.tr
                .replacePlaceholders([item.name]),
            style: TextStyle(
                fontSize: 15.sp,
                color: ColorName.primaryTextColor,
                fontWeight: FontWeight.bold),
          ),
        ),
        cancelText: StringName.cancel.tr,
        confirmText: StringName.sure.tr,
        confirmOnTap: () {
          _deleteTemplate(item);
        });
  }

  void _deleteTemplate(TemplateBean item) {
    if (item.id == null) {
      return;
    }
    templateRepository.templateDelete(item.id!).then((value) {
      result = true;
      refreshTemplateList();
    }).catchError((error) {
      ErrorHandler.toastError(error);
    });
  }
}
