package com.jan.state;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
import com.jan.Collision.CollisionEvent;
import com.jan.entity.Actor;
import com.jan.game.Display;
import com.jan.game.Game;

public class Platformer extends Game{
    
    private Actor player;
    private boolean isColiding;

    private float maxSpeed = 8;
    private float accelRate = 50f;
    private float jumpForce = 30f;

    private float coolDown = 0.3f;
    private float currCooldown = 0;

    public Platformer(){
        super();
    }

    public void start() {
        player = (Actor)this.currScene.getEntity("Player");    
    }

    @Override
    public void update() {
        updateInput();
        updateSpeed();
        updateCamera();
    }

    private void updateInput(){
        if(Gdx.input.isKeyPressed(Input.Keys.NUMPAD_6))
            player.getColider().getBody().applyForceToCenter(new Vector2(accelRate, 0), true);
        if(Gdx.input.isKeyPressed(Input.Keys.NUMPAD_4))
            player.getColider().getBody().applyForceToCenter(new Vector2(-accelRate, 0), true);
        if(Gdx.input.isKeyPressed(Input.Keys.A) && isColiding == true && currCooldown <= 0){
            float playerSpeed = player.getColider().getBody().getLinearVelocity().x;
            player.getColider().getBody().setLinearVelocity(playerSpeed, jumpForce);
        }
    }

    private void updateCamera(){
        if(player.getX() < 420){
            Display.getCamera().position.set(13.644001f ,8.388437f, 0);
            return;
        }
        System.out.println(Display.getCamera().position);
        float offsetHeight = (Display.getCamera().viewportHeight /2);
        float offsetWidth = (Display.getCamera().viewportWidth /2);
        Vector3 pos = new Vector3(player.getColider().getBody().getWorldCenter(), 0);
        pos.add(offsetWidth, offsetHeight, 0);
        pos.sub(Display.getCamera().viewportWidth /2, (player.getHeight() /32)*2.6f, 0) ;
        Display.getCamera().position.set(pos);
    }

    private void updateSpeed(){
        Vector2 speed = player.getColider().getBody().getLinearVelocity();
        speed = new Vector2(speed.x, speed.y);
        if(player.getColider().getBody().getPosition().x <= 0){
            System.out.println("Fuck");
            float angle = player.getColider().getBody().getAngle();
            player.getColider().getBody().setTransform(87.0f /32, 128.22997f /32, angle);
        }
        if(speed.x > maxSpeed )
            player.getColider().getBody().setLinearVelocity(maxSpeed, speed.y);
        if( speed.x < maxSpeed*-1)
            player.getColider().getBody().setLinearVelocity(-maxSpeed, speed.y);

        if(currCooldown > 0)
            currCooldown -= Gdx.graphics.getDeltaTime();

        
    }

    public void regulairCollision(CollisionEvent event){
        Vector2 distance = event.getBodyB().getPosition();
        distance.sub(event.getContact().getFixtureA().getBody().getPosition());
        if(distance.y < 1.4f){
            isColiding = false;
            return;
        }
        switch(event.getState()){
            case BEGIN: isColiding = true; currCooldown = coolDown; break;
            case END: isColiding = false; break;
        }
    }
    
}
