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

public class PlayerHealth : MonoBehaviour {

    public AudioClip expSound;
    public int maxHealth;
    private int health;

    public GameObject healthBar;
    public int playerID;

    public Sprite defaultFace;
    public Sprite damagedFace;
    public Sprite lowFace;

    public Image charFace;
    public GameObject explosion;
    private bool isDead = false;

    private void Start()
    {
        health = maxHealth;
        if(playerID==0)
        {
            healthBar = InputManager.INPUTMANAGER.player1HealthBar;
        }
        else
        {
            healthBar = InputManager.INPUTMANAGER.player2HealthBar;
        }
    }
    public void ModifyHealth(int a, bool b=false)
    {
        if(a<0)
        {
            if (b == false)
            {
                CameraShake.Shake(0.1f, 0.1f);
            }
        }
        health += a;
        UpdateHealthBar();
        if (health>=maxHealth)
        {
            health = maxHealth;
            
        }
        if(health<=0&&isDead==false)
        {
            isDead = true;
            health = 0;
            //TODO: Put some explosions here
            Destroy(charFace);
            AudioSource.PlayClipAtPoint(expSound, this.transform.position);
            GameObject newExp = Instantiate(explosion,
                new Vector3(this.transform.position.x + Random.Range(-0.5f, 0.5f), this.transform.position.y + Random.Range(-0.5f, 0.5f), -0.15f),
                Quaternion.identity);
            GameObject newExp2 = Instantiate(explosion,
                new Vector3(this.transform.position.x + Random.Range(-0.5f, 0.5f), this.transform.position.y + Random.Range(-0.5f, 0.5f), -0.15f),
                Quaternion.identity);
            Destroy(newExp, 0.25f);
            Destroy(newExp2, 0.25f);
            Destroy(this.gameObject,0.5f);
            gameObject.GetComponent<PlayerMovement>().enabled = false;
            gameObject.GetComponent<PlayerShooting>().enabled = false;
        }
    }

    public void UpdateHealthBar()
    {
        if(healthBar!=null)
        {
            StopCoroutine(LowerRedHealth(null,0));
            float x = (float)health / (float)maxHealth;
            Image[] imgArray = healthBar.GetComponentsInChildren<Image>();
            imgArray[3].fillAmount = x;
            StartCoroutine(LowerRedHealth(imgArray[2],x));
        } 
    }



    public IEnumerator LowerRedHealth(Image img, float x)
    {
        charFace.sprite = damagedFace;
        yield return new WaitForSeconds(0.1f);
        float val;
        float start = img.fillAmount;
        for (float i = 0; i < 1; i+=0.1f)
        {
            img.fillAmount = Mathf.Lerp(start, x, i);
            yield return new WaitForEndOfFrame();
        }
        img.fillAmount = x;
        if(health<=30)
        {
            if(charFace!=null)
                charFace.sprite = lowFace;
        }
        else
        {
            charFace.sprite = defaultFace;
        }
    }
}
