package de.frajul.endlessroll.entities;

import de.frajul.endlessroll.data.Vector;
import de.frajul.endlessroll.levels.MoveComponent;
import de.frajul.endlessroll.levels.ObstacleData;
import de.frajul.endlessroll.levels.worlds.World;
import de.frajul.endlessroll.main.GameLog;
import de.frajul.endlessroll.main.game.Camera;

/**
 * Created by Julian on 20.11.2015.
 */
public class Obstacle extends Entity {

    private boolean deadly;
    private boolean floating;
    private boolean moving;
    private final float GRID_SQUARE_SIZE = 0.1f;
    private Vector gridSize;
    private MoveComponent moveComponent;

    private float moveProgress;
    private float moveDirection;

    public Obstacle(World world, ObstacleData data, float terrainEdge) {
        super(world.getObstacleTexture(), new Vector(data.getX(), data.getY()), data.getWidth(),
                data.getHeight());
        this.deadly = data.isDeadly();
        this.floating = data.isFloating();
        this.moving = data.isMoving();
        this.moveComponent = data.getMoveComponent();
        if (!floating)
            super.setToTerrain(terrainEdge);
        if (moveComponent != null && moving) {
            moveComponent.init(super.getPosition());
            moveProgress = moveComponent.getStartOffset();
            moveDirection = moveComponent.getDirection();
        }
        gridSize = calcGridSize();
    }

    public boolean isPlayerOverTrigger(Player player) {
        if (moveComponent == null)
            return false;
        return moveComponent.getLeftmostPosition() - super.getWidth() / 2 - moveComponent.getTriggerDistance() <= player.getRightEdge();
    }

    private Vector calcGridSize() {
        int gridWidth = (int) (width / GRID_SQUARE_SIZE);
        int gridHeight = (int) (height / GRID_SQUARE_SIZE);
        gridWidth = Math.max(gridWidth, 2);
        gridHeight = Math.max(gridHeight, 2);
        return new Vector(gridWidth, gridHeight);
    }

    public void moveWithMoveComponent(float frameTime, Player player) {
        if (moveComponent.isHasPlayerXSpeed()) {
            moveComponent.calcSpeedForPlayerSpeed(player);
        }
        float distance = frameTime * moveComponent.getSpeed() * moveComponent.TRANSITION_VALUE;
        moveProgress += moveDirection * distance / moveComponent.getLength();
        if (moveProgress < 0) {
            moveProgress *= -1;
            moveDirection *= -1;
        }
        if (moveProgress > 1) {
            moveProgress = 2 - moveProgress;
            moveDirection *= -1;
        }
        Vector oldPosition = new Vector(super.getPosition());
        super.setPosition(moveComponent.getPositionForProgress(moveProgress));
        Vector newPosition = new Vector(super.getPosition());
        Vector move = newPosition.translate(oldPosition.negate());
        move.mul(1 / frameTime);
        super.setMovement(move);
    }

    public boolean isMoving() {
        return moving;
    }

    public boolean isDeadly() {
        return deadly;
    }

    public boolean isFloating() {
        return floating;
    }

    public Vector getGridSize() {
        return gridSize;
    }

}
