import type { DrawContext } from '../types';

export function drawPollutedOceanWaves(dc: DrawContext): void {
    const { ctx, width, height } = dc;
    const time = Date.now() * 0.001;

    for (let layer = 0; layer < 3; layer++) {
        const yBase = height * (0.55 + layer * 0.12);
        const waveHeight = 20 - layer * 4; // Taller waves
        const speed = 40 + layer * 20;

        // Base Water
        ctx.beginPath();
        ctx.moveTo(-100, height);

        // Generate points for fill
        for (let x = -100; x <= width + 100; x += 10) {
            const y = yBase + Math.sin((x + time * speed) * 0.015) * waveHeight; // Slower frequency
            ctx.lineTo(x, y);
        }

        ctx.lineTo(width + 100, height);
        ctx.closePath();

        // Layer Colors - Murky greenish-grey styles (Polluted look but lighter for visibility)
        if (layer === 0) ctx.fillStyle = '#62757f'; // Lighter muted blue-grey
        else if (layer === 1) ctx.fillStyle = '#4b5d67'; // Medium muted
        else ctx.fillStyle = '#37474f'; // Dark base, but not black

        ctx.fill();
    }
}

export function drawFloatingTrash(dc: DrawContext): void {
    const { ctx, width, height } = dc;
    const time = Date.now() * 0.001;

    // Scale multiplier to make trash visible
    const S = 1.8; 

    const drawTrashItem = (x: number, y: number, type: 'bottle' | 'bag' | 'net' | 'tire') => {
        const bob = Math.sin(time * 2 + x) * 5;
        const ty = y + bob;

        if (type === 'bottle') {
            // Bright Green Plastic Bottle
            ctx.fillStyle = '#76FF03'; 
            ctx.strokeStyle = '#33691E';
            ctx.lineWidth = 1;

            ctx.beginPath();
            ctx.roundRect(x - 8 * S, ty - 3 * S, 16 * S, 6 * S, 2);
            ctx.fill();
            ctx.stroke();
            
            // Neck
            ctx.fillStyle = '#CCFF90';
            ctx.fillRect(x + 8 * S, ty - 1 * S, 4 * S, 2 * S);
            
            // Label
            ctx.fillStyle = '#FFFFFF';
            ctx.fillRect(x - 2 * S, ty - 3 * S, 4 * S, 6 * S);

        } else if (type === 'bag') {
            // White Plastic Bag
            ctx.fillStyle = '#F5F5F5';
            ctx.strokeStyle = '#BDBDBD';
            ctx.lineWidth = 1;
            
            ctx.beginPath();
            ctx.moveTo(x, ty);
            ctx.bezierCurveTo(x + 12*S, ty - 12*S, x + 24*S, ty - 6*S, x + 30*S, ty);
            ctx.bezierCurveTo(x + 18*S, ty + 12*S, x + 6*S, ty + 6*S, x, ty);
            ctx.fill();
            ctx.stroke();

        } else if (type === 'net') {
            // Old Fishing Net
            ctx.strokeStyle = '#5D4037';
            ctx.lineWidth = 2; // Thicker lines
            ctx.beginPath();
            for(let i=0; i<6; i++) {
                ctx.moveTo(x + i*5*S, ty);
                ctx.lineTo(x + i*5*S + 15*S, ty + 20*S);
                ctx.moveTo(x, ty + i*4*S);
                ctx.lineTo(x + 30*S, ty + i*4*S + 8*S);
            }
            ctx.stroke();

        } else if (type === 'tire') {
            // Old Tire
            ctx.fillStyle = '#212121';
            ctx.strokeStyle = '#000000';
            ctx.lineWidth = 2;

            ctx.beginPath();
            ctx.ellipse(x, ty + 5*S, 15*S, 8*S, 0, 0, Math.PI * 2);
            ctx.fill();
            ctx.stroke();

            ctx.fillStyle = '#546E7A'; // Inner water color match roughly
            ctx.beginPath();
            ctx.ellipse(x, ty + 5*S, 8*S, 4*S, 0, 0, Math.PI * 2);
            ctx.fill();
        }
    };

    // Draw mass amounts of trash
    const trashItems = [
        { x: 0.1, y: 0.7, type: 'bag' },
        { x: 0.15, y: 0.85, type: 'bottle' },
        { x: 0.25, y: 0.65, type: 'tire' },
        { x: 0.3, y: 0.75, type: 'net' },
        { x: 0.4, y: 0.8, type: 'bottle' },
        { x: 0.45, y: 0.6, type: 'bag' },
        { x: 0.55, y: 0.72, type: 'tire' },
        { x: 0.65, y: 0.82, type: 'bottle' },
        { x: 0.7, y: 0.68, type: 'net' },
        { x: 0.8, y: 0.76, type: 'bag' },
        { x: 0.85, y: 0.62, type: 'bottle' },
        { x: 0.92, y: 0.88, type: 'bag' },
        { x: 0.05, y: 0.78, type: 'bottle' },
        
        { x: 0.12, y: 0.62, type: 'bottle' },
        { x: 0.22, y: 0.78, type: 'bag' },
        { x: 0.35, y: 0.82, type: 'tire' },
        { x: 0.38, y: 0.65, type: 'bottle' },
        { x: 0.48, y: 0.74, type: 'net' },
        { x: 0.58, y: 0.86, type: 'bottle' },
        { x: 0.62, y: 0.64, type: 'bag' },
        { x: 0.72, y: 0.79, type: 'tire' },
        { x: 0.82, y: 0.67, type: 'bottle' },
        { x: 0.88, y: 0.76, type: 'net' },
        { x: 0.08, y: 0.84, type: 'tire' },
        { x: 0.95, y: 0.68, type: 'bag' },
        { x: 0.52, y: 0.68, type: 'bottle' }
    ];

    trashItems.forEach(item => {
        const drift = Math.sin(time * 0.5 + item.x * 10) * 20;
        drawTrashItem(
            width * item.x + drift, 
            height * item.y, 
            item.type as any
        );
    });
}

export function drawPollutedFish(dc: DrawContext): void {
    const { ctx, width, height } = dc;
    const time = Date.now() * 0.001;
    const S = 0.6; // Scale

    const drawSickFish = (seed: number, baseX: number, baseY: number, color: string, speedBase: number, isSkeleton: boolean) => {
         const t = time * speedBase; 
         
         const dx = (baseX + t * 20 + Math.sin(t * 0.5 + seed) * 15); 
         const moveX = ((dx % (width + 50)) + (width + 50)) % (width + 50) - 25;

         const moveY = baseY + Math.sin(t * 1.0 + seed) * 8 + Math.cos(t * 2.5 + seed * 2) * 3; 
         
         const dy = (Math.cos(t * 1.0 + seed) * 10 - Math.sin(t * 2.5 + seed * 2) * 10) * 0.02;

         ctx.save();
         ctx.translate(moveX, moveY);
         ctx.rotate(dy);
         
         if (isSkeleton) {
             ctx.strokeStyle = '#E0E0E0'; // Very bright grey for visible skeletons
             ctx.lineWidth = 1.5; // Thicker lines
             ctx.beginPath();
             ctx.moveTo(0, 0); 
             ctx.lineTo(10*S, 0); 
             ctx.moveTo(3*S, -3*S); ctx.lineTo(3*S, 3*S);
             ctx.moveTo(6*S, -3*S); ctx.lineTo(6*S, 3*S);
             ctx.stroke();
             
             ctx.beginPath();
             ctx.moveTo(10*S, 0);
             ctx.lineTo(8*S, -4*S);
             ctx.lineTo(12*S, 0);
             ctx.lineTo(8*S, 4*S);
             ctx.closePath();
             ctx.stroke();
         } else {
             ctx.fillStyle = color;
             ctx.beginPath();
             ctx.ellipse(0, 0, 10 * S, 4 * S, 0, 0, Math.PI*2);
             ctx.fill();
             ctx.beginPath();
             ctx.moveTo(-8 * S, 0);
             ctx.lineTo(-14 * S, -4 * S);
             ctx.lineTo(-14 * S, 4 * S);
             ctx.fill();
             
             ctx.strokeStyle = '#000000';
             ctx.lineWidth = 1;
             ctx.beginPath();
             ctx.moveTo(4*S, -2*S); ctx.lineTo(6*S, 0);
             ctx.moveTo(6*S, -2*S); ctx.lineTo(4*S, 0);
             ctx.stroke();
         }
         ctx.restore();
    };

    // Schools of POLLUTED fish - Reduced count
    // Sickly pale green
    for(let i=0; i<5; i++) drawSickFish(i*9, width * 0.15 + i*25, height * 0.88, '#C5E1A5', 0.5, false);
    
    // Pale muddy brown
    for(let i=0; i<4; i++) drawSickFish(i*11 + 50, width * 0.6 + i*30, height * 0.92, '#D7CCC8', 0.4, false);
    
    // Skeleton fish - Reduced count
    for(let i=0; i<3; i++) drawSickFish(i*13 + 100, width * 0.3 + i*40, height * 0.85, '', 0.3, true);
    for(let i=0; i<2; i++) drawSickFish(i*19 + 200, width * 0.8 + i*50, height * 0.9, '', 0.2, true);
    
    // Extra top layer fish
    for(let i=0; i<3; i++) drawSickFish(i*7 + 300, width * 0.05 + i*60, height * 0.80, '#E6EE9C', 0.6, false);
}

export function drawPollutedOceanScene(dc: DrawContext): void {
    drawPollutedOceanWaves(dc);
    drawFloatingTrash(dc);
    drawPollutedFish(dc);
}
