import type { SceneColors, SceneType, ParallaxState } from './types';

export const SCENE_COLORS: Record<Exclude<SceneType, 'transition'>, SceneColors> = {
    eco: {
        skyTop: '#87CEEB',
        skyBottom: '#E0F7FA',
        sun: '#FFD54F',
        sunGlow: 'rgba(255, 213, 79, 0.3)',
        mountainFar: '#81C784',
        mountainMid: '#66BB6A',
        hillFront: '#4CAF50',
        treeDark: '#2E7D32',
        treeLight: '#66BB6A',
        ground: '#8BC34A',
        water: '#4FC3F7',
        cloud: 'rgba(255, 255, 255, 0.9)',
        accent: '#E91E63',
    },
    industrial: {
        skyTop: '#4A4A4A',
        skyBottom: '#757575',
        sun: '#FFA726',
        sunGlow: 'rgba(255, 167, 38, 0.2)',
        mountainFar: '#616161',
        mountainMid: '#757575',
        hillFront: '#5D4037',
        treeDark: '#4E342E',
        treeLight: '#6D4C41',
        ground: '#795548',
        water: '#546E7A',
        cloud: 'rgba(120, 120, 120, 0.7)',
        accent: '#D32F2F',
    },
    forest: {
        skyTop: '#64B5F6',
        skyBottom: '#B3E5FC',
        sun: '#FFD54F',
        sunGlow: 'rgba(255, 213, 79, 0.4)',
        mountainFar: '#1B5E20',
        mountainMid: '#2E7D32',
        hillFront: '#388E3C',
        treeDark: '#1B5E20',
        treeLight: '#4CAF50',
        ground: '#33691E',
        water: '#26A69A',
        cloud: 'rgba(255, 255, 255, 0.85)',
        accent: '#8BC34A',
    },
    deforestation: {
        skyTop: '#8D6E63',
        skyBottom: '#BCAAA4',
        sun: '#FF8A65',
        sunGlow: 'rgba(255, 138, 101, 0.3)',
        mountainFar: '#6D4C41',
        mountainMid: '#8D6E63',
        hillFront: '#A1887F',
        treeDark: '#5D4037',
        treeLight: '#8D6E63',
        ground: '#4E342E',
        water: '#78909C',
        cloud: 'rgba(180, 160, 140, 0.8)',
        accent: '#FF5722',
    },
    ocean: {
        skyTop: '#039BE5',
        skyBottom: '#81D4FA',
        sun: '#FFF176',
        sunGlow: 'rgba(255, 241, 118, 0.4)',
        mountainFar: '#0277BD',
        mountainMid: '#0288D1',
        hillFront: '#03A9F4',
        treeDark: '#00897B',
        treeLight: '#26A69A',
        ground: '#4DB6AC',
        water: '#00ACC1',
        cloud: 'rgba(255, 255, 255, 0.95)',
        accent: '#00BCD4',
    },
    oilRig: {
        skyTop: '#37474F',
        skyBottom: '#546E7A',
        sun: '#FF6F00',
        sunGlow: 'rgba(255, 111, 0, 0.25)',
        mountainFar: '#263238',
        mountainMid: '#37474F',
        hillFront: '#455A64',
        treeDark: '#263238',
        treeLight: '#37474F',
        ground: '#1C313A',
        water: '#1C313A',
        cloud: 'rgba(80, 80, 80, 0.8)',
        accent: '#FF5722',
    },
    city: {
        skyTop: '#42A5F5',
        skyBottom: '#90CAF9',
        sun: '#FFEB3B',
        sunGlow: 'rgba(255, 235, 59, 0.35)',
        mountainFar: '#78909C',
        mountainMid: '#90A4AE',
        hillFront: '#B0BEC5',
        treeDark: '#4CAF50',
        treeLight: '#81C784',
        ground: '#ECEFF1',
        water: '#26C6DA',
        cloud: 'rgba(255, 255, 255, 0.9)',
        accent: '#2196F3',
    },
    pollutedCity: {
        skyTop: '#424242',
        skyBottom: '#616161',
        sun: '#EF6C00',
        sunGlow: 'rgba(239, 108, 0, 0.2)',
        mountainFar: '#424242',
        mountainMid: '#616161',
        hillFront: '#757575',
        treeDark: '#4E342E',
        treeLight: '#5D4037',
        ground: '#3E2723',
        water: '#37474F',
        cloud: 'rgba(90, 90, 90, 0.85)',
        accent: '#F44336',
    },
};

export function lerpColor(color1: string, color2: string, t: number): string {
    const parse = (c: string) => {
        if (c.startsWith('#')) {
            const hex = c.slice(1);
            return {
                r: parseInt(hex.slice(0, 2), 16),
                g: parseInt(hex.slice(2, 4), 16),
                b: parseInt(hex.slice(4, 6), 16),
                a: 1,
            };
        }
        const match = c.match(/rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*([\d.]+))?\)/);
        if (match) {
            return {
                r: parseInt(match[1]),
                g: parseInt(match[2]),
                b: parseInt(match[3]),
                a: parseFloat(match[4] ?? '1'),
            };
        }
        return { r: 0, g: 0, b: 0, a: 1 };
    };

    const c1 = parse(color1);
    const c2 = parse(color2);

    const r = Math.round(c1.r + (c2.r - c1.r) * t);
    const g = Math.round(c1.g + (c2.g - c1.g) * t);
    const b = Math.round(c1.b + (c2.b - c1.b) * t);
    const a = c1.a + (c2.a - c1.a) * t;

    return `rgba(${r}, ${g}, ${b}, ${a})`;
}

function blendColors(colors1: SceneColors, colors2: SceneColors, t: number): SceneColors {
    const result = {} as SceneColors;
    for (const key of Object.keys(colors1) as (keyof SceneColors)[]) {
        result[key] = lerpColor(colors1[key], colors2[key], t);
    }
    return result;
}

export function getSceneColors(state: ParallaxState): SceneColors {
    const { sceneType, progress, blendToScene, blendProgress } = state;

    if (sceneType === 'transition') {
        return blendColors(SCENE_COLORS.eco, SCENE_COLORS.industrial, progress);
    }

    if (blendToScene && blendProgress !== undefined && blendToScene !== 'transition') {
        const fromColors = SCENE_COLORS[sceneType as keyof typeof SCENE_COLORS] || SCENE_COLORS.eco;
        const toColors = SCENE_COLORS[blendToScene as keyof typeof SCENE_COLORS] || SCENE_COLORS.eco;
        return blendColors(fromColors, toColors, blendProgress);
    }

    return SCENE_COLORS[sceneType as keyof typeof SCENE_COLORS] || SCENE_COLORS.eco;
}
