using System;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

[System.Serializable]
public class ShepAnim
{

    public string Name;
    public List<Sprite> Sprites;
    public float FrameDelay;

}

public enum EShepAnimBehavior
{

    loop_idle = 0,
    play_once,
    play_once_and_hide,
    play_once_and_destroy,

}

public class TM_SpriteAnimator : MonoBehaviour 
{
    [HideInInspector]
    public ShepAnim currentAnimation;

    public SpriteRenderer spriteRenderer;

    public EShepAnimBehavior behavior;
    public List<ShepAnim> animationList;
    public string StartAnimation;

    [Header("Audio")]
    public AudioClip clip_animStart;
    public AudioClip clip_animEnd;
    private AudioSource audioSource;

    float fTimer;
    int iFrameIndex;

    private bool bPlaying;


    //Delegates on animation changes
    private List<Action<GameObject>> onAnimStartActions;
    private List<Action<GameObject>> onAnimEndActions; 

    void Awake()
    {
        spriteRenderer = GetComponent<SpriteRenderer>();
        audioSource = GetComponent<AudioSource>();
    }

	// Use this for initialization
	void Start ()
	{
        fTimer = 0;
        if (currentAnimation != null)
            fTimer = currentAnimation.FrameDelay;

	    if (!string.IsNullOrEmpty(StartAnimation) && animationList != null)
	    {
	        for(int t=0; t < animationList.Count; t++)
                if( animationList[t] != null &&
                    animationList[t].Name == StartAnimation)
                    SetAnimation(animationList[t]);
	    }

	}

    public void SetAnimation( ShepAnim newAnim )
    {
        currentAnimation = newAnim;
        PlayAnim();
    }

    public void SetAnimation(string strAnimName)
    {
        if (!string.IsNullOrEmpty(strAnimName) && animationList != null)
        {
            for (int t = 0; t < animationList.Count; t++)
                if (animationList[t] != null &&
                    animationList[t].Name == strAnimName)
                    SetAnimation(animationList[t]);
        }
    }


    public void PlayAnim()
    {
        if (currentAnimation != null)
            fTimer = currentAnimation.FrameDelay;

        iFrameIndex = 0;
        if (clip_animStart != null && audioSource != null)
            TM_AudioManager.PlayClip(clip_animStart, audioSource);

        bPlaying = true;
        spriteRenderer.enabled = true;
    }

    public void StopAnim()
    {
        bPlaying = false;
    }



	void Update () 
    {
        if (currentAnimation == null)
            return;
	    if (!bPlaying)
	        return;

        fTimer -= Time.deltaTime;
        if( fTimer < 0 )
        {
            fTimer = currentAnimation.FrameDelay;

            iFrameIndex++;
            if (iFrameIndex >= currentAnimation.Sprites.Count)
            {
                iFrameIndex = 0;
                switch (behavior)
                {
                    case EShepAnimBehavior.loop_idle:
                        //Debug.Log("Loopy doops! " + currentAnimation.Name);
                        break;
                    case EShepAnimBehavior.play_once:
                        iFrameIndex = currentAnimation.Sprites.Count - 1;
                        OnAnimEnd();
                        break;
                    case EShepAnimBehavior.play_once_and_hide:
                        spriteRenderer.enabled = false;
                        OnAnimEnd();
                        break;
                    case EShepAnimBehavior.play_once_and_destroy:
                        spriteRenderer.enabled = false;
                        bPlaying = false;
                        Destroy(gameObject,1.0f);
                        break;
                }
            }
            //Debug.Log( gameObject.name + " is drawing " + currentAnimation.Sprites[iFrameIndex] + " index = " + iFrameIndex + " anim = " + currentAnimation.Name );
            spriteRenderer.sprite= currentAnimation.Sprites[iFrameIndex];
        }
	}

    void OnAnimEnd()
    {
        bPlaying = false;

        if (clip_animStart != null && audioSource != null)
            TM_AudioManager.PlayClip(clip_animEnd, audioSource);

        if( onAnimEndActions != null )
        {
            for (int t = 0; t < onAnimEndActions.Count; t++)
            {
                Action<GameObject> a = onAnimEndActions[t];
                a(gameObject);
            }
        }
    }

    public void AddFunctionOnAnimStart(Action<GameObject> func)
    {
        AddActionToListInternal(ref onAnimStartActions, func);
    }

    public void AddFunctionOnAnimEnd(Action<GameObject> func)
    {
        Debug.Log("Adding animEnd event " + func.ToString() );
        AddActionToListInternal( ref onAnimEndActions, func);
        Debug.Log("But hey: " + onAnimEndActions.Count);
        Debug.Log("And it's: " + onAnimEndActions[0].ToString());

    }

    void AddActionToListInternal(ref List<Action<GameObject>> myList, Action<GameObject> func)
    {
        if (myList == null)
            myList = new List<Action<GameObject>>();

        if (!myList.Contains(func))
            myList.Add(func);
    }
}

