import {
  ACCESS_TOKEN,
  REFRESH_TOKEN,
  REMEMBER_ME_KEY,
  TERMINAL_THEME_KEY,
  THEME_KEY,
} from '@/constants';

export function getAccessToken(): string {
  return localStorage.getItem(ACCESS_TOKEN) || '';
}

export function setAccessToken(accessToken: string) {
  localStorage.setItem(ACCESS_TOKEN, accessToken);
}

export function removeAccessToken() {
  localStorage.removeItem(ACCESS_TOKEN);
}

export function getRefreshToken(): string {
  return localStorage.getItem(REFRESH_TOKEN) || '';
}

export function setRefreshToken(refreshToken: string) {
  localStorage.setItem(REFRESH_TOKEN, refreshToken);
}

export function removeRefreshToken() {
  localStorage.removeItem(REFRESH_TOKEN);
}

export function getTerminalTheme(): string {
  return localStorage.getItem(TERMINAL_THEME_KEY) || 'dark';
}

export function setTerminalTheme(themeKey: string) {
  localStorage.setItem(TERMINAL_THEME_KEY, themeKey);
}

export function removeTerminalTheme() {
  localStorage.removeItem(TERMINAL_THEME_KEY);
}

export function getTheme(): string {
  return localStorage.getItem(THEME_KEY) || 'light';
}

export function setTheme(themeKey: string) {
  localStorage.setItem(THEME_KEY, themeKey);
}

export function removeTheme() {
  localStorage.removeItem(THEME_KEY);
}

export function getBrowserId() {
  const id = localStorage.getItem('sip-browserid');
  if (!id) {
    const { navigator, screen } = window;
    let uid = '';
    uid += navigator?.mimeTypes?.length || '';
    uid += navigator?.userAgent.replace(/\D+/g, '') || '';
    uid += navigator?.plugins?.length || '';
    uid += screen?.height || '';
    uid += screen?.width || '';
    uid += screen?.pixelDepth || '';
    localStorage.setItem('sip-browserid', uid);
    return uid;
  }
  return id;
}

// ============================================
// Remember Me Storage Operations
// ============================================

/**
 * Get encrypted credentials from localStorage
 */
export function getRememberMeData(): string | null {
  try {
    return localStorage.getItem(REMEMBER_ME_KEY);
  } catch (error) {
    console.error('Error reading remember me data:', error);
    return null;
  }
}

/**
 * Save encrypted credentials to localStorage
 */
export function setRememberMeData(encryptedData: string): void {
  try {
    localStorage.setItem(REMEMBER_ME_KEY, encryptedData);
  } catch (error) {
    console.error('Error saving remember me data:', error);
  }
}

/**
 * Remove encrypted credentials from localStorage
 */
export function removeRememberMeData(): void {
  try {
    localStorage.removeItem(REMEMBER_ME_KEY);
  } catch (error) {
    console.error('Error removing remember me data:', error);
  }
}

/**
 * Clear all localStorage data except browserId, theme and terminal theme
 */
export function clearAllData() {
  const browserId = localStorage.getItem('sip-browserid');
  const rememberMe = getRememberMeData();
  const theme = getTheme();
  const terminalTheme = getTerminalTheme();
  localStorage.clear();
  // Khôi phục các giá trị cần thiết
  if (browserId) {
    localStorage.setItem('sip-browserid', browserId);
  }
  if (rememberMe) {
    setRememberMeData(rememberMe);
  }
  setTheme(theme);
  setTerminalTheme(terminalTheme);
}

/**
 * Clear all sessionStorage data
 */
export function clearSessionData() {
  sessionStorage.clear();
}
