using UnityEngine;
using System.Collections;

public class AnimationPlayer : MonoBehaviour {

    private GameObject player;
    private Animator animator;

    AnimatorStateInfo infoAnim;

    private string attack = "attack";
    private string attack2 = "attack2";
    private string idle = "idle";
    private string walking = "walking";

    public bool isWalking = false;
    public bool isAttacking = false;
    public int atid = 0;
    public int wait;

    private string combo = "combo";
    private string attacking = "attacking";
    private string notCombo = "notCombo";

    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");


    void Awake() {
        Debug.Log("Awake AnimationPlayer.cs");
    }

	// Use this for initialization
	void Start () {
        this.player = GameObject.Find("PlayerMesh");
        this.animator = this.player.GetComponent<Animator>();
        this.animator.runtimeAnimatorController = (RuntimeAnimatorController) Resources.Load("Animation/PlayerBody");
        beginIdle();
	}

    public void beginIdle() {
        this.isAttacking = false;
        this.isWalking = false;

        setAttacking0(false);
        setAttacking1(false);
        setWalking(false);
        setIdle(true);
        this.atid = 0;
    }

    public void beginAttack0() {
        this.isAttacking = true;
        this.isWalking = false;
        setAttacking0(true);
        setAttacking1(false);
        setWalking(false);
        setIdle(false);
        this.animator.SetTrigger(attack);
        this.atid = 1;

    }

    public void beginAttack1() {
        this.isAttacking = true;
        this.isWalking = false;
        setAttacking0(false);
        setAttacking1(true);
        setWalking(false);
        setIdle(false);
        this.animator.SetTrigger(attack2);
        this.atid = 2;

    }

    public void beginWalking() {
        this.isWalking = true;
        this.isAttacking = false;
        setAttacking0(false);
        setAttacking1(false);
        setWalking(true);
        setIdle(false);
        this.atid = 0;

    }

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

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

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

    public void setNotCombo(bool state) {
        this.animator.SetBool(notCombo, state);
    }

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

    public bool getAttacking() {
        return isAttacking;
    }
	
	// Update is called once per frame
	void Update () {

        int x = this.animator.GetCurrentAnimatorStateInfo(0).fullPathHash;

        if (x == idleState) {
            if (isAttacking) {
                this.beginIdle();
                return;
            }

            // Attack begins...
            if (Input.GetKeyDown(KeyCode.X))
            {
                if (!isWalking) this.beginAttack0();
            }           
            
            // Attack begins...
            if (Input.GetKeyDown(KeyCode.Z))
            {
                if (!isWalking) this.beginAttack1();
            }

        } else if (x == attack1State) {
            this.isAttacking = true;
        } else if (x == attack2State) {
            this.isAttacking = true;
        } 

        else if (x == walkingState) {

        }
	}
}
