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

public class PriestManager : MonoBehaviour
{
	public float priestRunSpeed;

	void Reset()
	{
		priestRunSpeed = 3.0f;
	}

    // Use this for initialization
    void Start()
    {
        CurrentMode = Mode.Idle;
        TempleBottom = GetComponent<RectTransform>().anchoredPosition;
    }

    // Update is called once per frame
    void Update()
    {
        Vector2 v = GetComponent<RectTransform>().anchoredPosition;

        switch (CurrentMode)
        {
            case Mode.GettingSacrifice:
                if (v == TargetPosition)
                {
                    CurrentMode = Mode.TakingSacrificeToTemple;
                    CurrentSacrifice.transform.SetParent(GetComponent<RectTransform>());
                    break;
                }
                else
                {
                    // Move one unit closer to the sacrifice
                    Vector2 Direction = TargetPosition - v;
					if (Direction.magnitude > priestRunSpeed)
                    {
						v += Direction.normalized * priestRunSpeed;
                        GetComponent<RectTransform>().anchoredPosition = v;
                    }
                    else
                    {
                        GetComponent<RectTransform>().anchoredPosition = TargetPosition;
                    }
                }
                break;

            case Mode.TakingSacrificeToTemple:
                if (v == TempleBottom)
                {
                    CurrentMode = Mode.ClimbingTemple;
                    break;
                }
                else
                {
                    // Move one unit closer to the sacrifice
                    Vector2 Direction = TempleBottom - v;
					if (Direction.magnitude > priestRunSpeed)
                    {
						v += Direction.normalized * priestRunSpeed;
                        GetComponent<RectTransform>().anchoredPosition = v;
                    }
                    else
                    {
                        GetComponent<RectTransform>().anchoredPosition = TempleBottom;
                    }
                }
                break;

            case Mode.ClimbingTemple:
                if (v.y > TempleHeight)
                {
					CurrentSacrifice.GetComponent<Image>().enabled = false;
					CurrentSacrifice.transform.SetParent(transform.parent);
                    CurrentMode = Mode.DescendingTemple;
                }
                else
                {
					v.y += priestRunSpeed;
                    GetComponent<RectTransform>().anchoredPosition = v;
                }
                break;

            case Mode.DescendingTemple:
                if (v.y < TempleBottom.y)
                {
                    CurrentMode = Mode.Idle;
					GameManager.instance.changeState(GameManager.GameState.SeeEffectsOfChoice);
                }
                else
                {
					v.y -= priestRunSpeed;
                    GetComponent<RectTransform>().anchoredPosition = v;
                }
                break;
        }
    }

    public void StartClimb()
    {
        CurrentMode = Mode.ClimbingTemple;
    }

    public void GetSacrifice(/*Pig, sheep, whatever*/)
    {
        // Get the correct sacrifice from the map
        // Hardcoded to Peppa Pig for now
        CurrentSacrifice = GameObject.Find("ResourceIcon");
        TargetPosition = CurrentSacrifice.GetComponent<RectTransform>().anchoredPosition;
        CurrentMode = Mode.GettingSacrifice;
    }

    enum Mode
    {
        Idle,
        GettingSacrifice,
        TakingSacrificeToTemple,
        ClimbingTemple,
        DescendingTemple,
    }

    Mode CurrentMode;
    Vector2 TargetPosition;
    Vector2 TempleBottom;
    GameObject CurrentSacrifice;
    float TempleHeight = 75.0f;
}
