package de.thdeg.greiner.superpangworld.graphics.moveable;

import de.thdeg.greiner.superpangworld.gameview.GameView;
import de.thdeg.greiner.superpangworld.graphics.base.CollidableGameObject;

import java.util.Objects;

/**
 * The game character controlled by the player.
 */
public class PlayerObject extends CollidableGameObject implements Cloneable {

    /** Flag, if the player is shooting */
    private boolean shooting;
    /** Flag, if the player is moving right */
    private boolean movingRight;
    /** Flag, if the player is moving left */
    private boolean movingLeft;
    /** Flag, to rotate between standing and walking when moving around */
    private boolean movingStanding;

    /**
     * Create the game character with default values.
     *
     * @param gameView  the {@link GameView} to display the character
     */
    public PlayerObject(GameView gameView){
        super(gameView);
        setPosition(GameView.WIDTH/2.0, 360);
        shooting = false;
        size = 2;
        width = (int) (26 * size);
        height = (int) (41 * size);
        speedInPixel = 2;

        this.hitBox.width = width - 20;
        this.hitBox.height = height - 10;

        movingRight = false;
        movingLeft = false;

        movingStanding = true;
    }

    @Override
    protected void updateHitBoxPosition() {
        hitBox.x = (int) (position.x + 10);
        hitBox.y = (int) (position.y + 10);
    }

    @Override
    public void reactToCollision(CollidableGameObject otherObject) {

    }

    @Override
    public void addToCanvas(){
        if(!gameView.timerExpired("shootHarpoon","PlayerObject")){
            gameView.addImageToCanvas("behind.png",position.x,position.y,2,0);
            return;
        }

        if(gameView.timerExpired("movingStanding","PlayerObject")){
            movingStanding = !movingStanding;
            gameView.setTimer("movingStanding","PlayerObject",100);
        }
        if(movingLeft && !movingRight){
            if(movingStanding){
                gameView.addImageToCanvas("left_standing.png",position.x,position.y,2,0);
            }else{
                gameView.addImageToCanvas("left_walking.png",position.x,position.y,2,0);
            }
        }else if(movingRight && !movingLeft){
            if(movingStanding){
                gameView.addImageToCanvas("right_standing.png",position.x,position.y,2,0);
            }else{
                gameView.addImageToCanvas("right_walking.png",position.x,position.y,2,0);
            }
        }else{
            gameView.addImageToCanvas("behind.png",position.x,position.y,2,0);
            movingStanding = true;
        }
        movingLeft = false;
        movingRight = false;
    }

    @Override
    public void updateStatus() {
    }

    /**
     * Moves the character to the left
     */
    public void left(){

        if(gameView.timerExpired("shootHarpoon","PlayerObject") && position.x>8) {
            position.left(Math.min(speedInPixel, position.x-8));
            movingLeft = true;
        }
    }
    /**
     * Moves the character to the right
     */
    public void right(){
        if(gameView.timerExpired("shootHarpoon","PlayerObject") && position.x < GameView.WIDTH - width - 8){
            position.right(Math.min(speedInPixel, (GameView.WIDTH-position.x)));
            movingRight = true;
        }
    }
    /**
     * Lets the character shoot
     */
    public void shoot(){
        if(gamePlayManager.shootHarpoon(this.position)){
            shooting = true;
            gameView.setTimer("shootHarpoon","PlayerObject", 100);
        }
    }

    @Override
    public PlayerObject clone() {
        return (PlayerObject) super.clone();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        if (!super.equals(o)) return false;
        PlayerObject playerObject = (PlayerObject) o;
        return shooting == playerObject.shooting;
    }

    @Override
    public int hashCode() {
        return Objects.hash(super.hashCode(), shooting);
    }

}
