package com.mygdx.game.Character;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.math.Vector3;
import com.mygdx.game.Dimension.Chunks;
import com.mygdx.game.Restrictions;

import java.util.HashMap;

import static com.mygdx.game.Main.cam;

public class InputController  implements Restrictions {

    private Player player;
    int i = 0;
    private final HashMap<String, Animation<com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion>> animations;

    public InputController(Player player) {
        animations = new HashMap<>();
        animations.put("Up", new Animation<>(1 / 4f, player.textureAtlas.findRegions(player.getSpriteName() + "_" + "Up")));
        animations.put("UpRight", new Animation<>(1 / 4f, player.textureAtlas.findRegions(player.getSpriteName() + "_" + "UpRight")));
        animations.put("UpLeft", new Animation<>(1 / 4f, player.textureAtlas.findRegions(player.getSpriteName() + "_" + "UpLeft")));
        animations.put("Down", new Animation<>(1 / 4f, player.textureAtlas.findRegions(player.getSpriteName() + "_" + "Down")));
        animations.put("DownRight", new Animation<>(1 / 4f, player.textureAtlas.findRegions(player.getSpriteName() + "_" + "DownRight")));
        animations.put("DownLeft", new Animation<>(1 / 4f, player.textureAtlas.findRegions(player.getSpriteName() + "_" + "DownLeft")));
        animations.put("Left", new Animation<>(1 / 4f, player.textureAtlas.findRegions(player.getSpriteName() + "_" + "Left")));
        animations.put("Right", new Animation<>(1 / 4f, player.textureAtlas.findRegions(player.getSpriteName() + "_" + "Right")));

        this.player = player;
    }

    public void handleInput() {
        i++;

        if (Gdx.input.isKeyPressed(Input.Keys.Q)) {
            cam.zoom += 5;
        }
        if (Gdx.input.isKeyPressed(Input.Keys.E)) {
            cam.zoom -= 5;
        }


        if ((Gdx.input.isButtonJustPressed(Input.Buttons.LEFT) || Gdx.input.isButtonJustPressed(Input.Buttons.RIGHT) )&& i > KEY_DELAY) {
            Vector3 mousePos = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
            cam.unproject(mousePos);
            int x = Mouse.getSelectedX(mousePos) >> TILE_SHIFT;
            int y = Mouse.getSelectedY(mousePos) >> TILE_SHIFT;
            if(Gdx.input.isButtonJustPressed(Input.Buttons.LEFT)) {
                i = 0;
                Chunks.placeBlock(x, y, "wood");
            }
            if (Gdx.input.isButtonJustPressed(Input.Buttons.RIGHT)) {
                i = 0;
                Chunks.removeBlock(x,y);
            }
        }

        if (Gdx.input.isKeyPressed(Input.Keys.W) && Gdx.input.isKeyPressed(Input.Keys.A) && i > KEY_DELAY) {
            i = 0;
            directionAnimation("UpLeft");
            Player.addX(-MOVEMENT_SPEED);
            Player.addY(MOVEMENT_SPEED);
            cam.translate(-MOVEMENT_SPEED, MOVEMENT_SPEED);
        }
        else if (Gdx.input.isKeyPressed(Input.Keys.W) && Gdx.input.isKeyPressed(Input.Keys.D) && i > KEY_DELAY) {
            i = 0;
            directionAnimation("UpRight");
            Player.addX(MOVEMENT_SPEED);
            Player.addY(MOVEMENT_SPEED);
            cam.translate(MOVEMENT_SPEED, MOVEMENT_SPEED);
        }
        else if (Gdx.input.isKeyPressed(Input.Keys.S) && Gdx.input.isKeyPressed(Input.Keys.A) && i > KEY_DELAY) {
            i = 0;
            directionAnimation("DownLeft");
            Player.addX(-MOVEMENT_SPEED);
            Player.addY(-MOVEMENT_SPEED);
            cam.translate(-MOVEMENT_SPEED, -MOVEMENT_SPEED);
        }
        else if (Gdx.input.isKeyPressed(Input.Keys.S) && Gdx.input.isKeyPressed(Input.Keys.D) && i > KEY_DELAY) {
            i = 0;
            directionAnimation("DownRight");
            Player.addX(MOVEMENT_SPEED);
            Player.addY(-MOVEMENT_SPEED);

            cam.translate(MOVEMENT_SPEED, -MOVEMENT_SPEED);
        }
        else if (Gdx.input.isKeyPressed(Input.Keys.A) && i > KEY_DELAY) {
            i = 0;
            directionAnimation("Left");
            Player.addX(-MOVEMENT_SPEED);
            cam.translate(-MOVEMENT_SPEED, 0);
        }
        else if (Gdx.input.isKeyPressed(Input.Keys.D) && i > KEY_DELAY) {
            i = 0;
            directionAnimation("Right");
            Player.addX(MOVEMENT_SPEED);
            cam.translate(MOVEMENT_SPEED, 0);
        }
        else if (Gdx.input.isKeyPressed(Input.Keys.S) && i > KEY_DELAY) {
            i = 0;
            directionAnimation("Down");
            Player.addY(-MOVEMENT_SPEED);
            cam.translate(0, -MOVEMENT_SPEED);
        }
        if (Gdx.input.isKeyPressed(Input.Keys.W) && i > KEY_DELAY) {
            i = 0;
            directionAnimation("Up");
            Player.addY(MOVEMENT_SPEED);

            cam.translate(0, MOVEMENT_SPEED);
        }
        cam.update();
    }

    public void directionAnimation(String direction) {
        player.setAnimation(animations.get(direction));
    }
}
