import 'package:fis_common/env/env.dart';
import 'package:fis_jsonrpc/services/index.dart';
import 'package:flyinsono/architecture/define.dart';
import 'package:flyinsono/views/common/device_live/device_live_detail/state.dart';
import 'package:get/get.dart';

class DeviceLiveDetailDesktopState extends DeviceLiveDetailState {
  final _isListPanelCollapsed = FRxBool(false);
  final _isWinRecordStarted = FRxBool(false);

  /// 直播索引列表
  final liveIndexList = FRxList<int>();

  /// 初始选择设备编码
  String? initialCode;

  /// 最大同时直播数量
  int maxLiveCount = 1;

  /// 是否可录制窗口
  bool canRecordWindow = FPlatform.isWindows || FPlatform.isMacOS;

  /// 是否可以展示更多直播
  bool get canShowMoreLive => liveIndexList.length < maxLiveCount;

  /// 列表面板是否折叠
  bool get isListPanelCollapsed => _isListPanelCollapsed.value;

  set isListPanelCollapsed(bool val) => _isListPanelCollapsed.updateValue(val);

  /// 窗口录制是否已开启
  bool get isWinRecordStarted => _isWinRecordStarted.value;
  set isWinRecordStarted(bool val) => _isWinRecordStarted.updateValue(val);

  @override
  void updateDeviceList(List<DeviceExtendInfoDTO> dtoList) {
    final newModelList = <DeviceStateModel>[];
    for (var dto in dtoList) {
      final model = DeviceStateModel(dto);
      final old = deviceList
          .firstWhereOrNull((e) => e.dto.deviceCode == dto.deviceCode);
      if (old != null) {
        // 恢复状态
        model.isChecked = old.isChecked;
        model.liveInfo = old.liveInfo;
      }
      newModelList.add(model);
    }
    deviceList = newModelList;
  }
}
