package xaero.rotatenjump.game.object;

import xaero.rotatenjump.game.Game;
import xaero.rotatenjump.game.graphics.Graphics;
import xaero.rotatenjump.misc.Transformations;

/**
 * Created by xaero on 15/07/2017.
 */

public class Katana extends Entity {
    public static final float KATANA_LENGTH = 100f;//ingame units
    public static final float KATANA_SCALE = KATANA_LENGTH / 10.0f;

    private float angleX = -90;
    private float lastAngleXRender = angleX;
    private float angleXRender;
    private float angleY = 90;
    private float lastAngleYRender = angleY;
    private float angleYRender;
    private float angleZ;
    private int timer = 0;
    private int attackState = 0;
    private int attackDirection = -1;

    public Katana(float posX, float posY, float rotationZ, int timer) {
        super(posX, posY, KATANA_LENGTH * 2, KATANA_LENGTH * 2, KATANA_LENGTH * 2);
        this.angleZ = rotationZ;
        this.timer = timer;
    }

    @Override
    public void preUpdate() {
        super.preUpdate();
    }

    @Override
    public void update(Game game) {
        super.update(game);
        float toPlayerX = game.getPlayer().getPosX(0) - posX;
        float toPlayerY = game.getPlayer().getPosY(0) - posY;
        if(!game.getPlayer().isDead() && attackDirection * attackDirection == 1 && attackState == 5) {//mid point
            Transformations.rotate(toPlayerX, toPlayerY, -angleZ, updateVector2);
            float rotatedX = updateVector2[0];
            float rotatedY = updateVector2[1];
            //System.out.println(Arrays.toString(updateVector2));
            if(Math.abs(rotatedX) <= Player.PLAYER_RADIUS && rotatedY > 0 && rotatedY <= KATANA_LENGTH) {
                game.forceEnd = true;
                game.getPlayer().setDead(true);
                game.getOthers().add(new NinjaStarCut(game.getPlayer().getPosX(0), game.getPlayer().getPosY(0), game.getPlayer().velocityX/2, game.getPlayer().velocityY/2, 3, game.getPlayer().getRotation(), 0));
                game.getOthers().add(new NinjaStarCut(game.getPlayer().getPosX(0), game.getPlayer().getPosY(0), game.getPlayer().velocityX/2, game.getPlayer().velocityY/2, -3, game.getPlayer().getRotation(), 1));
                shake = 30;
            }
        }
        if (attackState > 0 && attackState < 9) {
            if(attackDirection * attackDirection == 1)
                angleX = attackDirection * (90 - 180 * Transformations.getSinAnimation(attackState, 8));
            else
                angleY = attackDirection / 2 * (90 - 180 * Transformations.getSinAnimation(attackState, 8));
            attackState++;
        } else if(attackState == 9){
            if(attackDirection * attackDirection == 1)
                attackDirection *= -2;
            else {
                attackDirection /= 2;
                timer = 15;
            }
            attackState = 0;
        } else if(timer == 0) {
            attackState = 1;
        } else
            timer--;
    }

    @Override
    public void refresh() {
        super.refresh();
        lastAngleXRender = angleXRender;
        lastAngleYRender = angleYRender;
        angleXRender = angleX;
        angleYRender = angleY;
    }

    @Override
    public void render(Game game, float partialTicks){
        Graphics.color = Graphics.WHITE;
        float shakeX = 0;
        float shakeY = 0;
        if(shake > 0) {
            shakeX = (float) (Math.random() - 0.5);
            shakeY = (float) (Math.random() - 0.5);
            shake--;
        }
        Graphics.modelsLoadedFromFiles[4].draw(posXRender + shakeX, posYRender + shakeY, angleZ, lastAngleYRender + (angleYRender - lastAngleYRender) * partialTicks, lastAngleXRender + (angleXRender - lastAngleXRender) * partialTicks, KATANA_SCALE, KATANA_SCALE, KATANA_SCALE);
    }

    public float getAngleX() {
        return angleX;
    }

    @Override
    public boolean isCollidingWith(Player player, float distance, float[][] points, float collisionPartial) {
        if(distance <= Player.PLAYER_RADIUS)
            return true;
        return super.isCollidingWith(player, distance, points, collisionPartial);
    }


    @Override
    public void collide(Player player, float[][] points) {
        super.collide(player, points);
        float toPlayerX = player.getPosX(0) - posX;
        float toPlayerY = player.getPosY(0) - posY;
        Transformations.normalize(toPlayerX, toPlayerY, updateVector2);
        float multCos = updateVector2[1] * player.velocityY + updateVector2[0] * player.velocityX;//velocity in the toPlayer direction
        if(multCos < 0) {
            player.velocityX -= updateVector2[0] * multCos * 1.5f;//cancel all velocity in
            player.velocityY -= updateVector2[1] * multCos * 1.5f;
            goThrough = 2;
        }
        shake = 30;
    }
}
