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 06/07/2017.
 */

public class Wall extends Platform {

    //private static final float[] COLOUR = {0.45f, 0.29f, 0.145f, 1f};
    private static final float[] COLOUR = {0.494f, 0.561f, 0.557f, 1f};
    private float width;
    private float height;
    private float boundingRadius;
    protected int attachSide;

    private static float getRenderBoxWidth(float rotation, float width, float height, float speed){
        if((rotation - 90) % 180 == 0 && speed == 0)
            return height;
        else if(speed != 0 || rotation != 0)
            return Math.max(width, height);
        else
            return width;
    }

    private static float getRenderBoxHeight(float rotation, float width, float height, float speed){
        if((rotation - 90) % 180 == 0 && speed == 0)
            return width;
        else if(speed != 0 || rotation != 0)
            return Math.max(width, height);
        else
            return height;
    }

    public Wall(float posX, float posY, float rotation, float width, float height, float speed){
        super(posX, posY, 4, rotation, -1, getRenderBoxWidth(rotation, width * 2, height * 2, speed), getRenderBoxHeight(rotation, width * 2, height * 2, speed), width, speed);
        //float[] x = new float[] {-width/2, width/2, width/2, -width/2};
        //float[] y = new float[] {height/2, height/2, -height/2, -height/2};
        //this.model = SimpleGraphics.createPolygon(x, y);
        this.width = width;
        this.boundingRadius = (float) Math.sqrt(width*width/4 + height*height/4);
        this.height = height;
    }

    @Override
    protected float[] getColour(){
        return COLOUR;//Graphics.WHITE;
    }

    @Override
    public void render(Game game, float partialTicks){
        Graphics.color = getColour();
        Graphics.CUBE.draw(posXRender, posYRender, lastRotationRender + (rotationRender - lastRotationRender) * partialTicks, 0, 0, width, height, width);
        /*float r = lastRotation + (rotation - lastRotation) * partialTicks;
        if(r % 180 == 0) {
            paint.setColor(colour);
            SimpleGraphics.scaledRectangle(canvas, paint, posX - width/2, posY - height/2, posX + width/2, posY + height/2);
        } else if((r - 90) % 180 == 0){
            paint.setColor(colour);
            SimpleGraphics.scaledRectangle(canvas, paint, posX - height/2, posY - width/2, posX + height/2, posY + width/2);
        } else
            super.render(partialTicks);*/
    }

    @Override
    public boolean isCollidingWith(Player player, float distance, float[][] points, float collisionPartial) {
        if(distance > boundingRadius + Player.PLAYER_RADIUS)
            return false;
        Transformations.rotate(player.posX - getPosX(collisionPartial), player.posY - getPosY(collisionPartial), -getRotation(collisionPartial), updateVector2);//where the point would be at 0 wall rotation
        float[] rotatedPoint = updateVector2;
        if(rotatedPoint[0] < -width/2 + DEPTH - Player.PLAYER_RADIUS || rotatedPoint[0] >= width/2 + Player.PLAYER_RADIUS - DEPTH || rotatedPoint[1] < -height/2 - Player.PLAYER_RADIUS + DEPTH || rotatedPoint[1] >= height/2 + Player.PLAYER_RADIUS - DEPTH)
            return false;
        for(int i = 0; i < 4; i++){
            float[] point = points[i];
            Transformations.rotate((player.posX + point[0]) - getPosX(collisionPartial), (player.posY + point[1]) - getPosY(collisionPartial), -getRotation(collisionPartial), rotatedPoint);//where the point would be at 0 wall rotation
            if(rotatedPoint[0] >= -width/2 + DEPTH && rotatedPoint[0] < width/2 - DEPTH && rotatedPoint[1] >= -height/2 + DEPTH && rotatedPoint[1] < height/2 - DEPTH)
                return true;
        }
        return false;
    }

    @Override
    public void onAttach(Game game, Player p) {
        super.onAttach(game, p);
        Transformations.rotate(p.posX - posX, p.posY - posY, -rotation, updateVector2);
        float[] playerCenterRotated = updateVector2;
        /*
         * Checking which side the player attached to
         */
        if (playerCenterRotated[1] < -height / 2)
            attachSide = 3;
        else if (playerCenterRotated[1] >= height / 2)
            attachSide = 1;
        else if (playerCenterRotated[0] < -width / 2)
            attachSide = 2;
        else
            attachSide = 0;
    }

    @Override
    public void onDettach(Player p) {
        float normalAngle = attachSide * 90 + rotation;
        Transformations.rotate(DETACH_STRENGTH, 0, normalAngle, updateVector2);
        float[] vel = updateVector2;
        p.addVelocity(vel[0], vel[1]);
    }
}
