using UnityEngine;

public class SortingRitual : Ritual
{

    private Vector3 target;
    private Monk.Direction[] dirs;
    private int step;    

    // Update is called once per frame
    void Update()
    {
        if (step < dirs.Length) {
            Vector3 diff = target - transform.position;
            float distanceFromPartialTarget = float.MaxValue;
            switch (dirs[step])
            {
                case Monk.Direction.Right:
                case Monk.Direction.Left:
                    distanceFromPartialTarget = diff.x;
                    break;
                case Monk.Direction.Up:
                case Monk.Direction.Down:
                    distanceFromPartialTarget = diff.y;
                    break;
            }
            if (Mathf.Abs(distanceFromPartialTarget) < monk.speed * 2)
            {
                step++;
                if (step < dirs.Length)
                {
                    monk.Move(dirs[step]);
                }
                else
                {
                    monk.Stop();
                }
            }
        }
    }

    void OnEnable()
    {
        target = GameMgr.instance.getRandomPos(monk.color);        
        bool horizontalThenVertical = (Random.value < 0.5);
        dirs = new Monk.Direction[2];
        Vector3 diff = target - transform.position;
        if (diff.x > 0)
        {
            dirs[horizontalThenVertical ? 0 : 1] = Monk.Direction.Right;
        }
        else
        {
            dirs[horizontalThenVertical ? 0 : 1] = Monk.Direction.Left;
        }
        if (diff.y > 0)
        {
            dirs[horizontalThenVertical ? 1 : 0] = Monk.Direction.Up;
        }
        else
        {
            dirs[horizontalThenVertical ? 1 : 0] = Monk.Direction.Down;
        }
        step = 0;
        monk.Move(dirs[0]);
    }


}