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

public class ActorStatus : MonoBehaviour {

    public Text HPText;
    public Text HPMaxText;    
    public int MaxHealth;
    public int Health;

    public AudioClip HurtSound;
    public AudioClip HurtSoundLarge;
    public AudioClip DeathSound;

    private JuiceDropper dropper;
    private Dictionary<string, int> lastTurnHit;

	void Start () {
        Health = MaxHealth;
        lastTurnHit = new Dictionary<string, int>();
        dropper = GetComponent<JuiceDropper>();
	}
	
	void Update () {
        if (HPText != null)
            HPText.text = "" + Health;
        if (HPMaxText != null)
            HPMaxText.text = "" + MaxHealth;
    }

    public void TakeDamage(int amount)
    {
        if (amount > 3 && HurtSoundLarge != null)
            iTween.Stab(GameController.Instance.gameObject, HurtSoundLarge, 0.01f);
        else if (amount > 0 && HurtSound != null)
            iTween.Stab(GameController.Instance.gameObject, HurtSound, 0.01f);

        SplatterBlood(amount);
        Health = Mathf.Max (0, Health-amount);
    
        if(Health <= 0 && DeathSound != null)
            iTween.Stab(GameController.Instance.gameObject, DeathSound, 0.085f);
        
        if(Health <= 0 && gameObject.name != "Player")
        {
            GameObject.Destroy(gameObject);
        }

        //if(gameObject.name == "Player")
        //{
        //    Camera.main.DOShakePosition(0.1f + amount*0.01f, .2f + 0.033f*amount);
        //    Camera.main.DOShakeRotation(0.1f + amount * 0.01f, 1 + 0.5f*amount);
        //}
        
    }

    public void TakeHeal(int amount)
    {
        Health = Mathf.Min(Health + amount, MaxHealth);
    }

    private void SplatterBlood(int damage)
    {
        if(dropper == null)
            return;

        damage = Mathf.Min(damage, Health);

        // this sets it up for one drop per point of damage
        dropper.DropInterval = dropper.DropTimeSpan * 0.98f / (float)damage;
        dropper.StartDropping();
    }

}
