/**
 * Roster FUTGOGOS original: figuritas de fútbol coleccionables.
 *
 * Cada personaje se define por datos planos que el canvas consume sin crear
 * sprites ni objetos por frame. La silueta cambia con `head` + `accessory`;
 * la cara cambia con `eyes`, `brows` y `mouth`, pero el motor puede
 * sobreescribirla según el estado de animación.
 */
export type CharacterId =
  | "tizo"
  | "lumi"
  | "mango"
  | "kiwi"
  | "coco"
  | "nube"
  | "bombo"
  | "pico";

export type HeadShape = "round" | "bean" | "wide" | "pear" | "square" | "oval" | "dome" | "diamond";
export type EyeStyle = "wide" | "happy" | "sleepy" | "sharp" | "dot" | "wink" | "round";
export type BrowStyle = "thick" | "arched" | "angry" | "flat" | "thin" | "worried";
export type MouthStyle = "grin" | "smile" | "open" | "tongue" | "grit" | "smirk" | "o" | "wide" | "subtle";
export type Accessory = "mohawk" | "beanie" | "topknot" | "catears" | "helmet" | "glasses" | "visor" | "headband";

export type Character = {
  id: CharacterId;
  name: string;
  shirt: string;
  shirtLight: string;
  shirtDark: string;
  shorts: string;
  skin: string;
  skinShade: string;
  boots: string;
  soles: string;
  hair: string;
  accent: string;
  head: HeadShape;
  eyes: EyeStyle;
  brows: BrowStyle;
  mouth: MouthStyle;
  accessory: Accessory;
};

/** Tinta común: contorno grueso, legible a tamaño móvil. */
export const INK = "#21151f";
export const PAPER = "#fff8e8";
export const TEAM_COLOR = { left: "#ff5a36", right: "#3d8bfd" } as const;
export const FX_COLORS = ["#ffb703", "#ff5a36", "#3d8bfd", "#fff8e8", "#7ada4a"] as const;

export const CHARACTERS: readonly Character[] = [
  {
    id: "tizo",
    name: "Tizo",
    shirt: "#ff6a3d",
    shirtLight: "#ff9a68",
    shirtDark: "#c83b27",
    shorts: "#33506b",
    skin: "#f0b47d",
    skinShade: "#cc8557",
    boots: "#ffd34f",
    soles: "#2a2030",
    hair: "#3b2430",
    accent: "#ffe36e",
    head: "round",
    eyes: "wide",
    brows: "angry",
    mouth: "smile",
    accessory: "mohawk",
  },
  {
    id: "lumi",
    name: "Lumi",
    shirt: "#45c6a7",
    shirtLight: "#86efd4",
    shirtDark: "#248b74",
    shorts: "#314a78",
    skin: "#ffe0bd",
    skinShade: "#d7a77f",
    boots: "#f8f3df",
    soles: "#213044",
    hair: "#b75be0",
    accent: "#ffcf70",
    head: "bean",
    eyes: "happy",
    brows: "thin",
    mouth: "smile",
    accessory: "beanie",
  },
  {
    id: "mango",
    name: "Mango",
    shirt: "#ffc63d",
    shirtLight: "#ffe37b",
    shirtDark: "#cf8f18",
    shorts: "#5d3152",
    skin: "#a9653f",
    skinShade: "#764225",
    boots: "#ff704d",
    soles: "#2a1624",
    hair: "#231824",
    accent: "#5fe0a1",
    head: "wide",
    eyes: "sharp",
    brows: "thick",
    mouth: "smirk",
    accessory: "topknot",
  },
  {
    id: "kiwi",
    name: "Kiwi",
    shirt: "#7ad64f",
    shirtLight: "#b6f37f",
    shirtDark: "#45932e",
    shorts: "#264e3c",
    skin: "#9cd66d",
    skinShade: "#669c45",
    boots: "#fff0b3",
    soles: "#273a2a",
    hair: "#52632d",
    accent: "#f8dc63",
    head: "pear",
    eyes: "wink",
    brows: "arched",
    mouth: "tongue",
    accessory: "catears",
  },
  {
    id: "coco",
    name: "Coco",
    shirt: "#ec7b45",
    shirtLight: "#ffae78",
    shirtDark: "#b64a2c",
    shorts: "#3b3157",
    skin: "#8a593c",
    skinShade: "#5e3826",
    boots: "#cbeeff",
    soles: "#1d2739",
    hair: "#2b1d1b",
    accent: "#fff1a8",
    head: "square",
    eyes: "dot",
    brows: "flat",
    mouth: "o",
    accessory: "helmet",
  },
  {
    id: "nube",
    name: "Nube",
    shirt: "#8aa7ff",
    shirtLight: "#c2d2ff",
    shirtDark: "#566fc6",
    shorts: "#394568",
    skin: "#ffd7c7",
    skinShade: "#d5a397",
    boots: "#fff7cf",
    soles: "#24324b",
    hair: "#eff6ff",
    accent: "#7be3ff",
    head: "oval",
    eyes: "sleepy",
    brows: "worried",
    mouth: "smile",
    accessory: "glasses",
  },
  {
    id: "bombo",
    name: "Bombo",
    shirt: "#f15471",
    shirtLight: "#ff8ea3",
    shirtDark: "#b72d4d",
    shorts: "#34223e",
    skin: "#d58d63",
    skinShade: "#a65d41",
    boots: "#9ef0bc",
    soles: "#241f2e",
    hair: "#201827",
    accent: "#ffd04a",
    head: "dome",
    eyes: "round",
    brows: "angry",
    mouth: "open",
    accessory: "visor",
  },
  {
    id: "pico",
    name: "Pico",
    shirt: "#24b7c8",
    shirtLight: "#70e2ec",
    shirtDark: "#157f92",
    shorts: "#5d3b2c",
    skin: "#f2c98d",
    skinShade: "#c69662",
    boots: "#ff9e3d",
    soles: "#25202b",
    hair: "#5a3329",
    accent: "#ffef75",
    head: "diamond",
    eyes: "wide",
    brows: "arched",
    mouth: "grin",
    accessory: "headband",
  },
] as const;

const BY_ID = new Map(CHARACTERS.map((character) => [character.id, character]));

export function isCharacterId(value: unknown): value is CharacterId {
  return typeof value === "string" && BY_ID.has(value as CharacterId);
}

export function getCharacter(id: CharacterId): Character {
  return BY_ID.get(id) ?? CHARACTERS[0];
}

export function nextCharacterId(id: CharacterId): CharacterId {
  const index = CHARACTERS.findIndex((character) => character.id === id);
  return CHARACTERS[(index + 1 + CHARACTERS.length) % CHARACTERS.length].id;
}

export function randomCharacterId(exclude?: CharacterId): CharacterId {
  const pool = exclude ? CHARACTERS.filter((character) => character.id !== exclude) : CHARACTERS;
  return pool[Math.floor(Math.random() * pool.length)].id;
}
