using UnityEngine;
using System.Collections;

public class Adept : MonoBehaviour {

	public Animator animator;
	public SpriteRenderer maskSr;

	public int currentDanceIndex = 0;
	public static int MAX_DANCES = 3;

	// Movement
	public CheckPoint targetCheckpoint;
	public float speed = 4f;
	private float startSpeed;
    public float speedMin = 0.5f;
    public float speedMax = 5f;

    public Transform followTransform = null;
	public Vector3 followPos = Vector3.zero;


    //seed for the spped
    float seed;

    //bool to activate/deactivate random speed following
    bool rndSpeed = true;

	void Awake(){
		startSpeed = speed;
	}

	void Start(){
		this.maskSr.sprite = RandomUtilities.UniformFromList (AdeptFactory.instance.maskSprites);

        //set seed
        seed = Random.value;

		this.animator.SetTime(Random.value);
	}

	float ELLIPSE_THRESHOLD = 1.0f;
	public float noise;
	public float newSpeed;

	void Update()
    {
		speedMax = Ellipse.instance.speed * 1000;
		speedMax = speedMax * 0.7f;
		speedMin = speedMax * 0.2f;

		if (rndSpeed) {
			noise = (Mathf.PerlinNoise (Time.time / 5 + seed, 0.0f) - 0.5f) * 2; //* (Random.value  > 0.5f ? 1f : -1f);
			//Debug.Log(noise);
			noise *= 6;
			newSpeed = speed + noise;
			newSpeed = Mathf.Clamp (newSpeed, speedMin, speedMax);
		} else
			newSpeed = speed;

        if (followTransform != null)
        {
			transform.position = Vector3.Lerp(transform.position, followTransform.position, newSpeed * Time.deltaTime);
        }
        if (followPos != Vector3.zero)
			transform.position = Vector3.Lerp (transform.position, followPos, newSpeed * Time.deltaTime); 

		switch (moveState) {
		case AdeptMoveState.TO_ELLIPSE:
			if ((transform.position - followPos).sqrMagnitude <= ELLIPSE_THRESHOLD * ELLIPSE_THRESHOLD)
				MoveInsideEllipse ();
			break;
		}
	}

	#region State switch
	public enum AdeptMoveState
	{
		TO_ELLIPSE,
		IN_ELLIPSE,
		TO_HOUSE,
	}
	public AdeptMoveState moveState;

	public void RemoveMovements(){
		followPos = Vector3.zero;
		followTransform = null;
	}

	public void GoToEllipse(){
        followPos = Ellipse.instance.GetEntryPoint(this.transform.position);
        followTransform = null;
		this.moveState = AdeptMoveState.TO_ELLIPSE;
		StopRandomSpeed ();
		Animate ("Walk");
	}
	public void MoveInsideEllipse(){
		followPos = Vector3.zero;
        followTransform = targetCheckpoint.transform;
		this.moveState = AdeptMoveState.IN_ELLIPSE;
		StartRandomSpeed ();
		Animate ("Walk");
	}
	public void GoToHouse(){
		// TODO: go to house
		//followPos = ...
		//followTransform = null;
		this.moveState = AdeptMoveState.TO_HOUSE;
		Animate ("Walk");
	}

	#endregion

	void OnMouseUpAsButton(){
		if (GameManager.instance.currentState == GameManager.GameState.InRitual) {
			NextDance ();
			MinigameManager.instance.CheckCorrectAdepts ();

		} else if (GameManager.instance.currentState == GameManager.GameState.InRitual_Sacrifice) {
			RitualManager.instance.AddSelectedAdept (this);
			RitualManager.instance.CompleteRitual_Dance ();

			this.Animate ("Awe");
		}
	}

	public void StartDancing(){
		//SwitchAnim();
		SetDance (Random.Range (0, MAX_DANCES));
	}

	public void StopDancing(){
		//maskSr.color = Color.white;
		Animate ("Idle");
	}

	void NextDance(){
		currentDanceIndex++;
		if (currentDanceIndex >= MAX_DANCES)
			currentDanceIndex = 0; 

		SetDance (currentDanceIndex);
	}

	void SetDance(int anim){
		this.currentDanceIndex = anim;

		Color col = Color.white;
		switch (currentDanceIndex) {
		case 0:
	 		col = Color.green;
			break;
		case 1:
			col = Color.red;
			break;
		case 2:
			col = Color.blue;
			break;
		}
		col += Color.white * 0.75f;
		col.a = 1;
		maskSr.color = col;

		SfxController.instance.PlaySound ("dance_" + currentDanceIndex, 0.2f);

		//Debug.Log ("SWITCH TO NEXT ANIM");
		Animate ("Dance " + currentDanceIndex); 
	}


	public void Animate(string s){
		//StartCoroutine ("AnimateDELAY", s);
		animator.Play (s, 0, Random.value); 
	}


	public void StartRandomSpeed()
	{
        rndSpeed=true;
    }

	public void StopRandomSpeed()
    {
		speed = startSpeed;
        rndSpeed =false;
    }

}
