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

export function drawStumps(dc: DrawContext): void {
    const { ctx, width, height, state } = dc;
    const parallax = state.mouseX * 0.7 + state.scrollY * 0.5;

    const drawStump = (x: number, y: number, scale: number) => {
        ctx.fillStyle = '#5D4037';
        ctx.fillRect(x - 12 * scale + parallax, y - 8 * scale, 24 * scale, 25 * scale);

        ctx.fillStyle = '#8D6E63';
        ctx.beginPath();
        ctx.ellipse(x + parallax, y - 8 * scale, 15 * scale, 8 * scale, 0, 0, Math.PI * 2);
        ctx.fill();

        ctx.strokeStyle = '#5D4037';
        ctx.lineWidth = 1;
        for (let ring = 1; ring <= 3; ring++) {
            ctx.beginPath();
            ctx.ellipse(x + parallax, y - 8 * scale, ring * 4 * scale, ring * 2.5 * scale, 0, 0, Math.PI * 2);
            ctx.stroke();
        }
    };

    const stumpPositions = [
        { x: width * 0.08, y: height * 0.78, scale: 1.2 },
        { x: width * 0.18, y: height * 0.82, scale: 0.9 },
        { x: width * 0.28, y: height * 0.76, scale: 1.4 },
        { x: width * 0.42, y: height * 0.8, scale: 1.0 },
        { x: width * 0.55, y: height * 0.78, scale: 1.3 },
        { x: width * 0.68, y: height * 0.82, scale: 0.8 },
        { x: width * 0.78, y: height * 0.77, scale: 1.5 },
        { x: width * 0.9, y: height * 0.8, scale: 1.1 },
    ];

    stumpPositions.forEach(s => drawStump(s.x, s.y, s.scale));
}

export function drawFallenLogs(dc: DrawContext): void {
    const { ctx, width, height, state } = dc;
    const parallax = state.mouseX * 0.6;

    const drawLog = (x: number, y: number, length: number, angle: number) => {
        ctx.save();
        ctx.translate(x + parallax, y);
        ctx.rotate(angle);

        ctx.fillStyle = '#6D4C41';
        ctx.beginPath();
        ctx.ellipse(0, 0, length, 12, 0, 0, Math.PI * 2);
        ctx.fill();

        ctx.fillStyle = '#8D6E63';
        ctx.beginPath();
        ctx.ellipse(length - 5, 0, 8, 12, 0, 0, Math.PI * 2);
        ctx.fill();

        ctx.strokeStyle = '#4E342E';
        ctx.lineWidth = 2;
        for (let i = -length + 20; i < length - 20; i += 25) {
            ctx.beginPath();
            ctx.moveTo(i, -10);
            ctx.lineTo(i + 10, 10);
            ctx.stroke();
        }

        ctx.restore();
    };

    drawLog(width * 0.2, height * 0.85, 80, 0.1);
    drawLog(width * 0.5, height * 0.86, 100, -0.15);
    drawLog(width * 0.75, height * 0.84, 70, 0.2);
}

export function drawLoggingTruck(dc: DrawContext): void {
    const { ctx, width, height, state } = dc;
    const parallax = state.mouseX * 0.4;
    const x = width * 0.65 + parallax;
    const y = height * 0.75;

    ctx.fillStyle = '#FF9800';
    ctx.fillRect(x, y, 50, 35);
    ctx.fillStyle = '#F57C00';
    ctx.fillRect(x, y, 50, 8);

    ctx.fillStyle = '#81D4FA';
    ctx.fillRect(x + 5, y + 10, 40, 15);

    ctx.fillStyle = '#616161';
    ctx.fillRect(x + 55, y + 5, 90, 30);

    ctx.fillStyle = '#8D6E63';
    for (let i = 0; i < 3; i++) {
        ctx.beginPath();
        ctx.ellipse(x + 75 + i * 25, y + 5, 12, 20, Math.PI / 2, 0, Math.PI * 2);
        ctx.fill();
    }

    ctx.fillStyle = '#212121';
    ctx.beginPath();
    ctx.arc(x + 15, y + 38, 12, 0, Math.PI * 2);
    ctx.arc(x + 80, y + 38, 12, 0, Math.PI * 2);
    ctx.arc(x + 120, y + 38, 12, 0, Math.PI * 2);
    ctx.fill();

    ctx.fillStyle = '#757575';
    ctx.beginPath();
    ctx.arc(x + 15, y + 38, 6, 0, Math.PI * 2);
    ctx.arc(x + 80, y + 38, 6, 0, Math.PI * 2);
    ctx.arc(x + 120, y + 38, 6, 0, Math.PI * 2);
    ctx.fill();
}

export function drawDirtPatches(dc: DrawContext): void {
    const { ctx, width, height } = dc;

    ctx.fillStyle = '#4E342E';

    const patches = [
        { x: width * 0.15, y: height * 0.83, rx: 40, ry: 15 },
        { x: width * 0.45, y: height * 0.85, rx: 60, ry: 20 },
        { x: width * 0.7, y: height * 0.82, rx: 50, ry: 18 },
    ];

    patches.forEach(p => {
        ctx.beginPath();
        ctx.ellipse(p.x, p.y, p.rx, p.ry, 0, 0, Math.PI * 2);
        ctx.fill();
    });
}

export function drawDeforestationScene(dc: DrawContext): void {
    drawDirtPatches(dc);
    drawStumps(dc);
    drawFallenLogs(dc);
    drawLoggingTruck(dc);
}
