using UnityEngine;
using System.Collections;

public class PlayerScript : MonoBehaviour
{

    //general
    public int id;
    private int direction;
    private ItemScript item;
    private PlayerVisuals visuals;
    private WinController winController;
    bool dontmove = false;

    //moving
    private float moveSpeed = 7;
    Vector3 velocity;
    float gravity;

    //jumps
    float jumpHeight = 2.8f;
    float timeToJumpApex = .4f;
    float jumpVelocity;
    bool onGround = false;//change

    void Start()
    {
        gravity = -(2 * jumpHeight) / Mathf.Pow(timeToJumpApex, 2);
        jumpVelocity = Mathf.Abs(gravity) * timeToJumpApex;
        visuals = GetComponent<PlayerVisuals>();
        winController = FindObjectOfType<WinController>();
    }

    // Update is called once per frame
    void Update()
    {
        if (dontmove) return;
        handleMovement();
        handleActions();
    }

    void handleActions()
    {
        if (item != null)
        {
            float drop, use;
            drop = Input.GetAxis("Drop_" + id);
            if (drop == 1)
            {
                visuals.drop();
                item.drop(direction);
                this.item = null;
                return;
            }
            use = Input.GetAxis("Use_" + id);
            if (use == 1)
            {
                if (item.isReady())
                {
                    visuals.useItem();
                    if (!item.use(direction))
                    {
                        this.item = null;
                    }
                }
            }
        }
        else
        {
            float use = Input.GetAxis("Use_" + id);
            if (use == 1)
            {
                visuals.useItem();
            }
        }
    }

    void handleMovement()
    {
        float h_axis, v_axis;
        h_axis = Input.GetAxis("Horizontal_" + id);
        if (h_axis != 0)
        {
            direction = h_axis < 0 ? -1 : 1;
            if (direction == transform.localScale.x)
                transform.localScale = new Vector3(direction * -1, 1, 1);
        }
        v_axis = Input.GetAxis("Jump_" + id);
        if (v_axis != 0 && onGround)
        {
            velocity.y = jumpVelocity;
            onGround = false;
        }
        velocity.x = h_axis * moveSpeed;
        if (velocity.x != 0)
        {
            visuals.move(true);
        }
        else
        {
            visuals.move(false);
        }
        velocity.y += gravity * Time.deltaTime;
        if (!onGround)
        {
            visuals.jump(true);
        }
        else
        {
            visuals.jump(false);
        }
        move(velocity * Time.deltaTime);
    }

    void move(Vector3 velocity)
    {
        if (transform.position.y <= 0 && velocity.y <= 0)
        {
            velocity.y = 0;
            transform.position = new Vector3(transform.position.x, 0, 0);
            onGround = true;
        }
        transform.Translate(velocity);
    }


    void OnTriggerEnter2D(Collider2D other)
    {
        if(other.gameObject.tag == "item")
        {
            int owner = other.gameObject.GetComponent<ItemScript>().getOwner();
            if (owner != 0 && owner != this.id)
            {
                if (item)
                {
                    item.dropDead();
                    visuals.drop();
                    this.item = null;
                }
                Camera.main.GetComponent<GameScript>().KillAndRespawn(gameObject);
            }
            //sth bad
        }
        if( other.gameObject.tag == "king")
        {
            if( (gameObject.transform.position.x > 0 && id == 1) ||
                (gameObject.transform.position.x < 0 && id == 2))
            {
                dontmove = true;
                winController.playerWins(id);
            }
        }
    }

    void OnTriggerStay2D(Collider2D other)
    {
        if (this.item == null && other.gameObject.tag == "item" && Input.GetAxis("Take_"+id) != 0)
        {
            //GameObject.Destroy(other.gameObject);
            item = other.gameObject.GetComponent<ItemScript>();
            item.setOwner(this.id);
            visuals.collect(item);
        }
    }
}