import 'dart:convert';
import 'package:fis_i18n/i18n.dart';
import 'package:fis_measure/process/workspace/measure_data_controller.dart';
import 'package:flyinsono/architecture/router/index.dart';
import 'package:flyinsono/architecture/services/modules/config_language.dart';
import 'package:flyinsono/architecture/states/states.dart';
import 'package:flyinsono/architecture/utils/web_parameter_helpler.dart';
import 'package:flyinsono/global.dart';
import 'package:flyinsono/js_platform/handlers/base.dart';
import 'package:flyinsono/js_platform/listener.dart';
import 'package:flyinsono/js_platform/methods.dart';
import 'package:fis_vid/data_channel/channel.dart';
import 'package:get/get.dart';
import 'package:fis_ui/index.dart';

class OpenMeasurePageHandler extends MethodHandlerBase {
  OpenMeasurePageHandler() : super(TargetMethodName.OpenMeasurePage);
  bool _waitToConfirm = false;
  @override
  Future<void> execute(List<String> args) async {
    Map<String, String> parameters = {};
    parameters['token'] = args[0];
    parameters['patientCode'] = args[1];
    parameters['remedicalCode'] = args[2];
    parameters['recordCode'] = args[3];
    parameters['source'] = args[4];
    ParameterHelper.parasmeters = parameters;
    await Global.initLanguageByPlatform();
    var token = parameters['token'] ?? '';
    if (token.isNotEmpty) {
      Store.user.setToken(token);
    }
    final languageService = Get.find<LanguageService>();

    /// 初始化语言包
    languageService.init();

    ///当前路由，减去后缀
    final curRouterAddress = router.currentName!.split("?").first;

    if (curRouterAddress == RouteNames.Remedical.Measure) {
      final measureData = Get.find<MeasureDataController>();
      bool isNeedConfirm =
          measureData.measureInfoData.recordCode != parameters['recordCode'];
      if (isNeedConfirm) {
        //_waitToConfirm是用来去重多次弹窗的
        if (_waitToConfirm) {
          router.back();
        }
        _waitToConfirm = true;
        var container = JsListener();
        FConfirmAlert.show(
          context: Get.context!,
          title: i18nBook.common.tip.t,
          subTitle: i18nBook.common.leaveCurrentPage.t,
          businessParent: container,
          confrimLabel: i18nBook.common.confirm.t,
          cancelLabel: i18nBook.common.cancel.t,
          onConfirm: () {
            /// 事件通知，更新测量窗口状态
            measureData.curMeasureDataChanged.emit(this, parameters);
            _waitToConfirm = false;
          },
          onCancel: () {
            router.back();
            _waitToConfirm = false;
          },
        );
      } else {
        /// 事件通知，更新测量窗口状态
        measureData.curMeasureDataChanged.emit(this, parameters);
      }
    } else {
      router.offAll(RouteNames.Remedical.Measure, parameters: parameters);
    }
  }
}

class VidChunkReceiveHandler extends MethodHandlerBase {
  VidChunkReceiveHandler() : super(TargetMethodName.VidChunkReceive);

  @override
  void execute(List<String> args) {
    final id = args[0];
    final chunkBase64 = args[1];
    final chunk = base64Decode(chunkBase64);
    VidDataChannel.receiveChunk(id, chunk);
  }
}
