import 'package:niogu_app/core/constants/app_key.dart';
import 'package:niogu_app/core/enums/user_role.dart';
import 'package:shared_preferences/shared_preferences.dart';

class SystemSetting {
  static Future<bool> isLoggedIn() async {
    final prefs = await SharedPreferences.getInstance();

    return await prefs.getBool(AppKey.USER_LOGIN) ?? false;
  }

  static Future<void> setIsLoggedIn() async {
    final prefs = await SharedPreferences.getInstance();

    await prefs.setBool(AppKey.USER_LOGIN, true);
  }

  static Future<String?> getTenantId() async {
    final prefs = await SharedPreferences.getInstance();

    return await prefs.getString(AppKey.TENANT_ID);
  }

  static Future<String?> getTenantCode() async {
    final prefs = await SharedPreferences.getInstance();

    return await prefs.getString(AppKey.TENANT_CODE);
  }

  static Future<String?> getTenantName() async {
    final prefs = await SharedPreferences.getInstance();

    return await prefs.getString(AppKey.TENANT_NAME);
  }

  static Future<void> setTenant({
    String? tenantId,
    String? tenantCode,
    String? tenantName,
  }) async {
    final prefs = await SharedPreferences.getInstance();

    if (tenantId != null) {
      prefs.setString(AppKey.TENANT_ID, tenantId);
    }

    if (tenantCode != null) {
      prefs.setString(AppKey.TENANT_CODE, tenantCode);
    }

    if (tenantName != null) {
      prefs.setString(AppKey.TENANT_NAME, tenantName);
    }
  }

  static Future<String?> getOutletIdByOwner() async {
    final prefs = await SharedPreferences.getInstance();

    return await prefs.getString(AppKey.OUTLET_ID_BY_OWNER);
  }

  static Future<void> setOutletIdByOwner(String? outletId) async {
    final prefs = await SharedPreferences.getInstance();

    if (outletId != null) {
      prefs.setString(AppKey.OUTLET_ID_BY_OWNER, outletId);
    }
  }

  static Future<String?> getCurrentOutletId() async {
    final prefs = await SharedPreferences.getInstance();

    return await prefs.getString(AppKey.OUTLET_ID);
  }

  static Future<String?> getCurrentOutletName() async {
    final prefs = await SharedPreferences.getInstance();

    return await prefs.getString(AppKey.OUTLET_NAME);
  }

  static Future<void> switchOutlet({
    String? outletId,
    String? outletName,
  }) async {
    final prefs = await SharedPreferences.getInstance();
    if (outletId != null) {
      prefs.setString(AppKey.OUTLET_ID, outletId);
    }

    if (outletName != null) {
      prefs.setString(AppKey.OUTLET_NAME, outletName);
    }
  }

  static Future<String?> getCurrentUserId() async {
    final prefs = await SharedPreferences.getInstance();

    return await prefs.getString(AppKey.USER_ID);
  }

  static Future<String?> getCurrentUserName() async {
    final prefs = await SharedPreferences.getInstance();

    return await prefs.getString(AppKey.USER_NAME);
  }

  static Future<String?> getCurrentUserEmail() async {
    final prefs = await SharedPreferences.getInstance();

    return await prefs.getString(AppKey.USER_EMAIL);
  }

  static Future<UserRole?> getCurrentUserRole() async {
    final prefs = await SharedPreferences.getInstance();

    final userRole = await prefs.getString(AppKey.USER_ROLE);

    return UserRole.values.byName(userRole ?? 'owner');
  }

  static Future<void> setUser({
    String? userId,
    String? userName,
    String? userEmail,
    UserRole? userRole,
  }) async {
    final prefs = await SharedPreferences.getInstance();

    if (userId != null) {
      prefs.setString(AppKey.USER_ID, userId);
    }

    if (userName != null) {
      prefs.setString(AppKey.USER_NAME, userName);
    }

    if (userEmail != null) {
      prefs.setString(AppKey.USER_EMAIL, userEmail);
    }

    if (userRole != null) {
      prefs.setString(AppKey.USER_ROLE, userRole.role);
    }
  }

  static Future<void> clear() async {
    final prefs = await SharedPreferences.getInstance();

    await prefs.clear();
  }
}
