package de.miaurizius.jgame2d.environment;

import de.miaurizius.jgame2d.core.GamePanel;

import java.awt.*;
import java.awt.image.BufferedImage;

public class Lighting {

    GamePanel panel;
    BufferedImage darknessFilter;
    public int dayCount;
    public DayState dayState = DayState.DAY;
    public float filterAlpha;

    public Lighting(GamePanel panel) {
        this.panel = panel;
        setLightSource();
    }

    public void draw(Graphics2D g2) {
        g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, filterAlpha));
        g2.drawImage(darknessFilter, 0, 0, null);
        g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1F));

        // DEBUG
        if(panel.keyH.debug) {
            String s;
            switch (dayState) {
                case DAY -> s = "DAY";
                case DUSK -> s = "DUSK";
                case NIGHT -> s = "NIGHT";
                case DAWN -> s = "DAWN";
                default -> s = "UNKNOWN";
            }
            g2.setColor(Color.white);
            g2.setFont(g2.getFont().deriveFont(50f));
            g2.drawString(s + " " + dayCount, 700, 500);
        }
    }
    public void update() {
        if(panel.player.lightUpdated) {
            setLightSource();
            panel.player.lightUpdated = false;
        }

        switch (dayState) {
            case DAY -> {
                dayCount++;
                if (dayCount > 36000) { //10 minutes
                    dayState = DayState.DUSK;
                    dayCount = 0;
                }
            }
            case DUSK -> {
                filterAlpha += 0.001f;
                if (filterAlpha > 1f) {
                    filterAlpha = 1f;
                    dayState = DayState.NIGHT;
                }
            }
            case NIGHT -> {
                dayCount++;
                if (dayCount > 36000) { //10 minutes
                    dayState = DayState.DAWN;
                    dayCount = 0;
                }
            }
            case DAWN -> {
                filterAlpha -= 0.001f;
                if (filterAlpha < 0f) {
                    filterAlpha = 0f;
                    dayState = DayState.DAY;
                }
            }
        }
    }
    public void resetDay() {
        dayState = DayState.DAY;
        filterAlpha = 0f;
        dayCount = 0;
    }

    // ...
    public void setLightSource() {
        darknessFilter = new BufferedImage(panel.screenWidth, panel.screenHeight, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2 = darknessFilter.createGraphics();

        int centerX = panel.player.screenX + panel.tileSize/2;
        int centerY = panel.player.screenY + panel.tileSize/2;

        RadialGradientPaint gPaint = getRadialGradientPaint(centerX, centerY, 0F,0F, 0.1F);
        g2.setPaint(gPaint);

        g2.fillRect(0, 0, panel.screenWidth, panel.screenHeight);
        g2.dispose();
    }

    // UTILITY
    private RadialGradientPaint getRadialGradientPaint(int centerX, int centerY, float r, float g, float b) {
        Color[] color = new Color[] {
                new Color(r, g, b, 0.1f),
                new Color(r, g, b, 0.42f),
                new Color(r, g, b, 0.52f),
                new Color(r, g, b, 0.61f),
                new Color(r, g, b, 0.69f),
                new Color(r, g, b, 0.76f),
                new Color(r, g, b, 0.82f),
                new Color(r, g, b, 0.87f),
                new Color(r, g, b, 0.91f),
                new Color(r, g, b, 0.94f),
                new Color(r, g, b, 0.96f),
                new Color(r, g, b, 0.97f)
        };

        final int lim = 3;
        if(panel.player.currentLight == null) {
            for(int i = 0; i < lim; i++) {
                color[i] = color[lim];
            }
        }

        float[] fraction = new float[] {
                0f, 0.4f, 0.5f, 0.6f, 0.65f, 0.7f, 0.75f, 0.8f, 0.85f, 0.9f, 0.95f, 1f
        };
        return new RadialGradientPaint(centerX, centerY, (panel.player.currentLight == null ? 75 : panel.player.currentLight.lightRadius), fraction, color);
    }

    public enum DayState {
        DAY,
        DUSK,
        NIGHT,
        DAWN,
    }

}
