import { getToken } from "../storage";
const LOCALSTORAGE_KEY = "reader_settings_local";
export async function loadSettings(): Promise<ReaderSettings> {
  const token = getToken();
  if (!token) {
    const local = localStorage.getItem(LOCALSTORAGE_KEY);
    return local ? JSON.parse(local) : getDefaultSettings();
  }
  try {
    const response = await fetch("/readers/settings", {
      headers: {
        Authorization: `Bearer ${token}`,
        "Content-Type": "application/json",
      },
    });
    const settings = await response.json();
    localStorage.setItem(LOCALSTORAGE_KEY, JSON.stringify(settings));
    return settings;
  } catch (error) {
    const local = localStorage.getItem(LOCALSTORAGE_KEY);
    return local ? JSON.parse(local) : getDefaultSettings();
  }
}
export async function saveSettings(
  settings: Partial<ReaderSettings>,
): Promise<void> {
  const token = getToken();
  const current = await loadSettings();
  const updated = { ...current, ...settings };
  if (!token) {
    localStorage.setItem(LOCALSTORAGE_KEY, JSON.stringify(updated));
    return;
  }
  try {
    await fetch("/readers/settings", {
      method: "PUT",
      headers: {
        Authorization: `Bearer ${token}`,
        "Content-Type": "application/json",
      },
      body: JSON.stringify(settings),
    });
    localStorage.setItem(LOCALSTORAGE_KEY, JSON.stringify(updated));
  } catch (error) {
    localStorage.setItem(LOCALSTORAGE_KEY, JSON.stringify(updated));
  }
}
export async function syncSettings(): Promise<void> {
  const local = localStorage.getItem(LOCALSTORAGE_KEY);
  if (!local) return;
  const settings = JSON.parse(local);
  const token = getToken();
  if (token) {
    try {
      await fetch("/readers/settings", {
        method: "PUT",
        headers: {
          Authorization: `Bearer ${token}`,
          "Content-Type": "application/json",
        },
        body: JSON.stringify(settings),
      });
    } catch (error) {
      console.error("Failed to sync settings:", error);
    }
  }
}
export function getDefaultSettings(): ReaderSettings {
  return {
    chrome_behavior: "auto-hide",
    progress_mode: "pages",
    chrome_theme: "tokyo-night",
    reading_theme: "light",
    reading_mode: "dark",
    reading_font: "literata",
    tap_zone_size: 30,
    auto_scroll: false,
    font_size: 18,
    line_height: 1.6,
    margin_width: 20,
    double_page_spread: true,
    pdf_interaction_mode: "select",
    tap_zones_enabled: true,
    reading_direction: "ltr",
    hardware_acceleration: true,
    panel_layout: {
      toc: {
        side: "left",
        visible: true,
        collapsed: false,
        width_px: 320,
        order: 1,
        locked: false,
        last_valid_side: "left",
      },
      settings: {
        side: "left",
        visible: false,
        collapsed: true,
        width_px: 380,
        order: 2,
        locked: false,
        last_valid_side: "left",
      },
      navigator: {
        side: "right",
        visible: false,
        collapsed: true,
        width_px: 280,
        order: 3,
        locked: false,
        last_valid_side: "right",
      },
      bookmarks: {
        side: "right",
        visible: false,
        collapsed: true,
        width_px: 280,
        order: 4,
        locked: false,
        last_valid_side: "right",
      },
      mobile_nav_visible: true,
    },
  } as ReaderSettings;
}
