using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class Player : MonoBehaviour {
    public GameObject projectile;

    public float speed = 5f;
    public float shootCD = 0.2f;
    public bool isPermanentProjectile = false;

    public Vector2 upperRightLimit = Vector2.one;
    public Vector2 lowerLeftLimit = -Vector2.one;

    public Image weaponsBar;

    public bool directControl = false;

    float currShootCD = 0;

    GameObject permanent;

    const float _WEAPONS_BAR_MAX_HEIGHT_ = 44;
	// Use this for initialization
	void Start () {
        currShootCD = shootCD;
        weaponsBar = GameObject.Find("WeaponsCDimg").GetComponent<Image>();
        UpdateWeaponsCDbar();

        directControl = (PlayerPrefs.GetInt("control", 0) == 1);
    }
	
	// Update is called once per frame
	void Update () {
        Control();
        Shoot();

        // UPDATE CDs
        currShootCD -= Time.deltaTime;
        UpdateWeaponsCDbar();
    }

    void Control()
    {
        //MOVEMENT
        //this.transform.position += speed * (new Vector3(xMovement(),yMovement(),0).normalized) * Time.deltaTime;
        if (directControl)
            this.transform.position += speed * ((xMovement() * this.transform.right + yMovement() * this.transform.up)).normalized * Time.deltaTime;
        else
            this.transform.position += speed * (new Vector3(xMovement(), yMovement(), 0).normalized) * Time.deltaTime;

        //ROTATION
        Vector3 cursorPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        cursorPos.z = 0;
        this.transform.rotation = Quaternion.LookRotation(this.transform.forward, cursorPos - this.transform.position);
    }

    void Shoot()
    {
        if (Input.GetAxis("Fire1") > 0 && currShootCD <= 0)
        {
            if (!isPermanentProjectile)
            {
                GameObject newProjectile = (GameObject)Instantiate(projectile, this.transform.position, this.transform.rotation);
                newProjectile.name = "PermanentWeapon";
                currShootCD = shootCD;
            }
            else if (isPermanentProjectile && permanent == null)
            {
                permanent = (GameObject)Instantiate(projectile, this.transform.position, this.transform.rotation);
                permanent.transform.SetParent(this.transform);
            }

        }
        else if (Input.GetAxis("Fire1") <= 0 && isPermanentProjectile)
        {
            Destroy(permanent);
            permanent = null;
        }

        if (Input.GetAxis("Fire2") > 0 && FindObjectOfType<GameManager>().isShiftEnabled()) FindObjectOfType<GameManager>().ActivateShiftDrive();
    }

    float xMovement()
    {
        float value = Input.GetAxis("Horizontal");

        if (value > 0 && this.transform.position.x >= upperRightLimit.x) value = 0;
        else if (value < 0 && this.transform.position.x <= lowerLeftLimit.x) value = 0;

        return value;
    }

    float yMovement()
    {
        float value = Input.GetAxis("Vertical");

        if (value > 0 && this.transform.position.y >= upperRightLimit.y) value = 0;
        else if (value < 0 && this.transform.position.y <= lowerLeftLimit.y) value = 0;

        return value;
    }

    void UpdateWeaponsCDbar()
    {
        weaponsBar.rectTransform.sizeDelta = new Vector2(weaponsBar.rectTransform.rect.width, _WEAPONS_BAR_MAX_HEIGHT_ * Mathf.Clamp(1 - currShootCD/shootCD,0,1));
    }

    void OnDestroy()
    {
        if (permanent != null)
        {
            Destroy(permanent);
            permanent = null;
        }
    }
}
