using UnityEngine;

public class WhirlingRitual : Ritual
{
    public int minTurnCheckPeriod = 0; // frames
    public int maxTurnCheckPeriod = 100; // frames

    private Vector3 pivot;
    private bool clockwise; // true == clockwise
    private int framesBeforeNextCheck;

    // Update is called once per frame
    void Update()
    {
        if (framesBeforeNextCheck <= 0)
        {
            monk.Move(AfterTurnDirection());
            framesBeforeNextCheck = Random.Range(minTurnCheckPeriod, maxTurnCheckPeriod);
        }
        else
        {
            framesBeforeNextCheck--;
        }
    }

    private Monk.Direction AfterTurnDirection()
    {
        bool leftOfPivot = (transform.position.x < pivot.x);
        bool belowPivot = (transform.position.y < pivot.y);
        if (leftOfPivot && belowPivot) // SW
        {
            return clockwise ? Monk.Direction.Up : Monk.Direction.Right;
        }
        if (leftOfPivot && !belowPivot) // NW
        {
            return clockwise ? Monk.Direction.Right : Monk.Direction.Down;
        }
        if (!leftOfPivot && belowPivot) // SE
        {
            return clockwise ? Monk.Direction.Left : Monk.Direction.Up;
        }
        if (!leftOfPivot && !belowPivot) // NE
        {
            return clockwise ? Monk.Direction.Down : Monk.Direction.Left;
        }
        return Monk.Direction.None; // should not happen
    }

    void OnEnable()
    {
        pivot = GameMgr.instance.GetQuadrantCenterByPosition(transform.position);
        clockwise = GameMgr.instance.GetWhirlSense(transform.position);
        framesBeforeNextCheck = 0;
    }


}
