﻿/*           INFINITY CODE           */
/*     https://infinity-code.com     */

using InfinityCode.uPano.Directions;
using UnityEngine;

namespace HutongGames.PlayMaker.Actions.uPano.Directions
{
    [ActionCategory(uPanoCategories.DIRECTIONS)]
    [Tooltip("Handle Direction Events")]
    public class HandleDirectionEvents : FsmStateAction
    {
        [RequiredField]
        public FsmObject direction;

        public FsmEvent OnClick;
        public FsmEvent OnPointerDown;
        public FsmEvent OnPointerEnter;
        public FsmEvent OnPointerExit;
        public FsmEvent OnPointerUp;


        public override void Reset()
        {
            direction = null;
            OnClick = null;
            OnPointerDown = null;
            OnPointerEnter = null;
            OnPointerExit = null;
            OnPointerUp = null;
        }

        public override void OnEnter()
        {
            DoEnter();
            Finish();
        }

        private void DoEnter()
        {
            if (direction.IsNone)
            {
                Debug.LogError("Requires reference to Hot Spot");
                return;
            }

            ObjectWrapper w = direction.Value as ObjectWrapper;
            if (w == null || !(w.obj is Direction))
            {
                Debug.LogError("Requires reference to Hot Spot");
                return;
            }

            Direction dir = w.obj as Direction;
            if (OnClick != null) dir.OnClick.AddListener(e => Fsm.Event(OnClick));
            if (OnPointerDown != null) dir.OnPointerDown.AddListener(e => Fsm.Event(OnPointerDown));
            if (OnPointerEnter != null) dir.OnPointerEnter.AddListener(e => Fsm.Event(OnPointerEnter));
            if (OnPointerExit != null) dir.OnPointerExit.AddListener(e => Fsm.Event(OnPointerExit));
            if (OnPointerUp != null) dir.OnPointerUp.AddListener(e => Fsm.Event(OnPointerUp));
        }
    }
}