import 'package:fis_jsonrpc/rpc.dart';
import 'package:get/get.dart';
import 'package:vnoteapp/architecture/defines.dart';
import 'package:vnoteapp/managers/interfaces/patient.dart';
import 'package:vnoteapp/pages/controllers/crowd_labels.dart';
import 'package:vnoteapp/pages/controllers/home_nav_mixin.dart';
import 'package:vnoteapp/pages/home/controller.dart';
import 'package:vnoteapp/pages/patient/create/state.dart';
import 'package:vnoteapp/pages/patient/list/controller.dart';

class CreatePatientController extends FControllerBase with HomeNavMixin {
  final _patientManager = Get.find<IPatientManager>();

  final state = CreatePatientState();

  @override
  Future<void> onLoad() {
    final params = Get.parameters;
    if (params.containsKey("from")) {
      if (params["from"] == "list") {
        state.isCreateOnly = true;
      }
    }
    return super.onLoad();
  }

  @override
  void onReady() {
    _loadVillageOptions();
    _loadCrowdLabelOptions();

    super.onReady();
  }

  /// 保存档案，调整签约
  void gotoSignContract() async {
    setBusy("正在保存...");
    final code = await _submitForm();
    if (code == null) {
      busy = false;
      return;
    }

    Get.find<HomeController>().switchNavByName("/patient/list");
    Future.delayed(
      const Duration(milliseconds: 800),
      () {
        // TODO:
        Get.find<PatientListController>().gotoDetail(code);
        busy = false;
      },
    );
  }

  /// 存为档案，调整到档案详情
  void gotoPatientDetail() async {
    setBusy("正在保存...");
    final code = await _submitForm();
    if (code == null) {
      busy = false;
      return;
    }

    Get.find<HomeController>().switchNavByName("/patient/list");
    Future.delayed(
      const Duration(milliseconds: 800),
      () {
        Get.find<PatientListController>().gotoDetail(code);
        busy = false;
      },
    );
  }

  /// 保存并返回
  void saveAndBack() async {
    setBusy("正在保存...");
    final code = await _submitForm();
    busy = false;

    if (code == null) {
      busy = false;
      return;
    }

    Get.back(result: code, id: 1001);
  }

  /// 点击读卡事件
  void onReadCardClicked() {
    Get.showSnackbar(
      const GetSnackBar(
        message: "尚未接入",
        snackPosition: SnackPosition.TOP,
        duration: Duration(seconds: 1),
      ),
    );
  }

  /// 处理 “同户籍地址” 勾选变更事件
  void onSyncAddressCheckChanged(bool isChecked) {
    state.isSyncAddresses = isChecked;
    if (isChecked) {
      // 同步户籍地址到现住地址
      state.address = state.censusRegister;
    } else {
      state.address = "";
    }
  }

  /// 处理户籍地址变更
  void onCensusRegisterChanged(String value) {
    state.censusRegister = value;
    if (state.isSyncAddresses) {
      state.address = value;
    }
  }

  Future<String?> _submitForm() async {
    final crowdLabelCodes =
        Get.find<CrowdLabelsController>().state.selectedCodes;
    final request = CreatePatientRequest(
      patientName: state.name,
      phone: state.phoneNo,
      patientGender: state.gender,
      nationality: state.nation,
      birthday: state.birthday.toUtc(),
      cardType: state.cardType,
      cardNo: state.cardNo,
      patientAddress: state.address,
      permanentResidenceAddress: state.censusRegister,
      crowdLabels: crowdLabelCodes,
    );
    final result = await _patientManager.create(request);
    return result;
  }

  /// 加载人群分类选项集合
  Future<void> _loadCrowdLabelOptions() async {}

  /// 加载村/居委会选项集合
  Future<void> _loadVillageOptions() async {}
}
