using UnityEngine;
using System.Collections;

public class AnimationDemon: MonoBehaviour {


    private GameObject player;
    public Animator animator;

    private string attack0 = "attack0";
    private string attack1 = "attack1";
    private string idle = "idle";
    private string walking = "walking";
    private string death = "death";
    private string wait = "wait";

    private bool loaded = false;
    private bool transition = false;
    private bool canAttack = false;

    int idleState = Animator.StringToHash("Base Layer.Idle");
    int attack1State = Animator.StringToHash("Base Layer.Attack0");
    int attack2State = Animator.StringToHash("Base Layer.Attack1");
    int walkingState = Animator.StringToHash("Base Layer.Walking");

        
    public void setData(GameObject go) {
        this.animator = go.GetComponent<Animator>();
        this.animator.runtimeAnimatorController = (RuntimeAnimatorController) Resources.Load("Animation/Demon");
        setWait(0);
        beginIdle();
        loaded = true;
    }


    public void setWalking(bool state)
    {
        this.animator.SetBool(walking, state);
    }

    public void setAttacking1(bool state)
    {
        this.animator.SetBool(attack1, state);
    }

    public void setAttacking0(bool state)
    {
        this.animator.SetBool(attack0, state);
    }

    public void setIdle(bool state)
    {
        this.animator.SetBool(idle, state);
    }

    public void setDeath(bool state)
    {
        this.animator.SetBool(death, state);
    }

    public int getWait()
    {
        return this.animator.GetInteger(wait);
    }

    public void setWait(int time) {
        this.animator.SetInteger(wait, time);
    }

    public void reduceWait()
    {
        if (getWait() > 0) setWait(getWait() - 1);
    }

    public void beginIdle()
    {
        setAttacking0(false);
        setAttacking1(false);
        setWalking(false);
        setIdle(true);
        setDeath(false);
        transition = true;

    }

    public void beginAttack0()
    {
        setAttacking0(true);
        setAttacking1(false);
        setWalking(false);
        setIdle(false);
        setDeath(false);
        transition = true;
    }

    public void beginAttack1()
    {
        setAttacking0(false);
        setAttacking1(true);
        setWalking(false);
        setIdle(false);
        setDeath(false);
        transition = true;

    }

    public void beginDeath()
    {
        setAttacking0(false);
        setAttacking1(false);
        setWalking(false);
        setIdle(false);
        setDeath(true);
        transition = true;
    }

    public void beginWalking() {
        setAttacking0(false);
        setAttacking1(false);
        setWalking(true);
        setIdle(false);
        setDeath(false);
        transition = true;

    }
    // Update is called once per frame
    void Update() {
        // Try to prevent to play upload before the mesh and other data are loaded.
        if (loaded)
        {

            // When idling, the mob is just moving around.
            if (getAnimationStatus() == idleState)
            {
                Debug.Log("IDLE_ANIM");

                // If is coming back of a transition, needs to update the cooldown.
                if (transition)
                {
                    transition = false;
                    //setWait(30);
                }

                reduceWait();
            }


            // The transition between idle and the others attacks requires that a var
            // (call it cool down, call it waiting time between actions) will be 0. So when the
            // animation state jumps into the new state, wait is updated with the correct value.
            else if (getAnimationStatus() == attack1State)
            {

                // If is coming back of a transition, needs to update the cooldown.
                if (transition)
                {
                    transition = false;
                    setWait(60);
                }

                reduceWait();

                //if (getWait() <= 0) {
                //    // 
                //    beginIdle();

                //}
            }

            // The transition between idle and the others attacks requires that a var
            // (call it cool down, call it waiting time between actions) will be 0. So when the
            // animation state jumps into the new state, wait is updated with the correct value.
            else if (getAnimationStatus() == attack2State)
            {
                if (transition)
                {
                    transition = false;
                    setWait(60);
                }
                reduceWait();
            }
            
            else if (getAnimationStatus() == walkingState)
            {
                if (transition)
                {
                    transition = false;
                    //setWait(30);
                }
            }

            Debug.Log("WAIT: " + getWait());

        }
    }

    public int getAnimationStatus()
    {
        return this.animator.GetCurrentAnimatorStateInfo(0).fullPathHash;
    }
}
