import { computed, readonly, ref } from "vue";
import { APP_COMPANY } from "../config/company";

export type ThemeKey = "blue" | "electric" | "green" | "aurora" | "purple";

export interface AppTheme {
  key: ThemeKey;
  name: string;
  description: string;
  primary: string;
  primaryDark: string;
  primaryLight: string;
  primarySoft: string;
  primaryBorder: string;
  pageBackground: string;
  cardBackground: string;
}

export const THEME_STORAGE_KEY = `mom_app_theme_${APP_COMPANY.key}`;

export const appThemes: AppTheme[] = [
  {
    key: "blue",
    name: "商务蓝",
    description: "清晰稳重，适合日常办公",
    primary: "#1769d8",
    primaryDark: "#0f54b8",
    primaryLight: "#4b8ee8",
    primarySoft: "#eef5ff",
    primaryBorder: "#bfdbff",
    pageBackground: "#f6f7f9",
    cardBackground: "#ffffff",
  },
  {
    key: "green",
    name: "生产绿",
    description: "沉稳自然，突出生产状态",
    primary: "#16835d",
    primaryDark: "#0f6949",
    primaryLight: "#42a782",
    primarySoft: "#eaf8f2",
    primaryBorder: "#bdebd7",
    pageBackground: "#f5f8f7",
    cardBackground: "#ffffff",
  },
  {
    key: "electric",
    name: "电力深蓝",
    description: "冷峻清晰，突出电力运行状态",
    primary: "#0879e8",
    primaryDark: "#0756ac",
    primaryLight: "#47a9ff",
    primarySoft: "#e9f5ff",
    primaryBorder: "#b7ddff",
    pageBackground: "#f2f7fb",
    cardBackground: "#ffffff",
  },
  {
    key: "aurora",
    name: "智能极光",
    description: "紫青渐变，体现智能科技感",
    primary: "#7257df",
    primaryDark: "#5640b7",
    primaryLight: "#9a84f3",
    primarySoft: "#f2efff",
    primaryBorder: "#d9d0ff",
    pageBackground: "#f7f5fc",
    cardBackground: "#ffffff",
  },
  {
    key: "purple",
    name: "科技紫",
    description: "层次鲜明，适合数据场景",
    primary: "#7156c7",
    primaryDark: "#5940ad",
    primaryLight: "#927de0",
    primarySoft: "#f3efff",
    primaryBorder: "#d8ccff",
    pageBackground: "#f7f6fa",
    cardBackground: "#ffffff",
  },
];

const companyDefaultThemeKey: ThemeKey = APP_COMPANY.key === "changhui"
  ? "electric"
  : APP_COMPANY.key === "kangyi"
    ? "aurora"
    : "blue";
const defaultTheme = appThemes.find(item => item.key === companyDefaultThemeKey) || appThemes[0];
const currentTheme = ref<AppTheme>(defaultTheme);

function findTheme(key?: string) {
  return appThemes.find((item) => item.key === key) || defaultTheme;
}

function syncPlatformChrome(theme: AppTheme) {
  uni.setNavigationBarColor({
    frontColor: "#000000",
    backgroundColor: theme.cardBackground,
    animation: { duration: 180, timingFunc: "easeIn" },
    fail: () => undefined,
  });
  uni.setTabBarStyle({
    color: "#7a8494",
    selectedColor: theme.primary,
    backgroundColor: theme.cardBackground,
    borderStyle: "white",
    fail: () => undefined,
  });
}

function syncCssVariables(theme: AppTheme) {
  // #ifdef H5
  if (typeof document !== "undefined") {
    const root = document.documentElement;
    root.style.setProperty("--color-primary", theme.primary);
    root.style.setProperty("--color-primary-dark", theme.primaryDark);
    root.style.setProperty("--color-primary-light", theme.primaryLight);
    root.style.setProperty("--color-primary-soft", theme.primarySoft);
    root.style.setProperty("--color-primary-border", theme.primaryBorder);
    root.style.setProperty("--color-bg-page", theme.pageBackground);
    root.style.setProperty("--color-bg-card", theme.cardBackground);
  }
  // #endif
}

export function applyTheme(theme: AppTheme) {
  currentTheme.value = theme;
  syncCssVariables(theme);
  syncPlatformChrome(theme);
  uni.$emit("app-theme-change", theme);
}

export function restoreTheme() {
  let savedKey: string | undefined;
  try {
    savedKey = uni.getStorageSync(THEME_STORAGE_KEY) || undefined;
  } catch {
    savedKey = undefined;
  }
  applyTheme(findTheme(savedKey));
}

export function setTheme(key: ThemeKey) {
  const theme = findTheme(key);
  uni.setStorageSync(THEME_STORAGE_KEY, theme.key);
  applyTheme(theme);
}

export function resetTheme() {
  uni.removeStorageSync(THEME_STORAGE_KEY);
  applyTheme(defaultTheme);
}

export function useAppTheme() {
  return {
    themes: appThemes,
    theme: readonly(currentTheme),
    themeKey: computed(() => currentTheme.value.key),
    primaryColor: computed(() => currentTheme.value.primary),
    themeStyle: computed(() => ({
      "--color-primary": currentTheme.value.primary,
      "--color-primary-dark": currentTheme.value.primaryDark,
      "--color-primary-light": currentTheme.value.primaryLight,
      "--color-primary-soft": currentTheme.value.primarySoft,
      "--color-primary-border": currentTheme.value.primaryBorder,
      "--color-bg-page": currentTheme.value.pageBackground,
      "--color-bg-card": currentTheme.value.cardBackground,
    })),
    setTheme,
    resetTheme,
    restoreTheme,
  };
}
