import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../../../app/data/sp/ix_sp.dart';
import '../ix_theme.dart';

/// 响应式主题扩展
/// 让 Get.theme 变成响应式对象
class ReactiveTheme {
  static final RxBool _isLightTheme = true.obs;

  /// 初始化主题状态
  static void init() {
    _isLightTheme.value = IXSP.getThemeIsLight();
  }

  /// 获取当前主题状态
  static bool get isLightTheme => _isLightTheme.value;

  /// 切换主题
  static void toggleTheme() {
    _isLightTheme.value = !_isLightTheme.value;
    IXSP.setThemeIsLight(_isLightTheme.value);
    Get.changeThemeMode(_isLightTheme.value ? ThemeMode.light : ThemeMode.dark);
  }

  /// 设置主题
  static void setTheme(bool isLight) {
    if (_isLightTheme.value != isLight) {
      _isLightTheme.value = isLight;
      IXSP.setThemeIsLight(isLight);
      Get.changeThemeMode(isLight ? ThemeMode.light : ThemeMode.dark);
    }
  }

  /// 响应式主题数据
  static ThemeData get theme =>
      IXTheme.getThemeData(isLight: _isLightTheme.value);
}

/// 扩展 GetX，添加响应式主题方法
extension GetXThemeExtension on GetInterface {
  /// 响应式主题数据
  ThemeData get reactiveTheme => ReactiveTheme.theme;
}
