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

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

import java.awt.*;
import java.util.Objects;

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

    /** Flag, if the player is shooting */
    private boolean shooting;
    /** Debug flag, to display X and O or the real image */
    private static final boolean SHOW_X = false;
    /** The pixel art of the character */
    private static final String SPIELFIGUR =
                    "     kkk   \n"+
                    "   BBBBk   \n"+
                    "  BBBBBB   \n"+
                    " BBBBBBBB  \n"+
                    " BBBBBBB   \n"+
                    " BBBBBBB   \n"+
                    "kBBBBBBB   \n"+
                    "kkBBBBBB   \n"+
                    "kkBBBBBBkkk\n"+
                    "kkBBBBBkkkk\n"+
                    "  BBBBBBkk \n"+
                    "  BBBBBBBk \n"+
                    "  kBBBBBBk \n"+
                    "   BBBBBkb \n"+
                    "  bbbk kbb \n"+
                    " BBBb  kBBb\n"+
                    " BBBk  kkkB";

    /**
     * Create the game character with default values
     * @param gameView  the {@link GameView} to display the character
     */
    public PlayerObject(GameView gameView){
        super(gameView);
        position.setTo(GameView.WIDTH/2, GameView.HEIGHT/1.5);
        shooting = false;
        size = 3;
        width = (int) size * 11;
        height = (int) size * 17;
        speedInPixel = 5;

        this.hitBox.width = (int) (width - (2*size));
        this.hitBox.height = (int) (height - (1*size));
    }

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

    @Override
    public void reactToCollision(CollidableGameObject otherObject) {

    }

    @Override
    public void addToCanvas(){
        if(SHOW_X) {
            gameView.addTextToCanvas(shooting ? "O" : "X", position.x, position.y, 50, Color.RED, rotation);
        }else{
            gameView.addBlockImageToCanvas(SPIELFIGUR, position.x, position.y, size, rotation);
        }
        shooting = false;
    }

    @Override
    public void updateStatus() {

    }

    /** Moves the character to the left */
    public void left(){
        position.left(speedInPixel);
    }
    /** Moves the character to the right */
    public void right(){
        position.right(speedInPixel);
    }
    /** Moves the character upwards */
    public void up(){
        position.up(speedInPixel);
    }
    /** Moves the character downwards */
    public void down(){
        position.down(speedInPixel);
    }
    /** Lets the character shoot */
    public void shoot(){
        shooting = true;
        gamePlayManager.shootHarpoon(this.position);
    }

    @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);
    }
}
