import 'dart:developer';

import 'package:hive/hive.dart';
import 'package:jwt_decoder/jwt_decoder.dart';
import '../presentation/screens/login/models/response_user.dart';

final LocalStorage = HiveLocalStorage();

class HiveLocalStorage {
  final box = Hive.box('LocalDBName');

  void open() {
    Hive.openBox('LocalDBName');
  }

  String getString(String key, {String? defaultValue}) {
    try {
      return box.get(key, defaultValue: defaultValue ?? '');
    } catch (_) {
      return '';
    }
  }

  int getInt(String key, {int? defaultValue}) {
    try {
      return box.get(key, defaultValue: defaultValue ?? -1);
    } catch (_) {
      return -1;
    }
  }

  bool getBool(String key, {bool? defaultValue}) {
    try {
      return box.get(key, defaultValue: defaultValue ?? false);
    } catch (_) {
      return false;
    }
  }

  dynamic getObject(String key) {
    try {
      return box.get(key);
    } catch (_) {
      return null;
    }
  }

  void save(String key, dynamic value) {
    box.put(key, value);
  }

  void clearUserInfo() {
    LocalStorage.save(LocalStorageKey.is_logged, false);
    LocalStorage.save(LocalStorageKey.access_token, '');
    LocalStorage.save(LocalStorageKey.full_name, '');
    LocalStorage.save(LocalStorageKey.user_id, '');
    LocalStorage.save(LocalStorageKey.username, '');
    LocalStorage.save(LocalStorageKey.username, '');
    LocalStorage.save(LocalStorageKey.trai_hien_tai, '');
    LocalStorage.save(LocalStorageKey.ten_hien_tai, '');
    LocalStorage.save(LocalStorageKey.ma_trais, []);
    LocalStorage.save(LocalStorageKey.user_roles, []);
    LocalStorage.save(LocalStorageKey.user_roles_by_tenants, []);
  }

  void saveUserInfo(ResponseUser data, String username) {
    try {
      var decodedToken = JwtDecoder.decode(data.accessToken!);
      var givenName = decodedToken['given_name'] ?? '';
      var fullName = decodedToken['family_name'] ?? '';
      var userId = decodedToken['userid'] ?? '';

      // var roles = decodedToken['http://schemas.microsoft.com/ws/2008/06/identity/claims/role'] as List;
      // var roles = decodedToken['resource_access']['uca']['roles'] as List;
      // var userRoles = roles.join('|');
      // LocalStorage.save(LocalStorageKey.user_permission, userRoles);

      LocalStorage.save(LocalStorageKey.is_logged, true);
      LocalStorage.save(LocalStorageKey.username, username);
      LocalStorage.save(LocalStorageKey.access_token, data.accessToken);
      LocalStorage.save(LocalStorageKey.refresh_token, '');
      // LocalStorage.save(LocalStorageKey.given_name, givenName);
      // LocalStorage.save(LocalStorageKey.full_name, fullName);
      // LocalStorage.save(LocalStorageKey.user_id, userId);
      // LocalStorage.save(LocalStorageKey.user_roles, roles);
    } catch (e) {
      print(e);
    }
  }
}

class LocalStorageKey {
  static const String username = 'username';
  static const String access_token = 'access_token';
  static const String refresh_token = 'refresh_token';
  static const String is_logged = 'is_logged';
  static const String full_name = 'full_name';
  static const String given_name = 'given_name';
  static const String user_id = 'user_id';
  static const String user_roles = 'user_roles';
  static const String user_roles_by_tenants = 'user_roles_by_tenants';
  static const String ma_trais = 'ma_trais';
  static const String ten_trais = 'ten_trais';
  static const String trai_hien_tai = 'trai_hien_tai';
  static const String ten_hien_tai = 'ten_hien_tai';
  static const String device_key = 'device_key';
}
