using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour {

    Vector3 velocity;
    public float speed = 5f;
    public float jumpStrenght = 5f;
    public float throwStrenght = 20f;
    private float jumpPressTimer = 0f;
    public float jumpPressMax = .2f;
    public bool jumpReady = true;
    public bool canMove = true;
    private float recoveryTimer = 0f;
    public float recoveryTime = 2.5f;
    Rigidbody2D RB;
    private BoxCollider2D BoxCollider;
    public float skindWidth = .015f;

    public KeyCode Move_left;
    public KeyCode Move_right;
    public KeyCode Jump;
    public KeyCode Use;

    public bool doesCarry = false;
    private Transform carriedObject;
    private Vector2 carriedObjectSize;
    private bool facingRight = true;

    public PlayerController anotherPlayer;

    // Use this for initialization
    void Start () {
        RB = GetComponent<Rigidbody2D>();
        BoxCollider = GetComponent<BoxCollider2D>();
	}
	
	// Update is called once per frame
	void Update () {
        velocity.x = 0;
        velocity.y = 0;
        if (canMove)
        {
            if (Input.GetKey(Move_left))
            {
                velocity.x = -speed;
                facingRight = false;
            }
            if (Input.GetKey(Move_right))
            {
                velocity.x = speed;
                facingRight = true;
            }
            if (Input.GetKey(Jump) && jumpReady)
            {
                if (jumpPressTimer < jumpPressMax)
                {
                    velocity.y += jumpStrenght;
                    jumpPressTimer += Time.deltaTime;
                }
            }
            if (Input.GetKeyUp(Jump))
            {
                jumpPressTimer = 0f;
                jumpReady = false;
            }
            if (Input.GetKeyDown(Use))
            {
                UseKeyUsed();
            }
        } else
        {
            recoveryTimer += Time.deltaTime;
            GetComponent<SpriteRenderer>().color = new Color32(255, 255, 255, 150);
        }

        if(recoveryTimer > recoveryTime)
        {
            canMove = true;
            GetComponent<SpriteRenderer>().color = new Color32(255, 255, 255, 255);
        }

        if (CheckVerticalCollision())
        {
            jumpReady = true;
        }


        transform.position += velocity * Time.deltaTime;

        if (doesCarry)
        {
            if(carriedObject != null)
            {
                carriedObject.transform.position = new Vector2(transform.position.x, transform.position.y + carriedObjectSize.y + .15f);
            }
        }
    }

    private bool CheckVerticalCollision()
    {
        Bounds bounds = BoxCollider.bounds;

        float boundsSize = bounds.size.x / 5;
        for (int i = 0; i < 5; i++)
        {
            Vector2 origin = new Vector2(bounds.min.x + boundsSize * i, bounds.min.y + skindWidth);
            RaycastHit2D hit = Physics2D.Raycast(origin, -Vector2.up, skindWidth);
            if (hit)
            {
                if(hit.distance <= skindWidth){
                    return true;
                }
            }
        }
        return false;
    }

    public void Hurt()
    {
        canMove = false;
        recoveryTimer = 0f;
        if(carriedObject != null)
            Drop(carriedObject);
    }

    public void UseKeyUsed()
    {
        if (doesCarry){
            if(carriedObject != null)
            {
                if (carriedObject.GetComponent<BombScript>())
                {
                    Throw(carriedObject);
                    return;
                }

                if (carriedObject.GetComponent<Pillar>())
                {
                    Drop(carriedObject);
                    return;
                }
            } else
            {
                doesCarry = false;
            }
        }
        else {
            Collider2D[] colliders = Physics2D.OverlapCircleAll(transform.position, .85f);
            foreach (Collider2D col in colliders)
            {
                if (col.transform.GetComponent<BombScript>())
                {
                    PickUp(col.transform);
                }

                if (col.transform.GetComponent<Pillar>())
                {
                    PickUp(col.transform);
                }
            }
        }
    }

    public void PickUp(Transform Object)
    {
        if (anotherPlayer.carriedObject == Object)
        {
            anotherPlayer.doesCarry = false;
            anotherPlayer.carriedObject = null;
        }
        doesCarry = true;
        carriedObject = Object;
        if (carriedObject.GetComponent<CircleCollider2D>())
            carriedObjectSize = carriedObject.GetComponent<CircleCollider2D>().bounds.size;
        if (carriedObject.GetComponent<BoxCollider2D>())
            carriedObjectSize = carriedObject.GetComponent<BoxCollider2D>().bounds.size;
    }

    public void Throw(Transform Object)
    {
        doesCarry = false;
        Vector2 direction = new Vector2(.5f,.5f);
        if (!facingRight)
            direction.x *= -1;

        if (Object.GetComponent<BombScript>())
        {
            Object.GetComponent<BombScript>().active = true;
            Object.GetComponent<BombScript>().timer = 0f;
        }

        Object.GetComponent<Rigidbody2D>().AddForce(direction * throwStrenght, ForceMode2D.Impulse);
        carriedObject = null;
    }


    public void Drop(Transform Object)
    {
        doesCarry = false;

        if (Object.GetComponent<BombScript>())
        {
            Object.GetComponent<BombScript>().active = true;
            Object.GetComponent<BombScript>().timer = 0f;
        }
        
        carriedObject = null;
    }
}
