import 'package:get/get.dart';
import '../../../base/base_controller.dart';
import '../../../controllers/core_controller.dart';
import '../../../data/models/launch/groups.dart';

/// 主页控制器
class HomeController extends BaseController {
  final coreController = Get.find<CoreController>();
  final TAG = 'HomeController';

  final _isOn = false.obs;
  bool get isOn => _isOn.value;
  set isOn(bool value) => _isOn.value = value;

  final _currentIndex = 0.obs;
  int get currentIndex => _currentIndex.value;
  set currentIndex(int value) => _currentIndex.value = value;

  final _lastIndex = 0.obs;
  int get lastIndex => _lastIndex.value;
  set lastIndex(int value) => _lastIndex.value = value;

  // 最近位置是否展开
  final _isRecentLocationsExpanded = false.obs;
  bool get isRecentLocationsExpanded => _isRecentLocationsExpanded.value;
  set isRecentLocationsExpanded(bool value) =>
      _isRecentLocationsExpanded.value = value;

  // 统计信息
  final _uplinkBytes = 0.obs;
  final _downlinkBytes = 0.obs;
  int get uplinkBytes => _uplinkBytes.value;
  int get downlinkBytes => _downlinkBytes.value;

  // 延迟信息
  final _currentDelay = 0.obs;
  int get currentDelay => _currentDelay.value;

  // 当前选择的位置
  final _selectedLocation = Rxn<Locations>();
  Locations? get selectedLocation => _selectedLocation.value;
  set selectedLocation(Locations? value) => _selectedLocation.value = value;

  // 最近使用的位置列表
  final _recentLocations = <Locations>[].obs;
  List<Locations> get recentLocations => _recentLocations;

  // 所有可用位置列表
  final _allLocations = <Locations>[].obs;
  List<Locations> get allLocations => _allLocations;

  // 是否是会员
  final _isPremium = false.obs;
  bool get isPremium => _isPremium.value;
  set isPremium(bool value) => _isPremium.value = value;

  @override
  void onInit() {
    super.onInit();
    _initializeLocations();
  }

  /// 初始化位置数据
  void _initializeLocations() {
    // 模拟数据 - 实际应用中应该从API获取
    _allLocations.addAll([
      const Locations(
        id: 1,
        name: 'LosAngles',
        code: 'US',
        country: 'United States',
        latency: 15,
      ),
      const Locations(
        id: 2,
        name: 'Singapore',
        code: 'SG',
        country: 'Singapore',
        latency: 25,
      ),
      const Locations(
        id: 3,
        name: 'Japan',
        code: 'JP',
        country: 'Japan',
        latency: 35,
      ),
      const Locations(
        id: 4,
        name: 'Madrid',
        code: 'ES',
        country: 'Spain',
        latency: 30,
      ),
    ]);

    // 设置默认选择的位置
    selectedLocation = _allLocations.first;

    // 设置最近使用的位置
    _recentLocations.addAll(_allLocations.skip(1).take(3));
  }

  /// 选择位置
  void selectLocation(Locations location) {
    selectedLocation = location;

    // 更新最近使用列表
    _updateRecentLocations(location);
  }

  /// 更新最近使用的位置列表
  void _updateRecentLocations(Locations location) {
    // 移除已存在的位置
    _recentLocations.removeWhere((loc) => loc.id == location.id);

    // 添加到列表开头
    _recentLocations.insert(0, location);

    // 保持最多3个最近使用的位置
    if (_recentLocations.length > 3) {
      _recentLocations.removeRange(3, _recentLocations.length);
    }
  }

  /// 判断位置是否快速
  bool isLocationFast(Locations location) {
    return location.latency != null && location.latency! < 30;
  }
}
