package tile;

import main.GamePanel;

import javax.imageio.ImageIO;
import java.awt.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;

public class TileManager {
    GamePanel gp;
    public  Tile[] tiles = new Tile[10];
    public int[][] mapTileNum;

    public TileManager(GamePanel gp) {
        this.gp = gp;
        getTileImage();
        this.mapTileNum = loadMap("/maps/world01.txt");
    }

    public int[][] loadMap(String path) {
        int[][] map = new int[gp.maxWorldRow][gp.maxWorldCol];
        try {
            InputStream stream = GamePanel.class.getResourceAsStream(path);
            BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
            int row = 0;
            String line;
            while ((line = reader.readLine()) != null && row < gp.maxWorldRow) {
                map[row++] = Arrays.stream(line.split(" "))
                        .mapToInt(Integer::parseInt)
                        .toArray();
            }
            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    public void getTileImage() {
        try {
            tiles[0] = new Tile();
            tiles[0].image = ImageIO.read(GamePanel.class.getResourceAsStream("/tiles/grass.png"));

            tiles[1] = new Tile();
            tiles[1].image = ImageIO.read(GamePanel.class.getResourceAsStream("/tiles/wall.png"));
            tiles[1].collission = true;

            tiles[2] = new Tile();
            tiles[2].image = ImageIO.read(GamePanel.class.getResourceAsStream("/tiles/water.png"));
            tiles[2].collission = true;

            tiles[3] = new Tile();
            tiles[3].image = ImageIO.read(GamePanel.class.getResourceAsStream("/tiles/earth.png"));

            tiles[4] = new Tile();
            tiles[4].image = ImageIO.read(GamePanel.class.getResourceAsStream("/tiles/tree.png"));
            tiles[4].collission = true;

            tiles[5] = new Tile();
            tiles[5].image = ImageIO.read(GamePanel.class.getResourceAsStream("/tiles/sand.png"));

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void draw(Graphics2D g2d) {
        int worldCol = 0;
        int worldRow = 0;

        while (worldCol < gp.maxWorldCol && worldRow < gp.maxWorldRow) {
            int tileNum = mapTileNum[worldRow][worldCol];

            int worldX = worldCol * gp.tileSize;
            int worldY = worldRow * gp.tileSize;

            // Only drawImage for tiles within camera
            if (worldX + gp.tileSize> gp.player.worldX - gp.player.screenX &&
                    worldX -gp.tileSize < gp.player.worldX + gp.player.screenX &&
                    worldY + gp.tileSize > gp.player.worldY - gp.player.screenY &&
                    worldY - gp.tileSize < gp.player.worldY + gp.player.screenY) {
                int screenX = worldX - gp.player.worldX + gp.player.screenX;
                int screenY = worldY - gp.player.worldY + gp.player.screenY;
                g2d.drawImage(tiles[tileNum].image, screenX, screenY, gp.tileSize, gp.tileSize, null);
            }

            worldCol++;

            if (worldCol == gp.maxWorldCol) {
                worldCol = 0;
                worldRow++;
            }
        }
    }
}
