import { PortfolioElement } from '@/src/types/portfolio';

function hexToRGB(hex: string) {
  const r = parseInt(hex.slice(1, 3), 16);
  const g = parseInt(hex.slice(3, 5), 16);
  const b = parseInt(hex.slice(5, 7), 16);
  return { r, g, b };
}

function rgbToHex(r: number, g: number, b: number) {
  return `#${((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1)}`;
}

function getComplementaryColor(hexColor: string) {
  const { r, g, b } = hexToRGB(hexColor);
  return rgbToHex(255 - r, 255 - g, 255 - b);
}

function lightenColor(hexColor: string, amount: number) {
  const { r, g, b } = hexToRGB(hexColor);
  return rgbToHex(
    Math.max(0, Math.min(255, r + Math.round(amount * 255))),
    Math.max(0, Math.min(255, g + Math.round(amount * 255))),
    Math.max(0, Math.min(255, b + Math.round(amount * 255)))
  );
}
function darkenColor(hexColor: string, amount: number) {
  return lightenColor(hexColor, -amount);
}

function rgbToHSL(r: number, g: number, b: number) {
  r /= 255;
  g /= 255;
  b /= 255;
  const max = Math.max(r, g, b),
    min = Math.min(r, g, b);
  let h = 0,
    s = 0;
  const l = (max + min) / 2;
  const d = max - min;

  if (d !== 0) {
    s = d / (1 - Math.abs(2 * l - 1));
    switch (max) {
      case r:
        h = (g - b) / d + (g < b ? 6 : 0);
        break;
      case g:
        h = (b - r) / d + 2;
        break;
      case b:
        h = (r - g) / d + 4;
        break;
    }
    h *= 60;
  }

  return { h, s, l };
}

function hslToRGB(h: number, s: number, l: number) {
  let r, g, b;

  h = (h % 360) / 360;

  if (s === 0) {
    r = g = b = l;
  } else {
    const hueToRGB = (p: number, q: number, t: number) => {
      if (t < 0) t += 1;
      if (t > 1) t -= 1;
      if (t < 1 / 6) return p + (q - p) * 6 * t;
      if (t < 1 / 2) return q;
      if (t < 2 / 3) return p + (q - p) * (2 / 3 - t) * 6;
      return p;
    };

    const q = l < 0.5 ? l * (1 + s) : l + s - l * s;
    const p = 2 * l - q;
    r = hueToRGB(p, q, h + 1 / 3);
    g = hueToRGB(p, q, h);
    b = hueToRGB(p, q, h - 1 / 3);
  }

  return { r: Math.round(r * 255), g: Math.round(g * 255), b: Math.round(b * 255) };
}

function getAnalogousColor(hexColor: string, degrees: number) {
  const { r, g, b } = hexToRGB(hexColor);
  const hsl = rgbToHSL(r, g, b);
  hsl.h = (hsl.h + degrees) % 360;
  const rgb = hslToRGB(hsl.h, hsl.s, hsl.l);
  return rgbToHex(rgb.r, rgb.g, rgb.b);
}

const getColor = (styling?: PortfolioElement['styling']) => styling?.color ?? 'var(--color-accent)';

const COLOR_PRESETS = [
  {
    name: 'Modern Purple',
    colors: {
      accent: '#6E53A9',
      text: '#12121B',
      background: '#F2F0F5',
    },
  },
  {
    name: 'Warm Earthy',
    colors: {
      background: '#FFFBF4',
      text: '#2C2C2C',
      accent: '#D97706',
    },
  },
  {
    name: 'Blue',
    colors: {
      background: '#FFFFFF',
      text: '#111827',
      accent: '#3B82F6',
    },
  },
  {
    name: 'Minty Fresh',
    colors: {
      background: '#E7F9F7',
      text: '#102A27',
      accent: '#10B981',
    },
  },
  {
    name: 'Lime Zest',
    colors: {
      background: '#F4FFF1',
      text: '#122610',
      accent: '#84CC16',
    },
  },
  {
    name: 'Rose Garden',
    colors: {
      background: '#FFF1F5',
      text: '#2C0519',
      accent: '#A51E0E',
    },
  },
  {
    name: 'Lemon Drop',
    colors: {
      background: '#FEFCE8',
      text: '#3F2A00',
      accent: '#EAB308',
    },
  },
];

const DEFAULT_COLORS = {
  background: '#FFFFFF',
  text: '#1F2937',
  accent: '#298C39',
};

export { getComplementaryColor, lightenColor, darkenColor, getAnalogousColor, getColor, COLOR_PRESETS, DEFAULT_COLORS };
