package se.urmo.game.main;

import se.urmo.game.input.KeyHandler;

import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;

public class GamePanel extends JPanel {
    public static final int ORIGINAL_TILE_SIZE = 8;
    public static final int SCALE = 1;
    public static final int TILE_SIZE = ORIGINAL_TILE_SIZE * SCALE;
    public static final int MAX_SCREEN_COL =  60;
    public static final int MAX_SCREEN_ROW =  80;
    public static final int SCREEN_WIDTH = MAX_SCREEN_COL * TILE_SIZE;
    public static final int SCREEN_HEIGHT = MAX_SCREEN_ROW * TILE_SIZE;
    private final Game game;
    private final GameStateManager gameStateManager;

    public GamePanel(Game game, GameStateManager gameStateManager) {
        this.game = game;
        this.gameStateManager = gameStateManager;
        this.setPreferredSize(new Dimension(SCREEN_WIDTH, SCREEN_HEIGHT));
        this.setBackground(Color.decode("#473B78"));
        this.setDoubleBuffered(true);
        this.addKeyListener(new KeyHandler(gameStateManager));
        this.setFocusable(true);
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        // Cast to Graphics2D for better control (optional)
        Graphics2D g2d = (Graphics2D) g;

        // Fill full background with purple border
        g2d.setColor(new Color(71, 59, 120)); // #473B78
        g2d.fillRect(0, 0, getWidth(), getHeight());

        // Fill the inner map background with dark (almost black)
        int border = 16;
        g2d.setColor(new Color(20, 20, 28)); // Dark bluish-black like in the image
        g2d.fillRect(border, border, getWidth() - 2 * border, getHeight() - 2 * border);
        gameStateManager.render((Graphics2D) g);
    }
}
