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

public class Monk : MonoBehaviour
{    
    public enum Direction
    {
        Up,
        Down,
        Left,
        Right,
        None
    };
    public int color; // 0 to 3
    public float speed; // distance per frame

    private Direction dir = Direction.None;
    private bool idle = true;
    private Transform t;

    private float maxX;
    private float minX;
    private float maxY;
    private float minY;


    // Use this for initialization
    void Start()
    {
        t = transform;
        maxX = + GameMgr.instance.maxWidth / 2;
        minX = - GameMgr.instance.maxWidth / 2;
        maxY = + GameMgr.instance.maxHeight / 2;
        minY = - GameMgr.instance.maxHeight / 2;
		GetComponent<Animator>().SetTrigger("idle");
    }

    // Update is called once per frame
    void Update()
    {
        if (!idle)
        {
            Vector3 newPosition = t.position;
            switch (dir)
            {
                case Direction.Up:
                    newPosition.y += speed;
					newPosition.z = newPosition.y/10f;
					
                    break;
                case Direction.Down:
					newPosition.y -= speed;
					newPosition.z = newPosition.y/10f;
                    break;
                case Direction.Left:
                    newPosition.x -= speed;
                    break;
                case Direction.Right:
                    newPosition.x += speed;
                    break;
            }
            if (IsValidPosition(newPosition))
            {
                t.position = newPosition;               
            }
            else
            {
                Stop();
            }
        }

    }

    private bool IsValidPosition(Vector3 position)
    {
        return (position.x >= minX &&
                position.x <= maxX &&
                position.y >= minY &&
                position.y <= maxY);
    }

    public void Move(Direction dir)
    {
        if (idle || dir != this.dir)
        {
			if(dir == Direction.Down)
				GetComponent<Animator>().SetTrigger("downWalking");
			if(dir == Direction.Up)
				GetComponent<Animator>().SetTrigger("upWalking");
			if(dir == Direction.Left)
				GetComponent<Animator>().SetTrigger("leftWalking");
			if(dir == Direction.Right)
				GetComponent<Animator>().SetTrigger("rightWalking");
            this.dir = dir;
            this.idle = false;
        }
    }

    public void Stop()
    {
        if (!idle)
        {
			GetComponent<Animator>().SetTrigger("idle");
            this.idle = true;
        }
    }

    public void Pray(int prayer)
    {
		Stop();
		if(prayer == 1)
			GetComponent<Animator>().SetTrigger("searching");
		if(prayer == 0)
		{
			GetComponent<Animator>().SetTrigger("genuflection");
		}

        this.idle = true;
    }
}
