import 'package:hive_flutter/hive_flutter.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';

class StorageService {
  static late Box _box;
  static const FlutterSecureStorage _secureStorage = FlutterSecureStorage(
    aOptions: AndroidOptions(
      encryptedSharedPreferences: true,
    ),
    iOptions: IOSOptions(
      accessibility: KeychainAccessibility.first_unlock_this_device,
    ),
  );

  /// 初始化儲存服務
  static Future<void> init() async {
    await Hive.initFlutter();
    _box = await Hive.openBox('dramaling_storage');
  }

  // 一般資料存取 (使用Hive)
  
  /// 儲存資料
  static Future<void> setData<T>(String key, T value) async {
    await _box.put(key, value);
  }

  /// 獲取資料
  static T? getData<T>(String key) {
    return _box.get(key) as T?;
  }

  /// 移除資料
  static Future<void> removeData(String key) async {
    await _box.delete(key);
  }

  /// 清除所有資料
  static Future<void> clearAll() async {
    await _box.clear();
  }

  /// 檢查資料是否存在
  static bool hasData(String key) {
    return _box.containsKey(key);
  }

  // 安全資料存取 (使用FlutterSecureStorage)
  
  /// 安全儲存資料 (用於敏感資料如token)
  static Future<void> setSecureData(String key, String value) async {
    await _secureStorage.write(key: key, value: value);
  }

  /// 獲取安全資料
  static Future<String?> getSecureData(String key) async {
    return await _secureStorage.read(key: key);
  }

  /// 移除安全資料
  static Future<void> removeSecureData(String key) async {
    await _secureStorage.delete(key: key);
  }

  /// 清除所有安全資料
  static Future<void> clearSecureData() async {
    await _secureStorage.deleteAll();
  }

  /// 檢查安全資料是否存在
  static Future<bool> hasSecureData(String key) async {
    final data = await _secureStorage.read(key: key);
    return data != null;
  }

  // 便捷方法

  /// 儲存用戶Token
  static Future<void> saveTokens({
    required String accessToken,
    required String refreshToken,
  }) async {
    await setSecureData('access_token', accessToken);
    await setSecureData('refresh_token', refreshToken);
  }

  /// 獲取存取Token
  static Future<String?> getAccessToken() async {
    return await getSecureData('access_token');
  }

  /// 獲取刷新Token
  static Future<String?> getRefreshToken() async {
    return await getSecureData('refresh_token');
  }

  /// 清除所有Token
  static Future<void> clearTokens() async {
    await removeSecureData('access_token');
    await removeSecureData('refresh_token');
  }

  /// 儲存用戶偏好設定
  static Future<void> saveUserPreferences({
    String? language,
    String? theme,
    bool? soundEnabled,
    bool? vibrationEnabled,
  }) async {
    if (language != null) await setData('language', language);
    if (theme != null) await setData('theme', theme);
    if (soundEnabled != null) await setData('sound_enabled', soundEnabled);
    if (vibrationEnabled != null) await setData('vibration_enabled', vibrationEnabled);
  }

  /// 獲取用戶偏好設定
  static Map<String, dynamic> getUserPreferences() {
    return {
      'language': getData<String>('language') ?? 'zh',
      'theme': getData<String>('theme') ?? 'system',
      'sound_enabled': getData<bool>('sound_enabled') ?? true,
      'vibration_enabled': getData<bool>('vibration_enabled') ?? true,
    };
  }
}