import 'dart:convert';

import 'package:fis_common/logger/logger.dart';
import 'package:fis_i18n/i18n.dart';
import 'package:fis_jsonrpc/rpc.dart';
import 'package:fis_lib_report/report_info/report_info.dart';
import 'package:flyinsono/architecture/services/entity/default_entry_setting.dart';
import 'package:flyinsono/architecture/services/modules/default_entry_configuration.dart';
import 'package:flyinsono/architecture/states/states.dart';
import 'package:flyinsono/architecture/utils/prompt_box.dart';
import 'package:flyinsono/managers/interfaces/entry.dart';
import 'package:flyinsono/pages/remedical/report/report_edit/controller.dart';
import 'package:flyinsono/pages/remedical/report/report_edit/state.dart';
import 'package:flyinsono/rpc.dart';
import 'package:get/get.dart';

class ThesaurusController {
  late final ReportEditController controller;
  late final ReportEditState state;
  final entryManager = Get.find<IEntryManager>();
  ThesaurusController(ReportEditController controller) {
    this.controller = controller;
    this.state = controller.state;
    initAllThesaurus();
  }
  DefaultEntrySetting defaultEntrySetting = DefaultEntrySetting.ins;
  final DefaultEntryService defaultEntryService =
      Get.find<DefaultEntryService>();

  /// 初始化词条数据
  Future<void> getDefaultThesaurus(String thesaurusCode) async {
    final allThesaurusItems =
        await entryManager.getThesaurusItems(thesaurusCode: thesaurusCode);
    if (allThesaurusItems.isEmpty) {
      return;
    } else {
      controller.state.allThesaurusList = allThesaurusItems;
    }
  }

  ///初始化默认词条
  Future<void> initDefaultThesaurus() async {
    ThesaurusAllDTO? defaultThesaurus;
    try {
      defaultThesaurus = await rpc.report.getDefaultThesaurusContentAsync(
        GetDefaultThesaurusContentRequest(
          token: Store.user.token,
          languageCode: i18nBook.locale.toCodeString('-'),
        ),
      );
      var thesaurusCode = defaultThesaurus.baseInfo!.thesaurusCode ?? '';
      if (thesaurusCode.isNotEmpty) {
        state.selectedEntryCode = thesaurusCode;
        await getDefaultThesaurus(thesaurusCode);
      }
    } catch (e) {
      print(e);
      logger.e('ReportEditController _initData ex:', e);
    }

    //对选择词条监听
    FReportInfo.instance.onDiagnosticTap.dispose();
    FReportInfo.instance.onDiagnosticTap.addListener((sender, e) {
      if (defaultThesaurus == null) {
        PromptBox.toast(i18nBook.remedical.noEntrys.t);
      } else {
        state.isEntryDisplayed = true;
      }
    });
  }

  Future<void> initAllThesaurus() async {
    if (Store.user.token.isEmpty) {
      return;
    }
    var allEntrys = await entryManager.getThesaurusPage();
    var userDefaultEnty =
        allEntrys.firstWhereOrNull((element) => element.isUserDefault);
    if (userDefaultEnty != null) {
      state.entryInfos = [userDefaultEnty];
      var normalEntrys = allEntrys.where((element) => !element.isUserDefault);
      state.entryInfos.addAll(normalEntrys);
    } else {
      state.entryInfos = allEntrys;
    }
  }

  ///切换词条
  void changeEntry() {
    getDefaultThesaurus(state.selectedEntryCode);
  }
}
