import 'package:fis_i18n/i18n.dart';
import 'package:fis_measure/utils/prompt_box.dart';
import 'package:flyinsono/architecture/router/router.dart';
import 'package:flyinsono/managers/interfaces/appointment_consultation.dart';
import 'package:flyinsono/views/desktop/real_time_consultation/index.dart';
import 'package:flyinsono/views/desktop/real_time_consultation/state.dart';
import 'package:flyinsono/views/desktop/real_time_consultation/widgets/add_consultation_evaluation.dart';
import 'package:get/get.dart';

class RealTimeConsultationEvaluationController {
  late final RealTimeConsultationController controller;
  late final RealTimeConsultationState state;
  final appointmentConsultationManager = Get.find<IAppointmentConsultation>();
  RealTimeConsultationEvaluationController(this.controller) {
    state = controller.state;
  }

  ///添加会诊评价
  Future<void> addEvaluation() async {
    if (state.score == 0) {
      PromptBox.toast(
          i18nBook.realTimeConsultation.requiredItemsNotCompleted.t);
    } else {
      await appointmentConsultationManager.addConsultationEvaluateAsync(
        state.currentConsultationCode,
        state.score,
        state.consultationEvaluation,
      );
    }
  }

  ///获取会诊评价
  Future<void> getEvaluationDetatil() async {
    await getEvaluationInfo();
    if (state.consultationEvaluateDetail != null) {
      router.dialog(() => AddConsultationEvaluationView(
            isOnlyRead: true,
          ));
    } else {
      router.dialog(() => AddConsultationEvaluationView());
    }
  }

  ///获取会诊评价详情
  Future<void> getEvaluationInfo() async {
    state.consultationEvaluateDetail = await appointmentConsultationManager
        .getEvaluationInfo(state.currentConsultationCode);
  }

  /// 打开指定 Code 的会诊评价
  Future<void> openEvaluateDialogByCode(String consultationCode) async {
    state.consultationEvaluateDetail = await appointmentConsultationManager
        .getEvaluationInfo(consultationCode);
    if (state.consultationEvaluateDetail != null) {
      router.dialog(() => AddConsultationEvaluationView(
            isOnlyRead: true,
          ));
    } else {
      router.dialog(() => AddConsultationEvaluationView());
    }
  }

  ///设置评价等级描述
  void setGradeDescription(bool isOnlyRead) {
    if (isOnlyRead) {
      double score = state.consultationEvaluateDetail?.evaluateScore ?? 0;
      if (score <= state.bad) {
        state.gradeDescription = i18nBook.realTimeConsultation.negative.t;
      } else if (score > state.bad && score <= state.good) {
        state.gradeDescription = i18nBook.realTimeConsultation.medium.t;
      } else if (score > state.good) {
        state.gradeDescription = i18nBook.realTimeConsultation.good.t;
      }
    }
  }

  ///设置会诊评分
  void setScore(double score) {
    state.score = score;
    if (score <= state.bad) {
      state.gradeDescription = i18nBook.realTimeConsultation.negative.t;
    } else if (score > state.bad && score <= state.good) {
      state.gradeDescription = i18nBook.realTimeConsultation.medium.t;
    } else if (score > state.good) {
      state.gradeDescription = i18nBook.realTimeConsultation.good.t;
    }
  }
}
