using UnityEngine;

namespace ftt.Puzzles
{
    public class MaskComponent : MonoBehaviour, IInteractable
    {
        enum MaskTriggerPoint
        {
            North = 45,
            East = 135,
            South = 225,
            West = 315,
        }

        const float DEGREE_INCREMENT = 90;

        [Header("Set in inspector")]
        [SerializeField] MasksPuzzle handler;
        [SerializeField] MaskTriggerPoint startingPoint = MaskTriggerPoint.North;
        [SerializeField] MaskTriggerPoint triggerPoint = MaskTriggerPoint.North;
        [SerializeField] AudioClip correctPosSFX;

        MaskTriggerPoint currentPoint;
        LineRenderer rayRenderer;
        bool isInCorrectPosition;

        public bool IsInCorrectPosition => isInCorrectPosition;

        private void Awake()
        {
            currentPoint = startingPoint;
            transform.rotation = Quaternion.Euler(new Vector3(0f, (float)startingPoint, 0f));
        }

        public void InteractWithObject()
        {
            currentPoint = (MaskTriggerPoint)((float)currentPoint + DEGREE_INCREMENT);

            if ((float)currentPoint >= 361)
                currentPoint = MaskTriggerPoint.North;

            transform.rotation = Quaternion.Euler(new Vector3(0f, (float)currentPoint, 0f));

            if (currentPoint == triggerPoint)
            {
                isInCorrectPosition = true;
                rayRenderer.enabled = true;
            }
            else
            {
                isInCorrectPosition = false;
                rayRenderer.enabled = false;
            }
        }
    }
}