package de.miaurizius.jgame2d.tile;

import de.miaurizius.jgame2d.core.GamePanel;
import de.miaurizius.jgame2d.core.enums.Map;

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

public class MiniMap extends TileManager {

    GamePanel panel;
    BufferedImage worldMap[];
    public boolean miniMapOn;

    public MiniMap(GamePanel panel) throws IOException {
        super(panel);
        this.panel = panel;
        createWorldMap();
    }

    public void createWorldMap() {
        worldMap = new BufferedImage[Map.values().length];
        int worldMapWidth = panel.maxWorldCol * panel.tileSize;
        int worldMapHeight = panel.maxWorldRow * panel.tileSize;

        for (int i = 0; i < Map.values().length; i++) {
            worldMap[i] = new BufferedImage(worldMapWidth, worldMapHeight, BufferedImage.TYPE_INT_ARGB);
            Graphics2D g2 = worldMap[i].createGraphics();

            int col = 0;
            int row = 0;

            while(col < panel.maxWorldCol && row < panel.maxWorldRow) {
                int tileNum = mapTileNum[i][col][row];
                int x = col * panel.tileSize;
                int y = row * panel.tileSize;
                g2.drawImage(panel.tileM.tile[tileNum].image, x, y, null);
                col++;
                if(col != panel.maxWorldCol) continue;
                col = 0;
                row++;
            }
            g2.dispose();
        }
    }

    public void drawFullMapScreen(Graphics2D g2) {
        // BACKGROUND
        g2.setColor(Color.black);
        g2.fillRect(0, 0, panel.screenWidth, panel.screenHeight);

        // DRAW MAP
        int width = 500;
        int height = 500;
        int x = panel.screenWidth / 2 - width / 2;
        int y = panel.screenHeight / 2 - height / 2;
        g2.drawImage(worldMap[panel.currentMap.getIndex()], x, y, width, height, null);

        // DRAW PLAYER
        double scale = (double)(panel.tileSize * panel.maxWorldCol)/width;
        int playerX = (int)(x + panel.player.worldX/scale);
        int playerY = (int)(y + panel.player.worldY/scale);
        int playerSize = (int)(panel.tileSize/scale);
        g2.drawImage(panel.player.down1, playerX, playerY, playerSize, playerSize, null);

        // HINT
        g2.setFont(panel.ui.font.deriveFont(32F));
        g2.setColor(Color.white);
        g2.drawString("Press M to close", 750, 550);
    }

    public void drawMiniMap(Graphics2D g2) {
        if(!miniMapOn) return;

        // DRAW MAP
        int width = 300;
        int height = 300;
        int x = panel.screenWidth - width - 50;
        int y = 50;
        g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.8f));
        g2.drawImage(worldMap[panel.currentMap.getIndex()], x, y, width, height, null);

        // DRAW PLAYER
        double scale = (double)(panel.tileSize * panel.maxWorldCol)/width;
        int playerX = (int)(x + panel.player.worldX/scale);
        int playerY = (int)(y + panel.player.worldY/scale);
        int playerSize = (int)(panel.tileSize/3);
        g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f));
        g2.drawImage(panel.player.down1, playerX-6, playerY-6, playerSize, playerSize, null);
    }

}
