"use client";

import React from "react";
import styles from "./ColorChanger.module.css";
import Image from "next/image";

type ColorScheme = {
  name: string;
  bg: string;
  bgAlt: string;
  fg: string;
  fgAlt: string;
  error?: string;
  warning?: string;
};

const colors: ColorScheme[] = [
  {
    name: "Monokai Pro",
    bg: "#2d2a2e",
    bgAlt: "#403e41",
    fg: "#fcfcfa",
    fgAlt: "#727072",
    error: "#ff6188",
    warning: "#ffd866"
  },
  {
    name: "Gruvbox Dark",
    bg: "#282828",
    bgAlt: "#3c3836",
    fg: "#ebdbb2",
    fgAlt: "#a89984"
  },
  {
    name: "Amora",
    bg: "#1a1a1a",
    bgAlt: "#171717",
    fg: "#DEDBEB",
    fgAlt: "#5c5c5c"
  },
  {
    name: "Amora Focus",
    bg: "#302838",
    bgAlt: "#2a2331",
    fg: "#dedbeb",
    fgAlt: "#5c5c5c"
  },
  {
    name: "Rosé Pine",
    bg: "#191724",
    bgAlt: "#26233a",
    fg: "#e0def4",
    fgAlt: "#908caa"
  },
  {
    name: "Rosé Pine Moon",
    bg: "#232136",
    bgAlt: "#393552",
    fg: "#e0def4",
    fgAlt: "#908caa"
  },
  {
    name: "Nord",
    bg: "#2e3440",
    bgAlt: "#3b4252",
    fg: "#eceff4",
    fgAlt: "#d8dee9"
  },
  {
    name: "lovelace",
    bg: "#1d1f28",
    bgAlt: "#282a36",
    fg: "#fdfdfd",
    fgAlt: "#414458"
  },
  {
    name: "skyfall",
    bg: "#282f37",
    bgAlt: "#20262c",
    fg: "#f1fcf9",
    fgAlt: "#465463"
  },
  {
    name: "Catppuccin Frappe",
    bg: "#303446",
    bgAlt: "#51576d",
    fg: "#c6d0f5",
    fgAlt: "#a5adce"
  },
  {
    name: "Catppuccin Macchiato",
    bg: "#24273a",
    bgAlt: "#494d64",
    fg: "#cad3f5",
    fgAlt: "#a5adcb"
  },
  {
    name: "Catppuccin Mocha",
    bg: "#1e1e2e",
    bgAlt: "#45475a",
    fg: "#cdd6f4",
    fgAlt: "#a6adc8"
  }
];

const fallback = colors[0];

export default function ColorChanger() {
  const [current, setCurrent] = React.useState<string>("Monokai Pro");

  return (
    <Image
      src="/paint.svg"
      width="64"
      height="64"
      alt="paint"
      title={current}
      onClick={() => {
        const pool = colors.filter((x) => x.name !== current);
        const idx = Math.floor(Math.random() * pool.length);
        const colorScheme = pool[idx];

        const fixedColors = {
          "--bg": colorScheme.bg,
          "--bg-alt": colorScheme.bgAlt,
          "--fg": colorScheme.fg,
          "--fg-alt": colorScheme.fgAlt,
          "--error": colorScheme.error ?? fallback.error!,
          "--warning": colorScheme.warning ?? fallback.warning!
        };

        for (const [k, v] of Object.entries(fixedColors)) {
          document.documentElement.style.setProperty(k, v);
        }

        setCurrent(colorScheme.name);
      }}
      className={styles["color-changer"]}
    />
  );
}