import 'package:fis_common/logger/logger.dart';
import 'package:flyinsono/architecture/router/router.dart';
import 'package:flyinsono/js_platform/methods.dart';
import 'package:flyinsono/lab/architecture/task_center.dart';
import 'package:flyinsono/managers/interfaces/entities/process_arg.dart';
import 'package:flyinsono/managers/interfaces/live_course.dart';
import 'package:flyinsono/managers/interfaces/remedical.dart';
import 'package:flyinsono/pages/upgrade/upgrade_widgets/upgrade_center.dart';
import 'package:flyinsono/views/common/device_live/device_live_list/controller.dart';
import 'package:flyinsono/views/desktop/real_time_consultation/controller/real_time_consultation_controller.dart';
import 'package:flyinsono/views/desktop/user_setting/controller.dart';
import 'package:get/get.dart';

import '../../architecture/router/desktop/names.dart';
import 'base.dart';

class UpdateExportProgessHandler extends MethodHandlerBase {
  UpdateExportProgessHandler() : super(TargetMethodName.UpdateExportProgess);

  @override
  void execute(List<String> args) {
    try {
      if (args.length >= 3) {
        print(
            "UpdateExportProgessHandler 有进度更新：${args[0]} ${args[1]} ${args[2]}");
        localTaskCenter.processChange.emit(
          this,
          ExportProcessArg(double.parse(args[0]), int.parse(args[1]), args[2]),
        );
        return;
      }
      if (UpgradeCenter.isChecking) {
        UpgradeCenter.emitProgressChanged(args[0]);
        return;
      }
      Get.find<IRemedicalManager>()
          .processChange
          .emit(this, ProcessArg(args[0], args[1]));
      final currentRouter = router.getCurrentNameByNavId(NavIds.HOME);
      if (currentRouter == RouteNames.RealTimeRemedical.RealTimeConsultation) {
        final realTimeConsultationListController =
            Get.find<RealTimeConsultationController>();
        realTimeConsultationListController.listController
            .handleProgressChanged(args[0], args[1]);
      } else if (currentRouter == RouteNames.User.Setting) {
        final userSettingController = Get.find<UserSettingController>();
        userSettingController.handleProgressChanged(args[0], args[1]);
      } else if (currentRouter == RouteNames.Live.DeviceList) {
        final deviceLiveListController = Get.find<DeviceLiveListController>();
        // 50 + int.parse((progressNum * 0.5).toStringAsFixed(2) * 100);
        deviceLiveListController.handleProgressChanged(args[0], args[1]);
        // 延迟10秒后触发方法 如果壳子下载在15秒钟后还是0 则触发方法 并且 20秒后进行到100%
        Future.delayed(Duration(seconds: 15), () {
          double progressNum = double.parse(args[0]);
          if (progressNum == 0) {
            deviceLiveListController.state.deviceProgress = 75;
            Future.delayed(Duration(seconds: 5), () {
              deviceLiveListController.state.deviceProgress = 100;
            });
          }
        });
      } else if (currentRouter == RouteNames.College.FlyinsonoCollege) {
        Get.find<ILiveCourseManager>().onProcessChange.emit(
              this,
              (double.parse(args[0])).toPrecision(2),
            );
      }
      print('remedicalController.handleProgressChanged ${args[0]} ${args[1]}');
    } catch (e) {
      logger.e('UpdateExportProgessHandler execute ex:', e);
    }
  }
}
