import 'package:flyinsono/architecture/utils/prompt_box.dart';
import 'package:flyinsono/lab/manager/interfaces/project.dart';
import 'package:flyinsono/lab/manager/page_manager.dart';
import 'package:flyinsono/lab/mixin/tab_hook_mixin.dart';
import 'package:flyinsono/lab/mock_data/mock_rpc.dart';
import 'package:flyinsono/lab/router/lab_route_names.dart';
import 'package:get/get.dart';

import 'index.dart';

class LabCreateProjectController extends GetxController with TabHookMixin {
  LabCreateProjectController({
    required this.tabHashCode,
    this.projectModel,
  });

  final state = LabCreateProjectState();

  final ProjectModel? projectModel;

  @override
  final int tabHashCode;

  /// 项目
  final project = Get.find<IProjectManager>();

  // tap
  void handleTap(int index) {
    Get.snackbar(
      "标题",
      "消息",
    );
  }

  Future<void> createProject() async {
    if (state.projectName.isEmpty) {
      PromptBox.toast("项目名称不能为空");
      return;
    }
    if (state.projectName.isNotEmpty) {
      await project.createResearchProjectAsync(
        projectName: state.projectName,
        projectType: state.projectType,
        projectIntroduction: state.projectIntroduction,
      );

      PageManager.ins.openTab(
        LabRouteNames.Project,
        args: 0,
      );
      Get.back();
      // PageManager.ins.closeTab(tabHashCode);
    }
  }

  Future<void> updateProject() async {
    if (state.projectName.isEmpty) {
      PromptBox.toast("项目名称不能为空");
      return;
    }
    if (state.projectName.isNotEmpty && projectModel != null) {
      await project.updateResearchProjectAsync(
        projectCode: projectModel!.id,
        projectName: state.projectName,
        projectType: state.projectType,
        projectIntroduction: state.projectIntroduction,
      );

      PageManager.ins.openTab(
        LabRouteNames.Project,
        args: 0,
      );
      Get.back();
      // PageManager.ins.closeTab(tabHashCode);
    }
  }

  void initEditData() {
    if (projectModel != null) {
      state.projectName = projectModel!.name;
      state.projectType = projectModel!.organizationPatientType;
      state.projectIntroduction = projectModel!.projectIntroduction;
    }
  }

  /// 在 widget 内存中分配后立即调用。
  @override
  void onInit() {
    super.onInit();
    initEditData();
  }

  /// 在 onInit() 之后调用 1 帧。这是进入的理想场所
  @override
  void onReady() {
    super.onReady();
  }

  /// 在 [onDelete] 方法之前调用。
  @override
  void onClose() {
    super.onClose();
  }

  /// dispose 释放内存
  @override
  void dispose() {
    super.dispose();
  }

  @override
  void onActive(arguments) {
    // TODO: implement onActive
  }

  @override
  void onSleep() {
    // TODO: implement onSleep
  }
}
