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

public enum Sexe
{
    Masculin,
    Feminin
}

public enum TorturePart
{
    ArmLeft,
    ArmRight,
    LegLeft,
    LegRight,
    Torso,
    Head
}

public class Victim : MonoBehaviour 
{
    public Sexe m_sexe;
    public TortureManager m_manager;
    public GameObject[] m_curPortraitGo;
    public Text m_text;
    public float m_health;
    public float m_curHealth;
    public bool m_needFeedback;
    public Feedback m_curFeedback;
    public Feedback m_feedback1;
    public Feedback m_feedback2;
    public Feedback m_feedback3;
    public Feedback m_feedback4;
    public Feedback m_feedbackNext;
    [Range(1,4)]
    public int m_victimeNb;
    public BodyPart m_armLeft;
    public float m_armLeftHealth;
    public BodyPart m_armRight;
    public float m_armRightHealth;
    public BodyPart m_legLeft;
    public float m_legLeftHealth;
    public BodyPart m_legRight;
    public float m_legRightHealth;
    public BodyPart m_torso;
    public float m_torsoHealth;
    public BodyPart m_head;
    public float m_headHealth;
    public GameObject m_portrait1Go;
    public GameObject[] m_portraits1;
    public GameObject m_portrait2Go;
    public GameObject[] m_portraits2;
    public GameObject m_portrait3Go;
    public GameObject[] m_portraits3;
    public GameObject m_portrait4Go;
    public GameObject[] m_portraits4;
    public int m_curPortrait;
    public VictimBlink m_blink;
    public VictimNext m_nextDead;
    public VictimNext m_nextSaved;

	void Start () 
    {
        m_manager = GameObject.FindGameObjectWithTag("TortureManager").GetComponent<TortureManager>();
        m_blink.SetActive(false);
        m_nextDead.Activate(false);
	}
	
	// Update is called once per frame
	void Update () 
    {
        if (m_curHealth / m_health >= 0.95f)
        {
            m_curPortraitGo[0].SetActive(true);
            m_curPortraitGo[1].SetActive(false);
            m_curPortraitGo[2].SetActive(false);
            m_curPortraitGo[3].SetActive(false);
            m_curPortrait = 0;
            m_curFeedback = m_feedback1;
        }
        else if (m_curHealth / m_health >= 0.6f)
        {
            m_curPortraitGo[0].SetActive(false);
            m_curPortraitGo[1].SetActive(true);
            m_curPortraitGo[2].SetActive(false);
            m_curPortraitGo[3].SetActive(false);
            m_curPortrait = 1;
            m_curFeedback = m_feedback2;
        }
        else if (m_curHealth / m_health >= 0.3f)
        {
            m_curPortraitGo[0].SetActive(false);
            m_curPortraitGo[1].SetActive(false);
            m_curPortraitGo[2].SetActive(true);
            m_curPortraitGo[3].SetActive(false);
            m_curPortrait = 2;
            m_curFeedback = m_feedback3;
        }
        else if (m_curHealth / m_health >= 0)
        {
            m_curPortraitGo[0].SetActive(false);
            m_curPortraitGo[1].SetActive(false);
            m_curPortraitGo[2].SetActive(false);
            m_curPortraitGo[3].SetActive(true);
            m_curFeedback = m_feedback4;
        }
        else
        {
            Debug.Log("Victim - I'm dead mofo!");
            m_nextDead.Activate(true);
            m_curFeedback = m_feedbackNext;
            m_manager.OnVictimDead();
        }

        if (m_needFeedback == true)
        {
            m_needFeedback = false;
            Debug.Log("Generating Feedback " + m_curFeedback.m_type);
            m_curFeedback.GenerateFeedback();
            m_curFeedback.Activate(true);
        }
        m_legLeftHealth = m_legLeft.m_health;
        m_legRightHealth = m_legRight.m_health;
        m_armLeftHealth = m_armLeft.m_health;
        m_armRightHealth = m_armRight.m_health;
        m_torsoHealth = m_torso.m_health;
        m_headHealth = m_head.m_health;
	}

    public void OnDamage(float _dmg)
    {
        Debug.Log("Victim takes " + _dmg + " damages!");
        m_curHealth -= _dmg;
        m_blink.SetActive(true);
        m_needFeedback = true;
        m_manager.m_executioner.m_sanity -= _dmg * m_manager.m_executionerRatio / 100;
        m_manager.SetState(TortureState.Torture);
    }

    public void OnRelease()
    {
        Debug.Log("Victim Released : )");
        m_needFeedback = true;
        m_manager.m_executioner.m_sanity += m_manager.m_executionerBaseInc * (1 + m_curHealth/m_health);
        m_manager.SetState(TortureState.Torture);
    }

    public void CreateRandomVictom()
    {
        int rSexe = Random.Range((int)0, (int)1);
        if(rSexe == 0)
        {
            m_sexe = Sexe.Masculin;
        }
        else
        {
            m_sexe = Sexe.Feminin;
        }

        m_victimeNb = Random.Range((int)1, (int)5);

        m_health = Random.Range(m_manager.m_victimHealthMin, m_manager.m_victimHealthMax);
        m_curHealth = m_health;

        switch(m_victimeNb)
        {
            case 2:
            {
                m_portrait1Go.SetActive(false);
                m_portraits1[0].SetActive(true);
                m_portraits1[1].SetActive(false);
                m_portraits1[2].SetActive(false);
                m_portraits1[3].SetActive(false);
                m_curPortraitGo = m_portraits2;
                m_portrait2Go.SetActive(true);
                m_portraits2[0].SetActive(true);
                m_portraits2[1].SetActive(false);
                m_portraits2[2].SetActive(false);
                m_portraits2[3].SetActive(false);
                m_portrait3Go.SetActive(false);
                m_portraits3[0].SetActive(true);
                m_portraits3[1].SetActive(false);
                m_portraits3[2].SetActive(false);
                m_portraits3[3].SetActive(false);
                m_portrait4Go.SetActive(false);
                m_portraits4[0].SetActive(true);
                m_portraits4[1].SetActive(false);
                m_portraits4[2].SetActive(false);
                m_portraits4[3].SetActive(false);
                break;
            }
            case 3:
            {
                m_portrait1Go.SetActive(false);
                m_portraits1[0].SetActive(true);
                m_portraits1[1].SetActive(false);
                m_portraits1[2].SetActive(false);
                m_portraits1[3].SetActive(false);
                m_portrait2Go.SetActive(false);
                m_portraits2[0].SetActive(true);
                m_portraits2[1].SetActive(false);
                m_portraits2[2].SetActive(false);
                m_portraits2[3].SetActive(false);
                m_curPortraitGo = m_portraits3;
                m_portrait3Go.SetActive(true);
                m_portraits3[0].SetActive(true);
                m_portraits3[1].SetActive(false);
                m_portraits3[2].SetActive(false);
                m_portraits3[3].SetActive(false);
                m_portrait4Go.SetActive(false);
                m_portraits4[0].SetActive(true);
                m_portraits4[1].SetActive(false);
                m_portraits4[2].SetActive(false);
                m_portraits4[3].SetActive(false);
                break;
            }
            case 4:
            {
                m_portrait1Go.SetActive(false);
                m_portraits1[0].SetActive(true);
                m_portraits1[1].SetActive(false);
                m_portraits1[2].SetActive(false);
                m_portraits1[3].SetActive(false);
                m_portrait2Go.SetActive(false);
                m_portraits2[0].SetActive(true);
                m_portraits2[1].SetActive(false);
                m_portraits2[2].SetActive(false);
                m_portraits2[3].SetActive(false);
                m_portrait3Go.SetActive(false);
                m_portraits3[0].SetActive(true);
                m_portraits3[1].SetActive(false);
                m_portraits3[2].SetActive(false);
                m_portraits3[3].SetActive(false);
                m_curPortraitGo = m_portraits4;
                m_portrait4Go.SetActive(true);
                m_portraits4[0].SetActive(true);
                m_portraits4[1].SetActive(false);
                m_portraits4[2].SetActive(false);
                m_portraits4[3].SetActive(false);
                break;
            }
            default:
            {
                m_curPortraitGo = m_portraits1;
                m_portrait1Go.SetActive(true);
                m_portraits1[0].SetActive(true);
                m_portraits1[1].SetActive(false);
                m_portraits1[2].SetActive(false);
                m_portraits1[3].SetActive(false);
                m_portrait2Go.SetActive(false);
                m_portraits2[0].SetActive(true);
                m_portraits2[1].SetActive(false);
                m_portraits2[2].SetActive(false);
                m_portraits2[3].SetActive(false);
                m_portrait3Go.SetActive(false);
                m_portraits3[0].SetActive(true);
                m_portraits3[1].SetActive(false);
                m_portraits3[2].SetActive(false);
                m_portraits3[3].SetActive(false);
                m_portrait4Go.SetActive(false);
                m_portraits4[0].SetActive(true);
                m_portraits4[1].SetActive(false);
                m_portraits4[2].SetActive(false);
                m_portraits4[3].SetActive(false);
                break;
            }
        }
        ResetBody();
    }

    public void ResetBody()
    {
        m_legLeft.m_health = 100;
        m_legLeft.m_isSelected = false;
        m_legRight.m_health = 100;
        m_legRight.m_isSelected = false;
        m_armRight.m_health = 100;
        m_armRight.m_isSelected = false;
        m_armLeft.m_health = 100;
        m_armLeft.m_isSelected = false;
        m_torso.m_health = 100;
        m_torso.m_isSelected = false;
        m_head.m_health = 100;
        m_head.m_isSelected = false;
    }
}
