using UnityEngine;
using System.Collections;
using System;

public class SliderManager : MonoBehaviour {

    public float barIncreaseSpeed; // only for positive increases
    public float startLevel;

    [Header("Sorting Ritual Parameters")]
    public float barIncreaseForSorting;
    public float barInertiaForSorting;

    [Header("Marching Ritual Parameters")]
    public float barIncreaseForMarching;
    public float barInertiaForMarching;

    [Header("Whirling Ritual Parameters")]
    public float barIncreaseForWhirling;
    public float barInertiaForWhirling;

    [Header("Genuflexion Ritual Parameters")]
    public float barIncreaseForGenuflexion;
    public float barInertiaForGenuflexion;

    [Header("Torch Ritual Parameters")]
    public float barIncreaseForTorch;
    public float barInertiaForTorch;

    [Header("Suspicion Parameters")]
    public float barIncreaseForSuspect;
    public float barInertiaForSuspect;

    [Header("Wrong Accusation Parameters")]
    public float barIncreaseForWrongAccusation;
    public float barInertiaForWrongAccusation;

    private UnityEngine.UI.Image bloodBar;
    private float targetLevel;
    private float inertia;

	// Use this for initialization
	void Start () {
        targetLevel = startLevel;                
        inertia = 0;
    }
	
    private UnityEngine.UI.Image GetBloodBar()
    {
        if (bloodBar == null)
        {
            GameObject go = GameObject.Find("Blood Bar");
            if (go != null)
            {
                bloodBar = go.GetComponent<UnityEngine.UI.Image>();
                if (bloodBar != null)
                {
                    bloodBar.fillAmount = startLevel;
                }
            }
        }
        return bloodBar;
    }

    // Update is called once per frame
    void Update () {
        targetLevel += inertia;
        if (targetLevel < 0)
        {
            targetLevel = 0.001f;
        }
        RefreshBar();
    }

    public void doActionOne()
    {
        Set(barIncreaseForSorting, barInertiaForSorting);
    }

    public void doActionTwo()
    {
        Set(barIncreaseForMarching, barInertiaForMarching);
    }

    public void doActionThree()
    {
        Set(barIncreaseForWhirling, barInertiaForWhirling);
    }

    public void doActionFour()
    {
        Set(barIncreaseForGenuflexion, barInertiaForGenuflexion);
    }

    public void doActionFive()
    {
        Set(barIncreaseForTorch, barInertiaForTorch);
    }

    public void Suspect()
    {
        Set(barIncreaseForSuspect, barInertiaForSuspect);
    }

    public void AccuseInnocent()
    {
        Set(barIncreaseForWrongAccusation, barInertiaForWrongAccusation);
    }

    private void Set(float increase, float inertia)
    {
        this.inertia = inertia;
        this.targetLevel += increase;
    }

    private void RefreshBar()
    {
        if (GetBloodBar() != null)
        {
            if (targetLevel > bloodBar.fillAmount)
            {
                GetBloodBar().fillAmount += barIncreaseSpeed;
            }
            else
            {
                GetBloodBar().fillAmount = targetLevel;
            }
            if (GetBloodBar().fillAmount >= 1)
            {
                GameMgr.instance.toWin("complete");
            }
        }
    }

    internal bool IsOverMinThreshold()
    {
        if (GetBloodBar() != null)
        {
            return GetBloodBar().fillAmount > 0.05f;
        }
        return false;
    }

    internal bool IsOverAccusationThreshold()
    {
        if (GetBloodBar() != null)
        {
            return GetBloodBar().fillAmount > Math.Abs(barIncreaseForWrongAccusation);
        }
        return false;
    }
}
