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

public enum TortureState
{
    FadeTitle,
    FadeBlack,
    Choose,
    Torture,
    Feedback,
    Release,
    Pause,
    GameOver,
    End
}

public class TortureManager : MonoBehaviour 
{
    public TortureState m_state = TortureState.FadeTitle;
    public Text m_textTitle;
    public Image m_blackFade;
    public float m_fadeOutTimeTitle;
    public AnimationCurve m_fadeTitleCurve;
    public float m_curFadeOutTimeTitle;
    public int m_curFadeTitleDir = 1;
    public float m_fadeOutTimeBlack;
    public float m_curFadeOutTimeBlack;
    public AnimationCurve m_fadeBlackCurve;
    public Canvas m_canvasBg;
    public Canvas m_canvasFrames;
    public Text m_textExecutioner;
    public Text m_textVictim;
    public Victim m_curVictim;
    public Executioner m_executioner;
    public float m_curTortureTime;
    public float m_curFeedbackTime;
    public float m_releaseTime;
    public float m_curReleaseTime;
    [Range(1,100)]
    public float m_victimHealthMin;
    [Range(1,100)]
    public float m_victimHealthMax;
    public float m_executionerBaseInc;
    [Range(1, 100)]
    public float m_executionerRatio;
    public TortureTool m_activeToolType;
    public ButtonTool m_activeTool;

	void Start () 
    {
        m_curFadeOutTimeTitle = 0;
        m_curFadeOutTimeBlack = 0;
        m_blackFade.gameObject.SetActive(true);
        NextVictim();
	}
	
	// Update is called once per frame
	void Update () 
    {
	    switch(m_state)
        {
            case TortureState.FadeTitle:
            {
                m_curFadeOutTimeTitle += Time.deltaTime * m_curFadeTitleDir;
                float fraction = Mathf.Abs(m_curFadeOutTimeTitle) / m_fadeOutTimeTitle;
                float curFade = m_fadeTitleCurve.Evaluate(fraction);
                m_textTitle.color = new Color(255, 255, 255, curFade);
                if(m_curFadeOutTimeTitle >= m_fadeOutTimeTitle)
                {
                    m_curFadeTitleDir = -1;
                }
                else if(m_curFadeOutTimeTitle < 0)
                {
                    SetState(TortureState.FadeBlack);
                }
                
                break;
            }
            case TortureState.FadeBlack:
            {
                m_curFadeOutTimeBlack += Time.deltaTime;
                float fraction = m_curFadeOutTimeBlack / m_fadeOutTimeBlack;
                float curFade = m_fadeBlackCurve.Evaluate(fraction);
                m_blackFade.color = new Color(0, 0, 0, curFade);
                if (m_curFadeOutTimeBlack >= m_fadeOutTimeBlack)
                {
                    m_curFadeOutTimeBlack = 0;
                    SetState(TortureState.Choose);
                }
                break;
            }
            case TortureState.Choose:
            {
                break;
            }
            case TortureState.Torture:
            {
                m_curTortureTime -= Time.deltaTime;
                if (m_curTortureTime <= 0)
                {
                    SetState(TortureState.Feedback);
                }
                break;
            }
            case TortureState.Feedback:
            {
                
                break;
            }
            case TortureState.Release:
            {
                m_curReleaseTime += Time.deltaTime;
                if (m_curReleaseTime >= m_releaseTime)
                {
                    NextVictim();
                }
                break;
            }
            case TortureState.Pause:
            {
                break;
            }
            case TortureState.GameOver:
            {
                break;
            }
            case TortureState.End:
            {
                break;
            }
            default:
            {
                break;
            }
        }
	}

    public void OnFeedbackEnd(Feedback _f)
    {
        Debug.Log("********END OF FEEDBACK !");
        if(_f.m_type == FeedbackType.VictimNext)
        {
            NextVictim();
        }
        if(_f.m_type == FeedbackType.VictimDamage)
        {
            SetState(TortureState.Choose);
        }
    }

    public void OnVictimDead()
    {
        m_executioner.OnNewVictim(false);
        //NextVictim();
        //SetState(TortureState.Choose);
    }

    public void OnRelease()
    {
        m_executioner.OnNewVictim(true);
        NextVictim();
        SetState(TortureState.Choose);
    }

    public void NextVictim()
    {
        //SetActiveTool();
        m_curVictim.CreateRandomVictom();
        //SetState(TortureState.Choose);
    }

    public void SetActiveTool(ButtonTool _nTool = null)
    {
        if (_nTool)
        {
            //Debug.Log("Manager: setting active tool to " + _nTool.m_tool);
            m_activeTool = _nTool;
            RefreshTools(m_activeTool.m_tool);
        }
        else
        {
            m_activeTool = null;
            RefreshTools(TortureTool.None);
        }
    }

    public void RefreshTools(TortureTool _selectedTool)
    {
        GameObject[] tools = GameObject.FindGameObjectsWithTag("Tool");

        foreach (GameObject t in tools)
        {
            ButtonTool bT = t.GetComponent<ButtonTool>();
            if (bT.m_tool != _selectedTool)
            {
                bT.m_isSelected = false;
            }
        }
    }

    public void SetState(TortureState _nState)
    {
        m_state = _nState;
        switch (m_state)
        {
            case TortureState.FadeBlack:
            {
                m_curVictim.gameObject.SetActive(true);
                m_executioner.gameObject.SetActive(true);
                break;
            }
            case TortureState.Choose:
                {
                    
                    break;
                }
            case TortureState.Torture:
                {
                    //SetActiveTool();
                    break;
                }
            case TortureState.Feedback:
                {
                    break;
                }
            case TortureState.Pause:
                {
                    break;
                }
            case TortureState.GameOver:
                {
                    break;
                }
            case TortureState.End:
                {
                    break;
                }
            default:
                {
                    break;
                }
        }
    }
}
