package de.miaurizius.jgame2d.entity.npc;

import de.miaurizius.jgame2d.core.GamePanel;
import de.miaurizius.jgame2d.core.enums.Direction;
import de.miaurizius.jgame2d.core.enums.EntityType;
import de.miaurizius.jgame2d.core.enums.Map;
import de.miaurizius.jgame2d.entity.Entity;
import de.miaurizius.jgame2d.entity.obstacle.IronDoorObj;
import de.miaurizius.jgame2d.tile.interactive.InteractiveTile;
import de.miaurizius.jgame2d.tile.interactive.MetalPlateIT;

import java.util.ArrayList;
import java.util.Random;

public class BigRockNPC extends Entity {

    public BigRockNPC(GamePanel panel) {
        super(panel);
        type = EntityType.NPC;
        name = "bigrock";

        direction = Direction.DOWN;
        speed = 4;
        getImage();
        setDialogue();

        dialogueSet = -1;

        solidArea.x = 2;
        solidArea.y = 6;
        solidAreaDefaultX = solidArea.x;
        solidAreaDefaultY = solidArea.y;
        solidArea.width = 44;
        solidArea.height = 40;
    }

    @Override
    public void update() {
    }

    @Override
    public void move(Direction direction) {
        this.direction = direction;
        checkCollision();
        if(collisionOn) return;
        switch (direction) {
            case UP -> worldY -= speed;
            case DOWN -> worldY += speed;
            case LEFT -> worldX -= speed;
            case RIGHT -> worldX += speed;
        }
        detectPlate();
    }

    @Override
    public void speak() {
        super.facePlayer();
        super.startDialogue(this,super.dialogueSet);
        dialogueSet++;
        if(dialogue[dialogueSet][0] == null) dialogueSet = 0;
    }

    public void detectPlate() {
        ArrayList<InteractiveTile> plateList = new ArrayList<>();
        ArrayList<Entity> rockList = new ArrayList<>();

        for(int i = 0; i < panel.iTile[Map.DUNGEON_FIRST_FLOOR.getIndex()].length; i++) {
            if(panel.iTile[panel.currentMap.getIndex()][i] != null && panel.iTile[panel.currentMap.getIndex()][i] instanceof MetalPlateIT) {
                plateList.add(panel.iTile[panel.currentMap.getIndex()][i]);
            }
        }

        for(int i = 0; i < panel.npc[Map.DUNGEON_FIRST_FLOOR.getIndex()].length; i++) {
            if(panel.npc[panel.currentMap.getIndex()][i] != null && panel.npc[panel.currentMap.getIndex()][i] instanceof BigRockNPC) {
                rockList.add(panel.npc[panel.currentMap.getIndex()][i]);
            }
        }

        int count = 0;

        for (int i = 0; i < plateList.size(); i++) {
            int dx = Math.abs(worldX - plateList.get(i).worldX);
            int dy = Math.abs(worldY - plateList.get(i).worldY);
            if (Math.max(dx, dy) < 8 ) {
                if(linkedEntity == null) {
                    linkedEntity = plateList.get(i);
                    panel.playSE(3);
                }
            } else if(linkedEntity == plateList.get(i)) linkedEntity = null;
        }

        for(Entity rock : rockList) if(rock.linkedEntity != null) count++;

        if(count == rockList.size()) {
            for(int i = 0; i < panel.obj[Map.DUNGEON_FIRST_FLOOR.getIndex()].length; i++) {
                if(panel.obj[panel.currentMap.getIndex()][i] != null && panel.obj[panel.currentMap.getIndex()][i] instanceof IronDoorObj) {
                    panel.obj[panel.currentMap.getIndex()][i] = null;
                    panel.playSE(21);
                }
            }
        }
    }

    // SETTING THINGS UP
    private void getImage()  {
        up1 = initEntitySprites("npc/bigrock");
        up2 = initEntitySprites("npc/bigrock");
        down1 = initEntitySprites("npc/bigrock");
        down2 = initEntitySprites("npc/bigrock");
        left1 = initEntitySprites("npc/bigrock");
        left2 = initEntitySprites("npc/bigrock");
        right1 = initEntitySprites("npc/bigrock");
        right2 = initEntitySprites("npc/bigrock");
    }
    public void setDialogue() {
        dialogue[0][0] = "It's a giant rock.";
    }

}
