import 'package:fis_common/logger/logger.dart';
import 'package:fis_i18n/i18n.dart';
import 'package:fis_ui/index.dart';
import 'package:flyinsono/architecture/define.dart';
import 'package:flyinsono/architecture/router/index.dart';
import 'package:flyinsono/architecture/services/entity/local_setting.dart';
import 'package:flyinsono/architecture/states/states.dart';
import 'package:flyinsono/js_platform/methods.dart';
import 'package:flyinsono/managers/interfaces/remote_controller.dart';
import 'package:flyinsono/pages/live_course/index.dart';
import 'package:flyinsono/pages/main/home/controller.dart';
import 'package:flyinsono/pages/remote_controller/controller.dart';
import 'package:flyinsono/views/desktop/real_time_consultation/live_consultation/controller.dart';
import 'package:get/get.dart';
import 'package:fis_jsonrpc/rpc.dart';
import 'base.dart';

class CloseLivePageConfirmHandler extends MethodHandlerBase {
  CloseLivePageConfirmHandler() : super(TargetMethodName.CloseLivePageConfirm);
  @override
  void execute(List<String> args) {
    try {
      final curRouterAddress = router.currentName!.split("?").first;
      if (curRouterAddress == RouteNames.College.LiveCourseDetail) {
        final controller = Get.find<LiveCourseController>();
        if (controller.isExit) {
          return;
        }
        controller.isExit = true;
      }
      FConfirmAlert.show(
          context: Get.context!,
          title: i18nBook.common.tip.t,
          subTitle: i18nBook.common.leaveCurrentPage.t,
          confrimLabel: i18nBook.common.confirm.t,
          cancelLabel: i18nBook.common.cancel.t,
          businessParent:
              FVirtualPageWidget('CloseLiveConsultationConfirmHandler'),
          onConfirm: () {
            print('CloseLivePageConfirmHandler:$curRouterAddress');
            if (curRouterAddress ==
                RouteNames.RealTimeRemedical.LiveConsultation) {
              final controller = Get.find<LiveConsultationController>();
              controller.sendInteractiveBoardData('', true);
              controller.onClearCanavsHandler.emit(this, Store.user.code);
              controller.leaveConsultation();

              try {
                if (!(LocalSetting.ins.isUseSecondWindow ?? false)) {
                  final _navController = Get.find<HomeNavigatorController>();
                  _navController.subBack();
                }
              } catch (ex) {
                logger.e('_navController subBack ex:', ex);
              }
            } else if (curRouterAddress == RouteNames.Live.RemoteController) {
              ///如果走到这个判断，说明是会诊中调参页面要关闭，且为第二窗口
              final controller = Get.find<RemoteControllerController>();
              final remoteControllerManager =
                  Get.find<IRemoteControllerManager>();

              ///退出会诊调参
              remoteControllerManager.applyRemoteConnectionAsync(
                deviceCode: controller.state.deviceCode,
                controlType: ControlDeviceParameterEnum.End,
                roomCode: controller.state.consultationCode,
              );
              router.back();
            } else if (curRouterAddress ==
                RouteNames.College.LiveCourseDetail) {
              final controller = Get.find<LiveCourseController>();
              controller.exitLiveCourse(true);
              controller.isExit = false;
            } else {
              print(RouteNames.College.LiveCourseDetail);
            }
          },
          onCancel: () {
            if (curRouterAddress == RouteNames.College.LiveCourseDetail) {
              final controller = Get.find<LiveCourseController>();
              controller.isExit = false;
            }
          });
    } catch (e) {
      logger.e('CloseLiveConsultationConfirmHandler execute ex:', e);
    }
  }
}
