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

public class Player : MonoBehaviour {

    public float moveSpeed = 2;

    public GameObject graphics;

    public GameObject baseWeaponPrefab;

    Weapon currentWeapon;

    CharacterController character;

    Vector3 inputMove;

    public float maxHealth = 100f;

    float health;

    float invulTime;

    float speedBoostTime;

    Animator anim;

    AudioSource hitAudio;

    public float healthRatio
    {
        get
        {
            return health / maxHealth;
        }
    }

    public int ammo
    {
        get
        {
            if (currentWeapon.ammo == float.PositiveInfinity)
                return -1;
            return Mathf.RoundToInt(currentWeapon.ammo);
        }
    }

	// Use this for initialization
	void Start () {
        character = GetComponent<CharacterController>();
        anim = GetComponentInChildren<Animator>();
        hitAudio = GetComponent<AudioSource>();
        health = maxHealth;
        SwitchWeapon(null);
	}
	
	// Update is called once per frame
	void Update () {
        Plane plane = new Plane(Vector3.up, transform.position);
        Ray cameraRay = Camera.main.ScreenPointToRay(Input.mousePosition);
        float enter;
        plane.Raycast(cameraRay, out enter);
        Vector3 targetPoint = cameraRay.GetPoint(enter) - transform.position;

        inputMove = Vector3.ClampMagnitude(new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")), 1);
        float angle = Mathf.Atan2(targetPoint.x, targetPoint.z) * Mathf.Rad2Deg;
        transform.rotation = Quaternion.Euler(0, angle, 0);

        if (invulTime > 0)
        {
            invulTime -= Time.deltaTime;
            if (invulTime <= 0)
                graphics.SetActive(true);
            else
            {
                graphics.SetActive(Mathf.Floor(invulTime * 10) % 2 == 0);
            }
        }

        if (speedBoostTime > 0)
            speedBoostTime -= Time.deltaTime;

        if (anim.gameObject.activeSelf)
            anim.SetBool("walking", inputMove.sqrMagnitude > 0.1f);
    }

    void FixedUpdate()
    {
        float boost = 1;
        if (speedBoostTime > 0)
            boost = 2;
        character.SimpleMove(inputMove * moveSpeed * boost);
    }

    public void TakeDamage(float damage)
    {
        if (invulTime > 0)
            return;

        hitAudio.Play();
        health -= damage;
        if (health <= 0)
        {
            GameObject.FindGameObjectWithTag("GameController").GetComponent<GlobalManager>().EndGame();
            Camera.main.GetComponent<AudioListener>().enabled = true;
            Time.timeScale = 0;

            GameObject.Destroy(gameObject);
            return;
        }

        invulTime = 1f;
    }

    public void SwitchWeapon(GameObject weapon)
    {
        if (weapon == null)
        {
            SwitchWeapon(baseWeaponPrefab);
            return;
        }

        if (currentWeapon != null)
            GameObject.Destroy(currentWeapon.gameObject);
        GameObject newWeaponGo = GameObject.Instantiate(weapon, transform);
        currentWeapon = newWeaponGo.GetComponent<Weapon>();
        currentWeapon.Init(graphics.transform);
    }

    public void Heal(float amount)
    {
        health = Mathf.Min(maxHealth, health + maxHealth * amount);
    }

    public void SpeedBoost(float time)
    {
        speedBoostTime = time;
    }

    public void Invul(float time)
    {
        invulTime = time;
    }
}
