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

public class BodyPart : MonoBehaviour 
{
    public float m_health;
    public Vector3 m_rgbLow;
    public Vector3 m_rgbHigh;
    public Vector3 m_rgbMedium;
    public Vector3 m_rgbSelected;
    public Image m_image;
    public Victim m_victim;
    public bool m_isSelected;

	// Use this for initialization
	void Start () 
    {
	    
	}
	
	// Update is called once per frame
	void Update () 
    {
        if(m_isSelected)
        {
            m_image.color = new Color(m_rgbSelected.x / 255, m_rgbSelected.y / 255, m_rgbSelected.z / 255);
        }
        else
        {
            if (m_health >= 100)
            {
                m_image.color = Color.white;
            }
            else if (m_health >= 75)
            {
                m_image.color = new Color(m_rgbLow.x / 255, m_rgbLow.y / 255, m_rgbLow.z / 255);
            }
            else if (m_health >= 50)
            {
                m_image.color = new Color(m_rgbMedium.x / 255, m_rgbMedium.y / 255, m_rgbMedium.z / 255);
            }
            else if (m_health >= 25)
            {
                m_image.color = new Color(m_rgbHigh.x / 255, m_rgbHigh.y / 255, m_rgbHigh.z / 255);
            }
            else if (m_health >= 0)
            {
                m_image.color = new Color(m_rgbHigh.x / 255, m_rgbHigh.y / 255, m_rgbHigh.z / 255);
            }
            else
            {
                //Debug.Log("BodyPart - " + transform.name + " is dead!");
            }
        }
        
	}

    public void OnClick()
    {
        if(m_health > 0 && m_victim.m_manager.m_activeTool != null)
        {
            float dmg = m_victim.m_manager.m_activeTool.m_damage;
            m_health -= dmg;
            if(m_health < 0)
            {
                dmg += m_health;
            }
            m_victim.OnDamage(dmg);
        }
        m_isSelected = false;
    }

    public void OnMouseEnter()
    {
        //Debug.Log(transform.name + " Part - pointer entered");
        m_isSelected = true;
    }

    public void OnMouseExit()
    {
        //Debug.Log(transform.name + " Part - pointer exited");
        m_isSelected = false;
    }
}
