package entity;

import main.GamePanel;
import main.KeyHandler;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Objects;

public class Player extends Entity {
    private final GamePanel gamePanel;
    private final KeyHandler keyHandler;

    public final int screenX;
    public final int screenY;

    int hasKey = 0;

    public Player(GamePanel gamePanel, KeyHandler keyHandler) {
        this.gamePanel = gamePanel;
        this.keyHandler = keyHandler;

        screenX = gamePanel.screenWidth / 2 - (gamePanel.tileSize/2);
        screenY = gamePanel.screenHeight / 2 - (gamePanel.tileSize/2);

        solidArea = new Rectangle(8,16,32,32);
        solidAreaDefaultX = solidArea.x;
        solidAreaDefaultY = solidArea.y;

        setDefaultValues();
        getPlayerImage();

    }

    public void setDefaultValues(){
        worldX = gamePanel.tileSize * 22;
        worldY = gamePanel.tileSize * 22;
        speed = 1;
        direction = "down";
    }

    public void getPlayerImage(){
        try {
            up1 = ImageIO.read(Objects.requireNonNull(getClass().getResourceAsStream("/player/boy_up_1.png")));
            up2 = ImageIO.read(Objects.requireNonNull(getClass().getResourceAsStream("/player/boy_up_2.png")));
            down1 = ImageIO.read(Objects.requireNonNull(getClass().getResourceAsStream("/player/boy_down_1.png")));
            down2 = ImageIO.read(Objects.requireNonNull(getClass().getResourceAsStream("/player/boy_down_2.png")));
            left1 = ImageIO.read(Objects.requireNonNull(getClass().getResourceAsStream("/player/boy_left_1.png")));
            left2 = ImageIO.read(Objects.requireNonNull(getClass().getResourceAsStream("/player/boy_left_2.png")));
            right1 = ImageIO.read(Objects.requireNonNull(getClass().getResourceAsStream("/player/boy_right_1.png")));
            right2 = ImageIO.read(Objects.requireNonNull(getClass().getResourceAsStream("/player/boy_right_2.png")));
        }catch (IOException e){
            e.printStackTrace();
        }
    }

    public void update(){
        if(keyHandler.upPressed || keyHandler.downPressed
        || keyHandler.leftPressed || keyHandler.rightPressed) {
            if (keyHandler.upPressed) {
                direction = "up";
            }
            if (keyHandler.downPressed) {
                direction = "down";
            }
            if (keyHandler.leftPressed) {
                direction = "left";
            }
            if (keyHandler.rightPressed) {
                direction = "right";
            }

            //Check tile collision
            collisionOn = false;
            gamePanel.collisionChecker.checkTile(gamePanel.player);
            int objectIndex = gamePanel.collisionChecker.checkObject(this,true);
            pickUpObject(objectIndex);
            if(!collisionOn){
                switch (direction){
                    case "up" -> worldY -= speed;
                    case "down" -> worldY += speed;
                    case "left" -> worldX -= speed;
                    case "right" -> worldX += speed;
                }
            }

            spriteCount++;
            if (spriteCount == 30) {
                if (spriteNum == 1) {
                    spriteNum = 2;
                } else if (spriteNum == 2) {
                    spriteNum = 1;
                }
                spriteCount = 0;
            }
        }
    }

    public void pickUpObject(int objectIndex){
        if(objectIndex != 999){
            String objectName = gamePanel.obj[objectIndex].name;
            switch (objectName){
                case "Key" -> {
                    gamePanel.playSE(1);
                    hasKey++;
                    gamePanel.obj[objectIndex] = null;
                }
                case "Door" -> {
                    if(hasKey > 0 ){
                        gamePanel.playSE(3);
                        gamePanel.obj[objectIndex] = null;
                        hasKey--;
                    }
                }
                case "Boots" -> {
                    gamePanel.playSE(2);
                    speed += 1;
                    gamePanel.obj[objectIndex] = null;
                }
            }
        }
    }

    public void draw(Graphics2D g2d){
        BufferedImage image = null;
        switch(direction){
            case "up" -> image = spriteNum == 1 ? up1 : up2;
            case "down" -> image = spriteNum == 1 ? down1 : down2;
            case "left" -> image = spriteNum == 1 ? left1 : left2;
            case "right" -> image = spriteNum == 1 ? right1 : right2;
        }

        g2d.drawImage(image, screenX, screenY, gamePanel.tileSize, gamePanel.tileSize, null);
    }

    private void drawRectangel(Graphics2D g2d) {
        int num = (worldX - 24) / gamePanel.tileSize;
        int num2 = (worldY - 24) / gamePanel.tileSize;
        g2d.drawRect(screenX - gamePanel.tileSize, screenY - gamePanel.tileSize, 48, 48);
        g2d.drawString("" + num + "," + num2, screenX - gamePanel.tileSize + 10, screenY - gamePanel.tileSize + 20);
    }
}
