import 'package:flyinsono/architecture/router/index.dart';
import 'package:flyinsono/architecture/states/states.dart';
import 'package:flyinsono/architecture/utils/web_parameter_helpler.dart';
import 'package:flyinsono/js_platform/methods.dart';
import 'package:flyinsono/managers/interfaces/user_info.dart';
import 'package:flyinsono/views/desktop/device_live/device_live_detail/controller.dart';
import 'package:get/get.dart';

import 'base.dart';

class OpenDeviceLiveHandler extends MethodHandlerBase {
  OpenDeviceLiveHandler() : super(TargetMethodName.OpenDeviceLivePage);
  final manager = Get.find<IUserInfoManager>();
  @override
  Future<void> execute(List<String> args) async {
    String token = args[0];
    String code = args[1];
    int maxLiveCount = int.tryParse(args[2]) ?? 1;
    Store.user.setToken(token);
    Store.user.deviceLiveMaxCount = maxLiveCount;
    await manager.getUserInfoAsync(); // 检查登录状态
    ParameterHelper.parasmeters = {};
    if (router.currentName?.startsWith(RouteNames.Live.DeviceDetail) == true) {
      final controller = Get.find<DeviceLiveDetailDesktopController>();
      final index = controller.findIndexByCode(code);
      controller.handleDeviceChecked(true, index);
    } else {
      router.to(
        RouteNames.Live.DeviceDetail,
        parameters: {
          "code": code,
        },
      );
    }
  }
}
