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

using InfinityCode.uPano.HotSpots;
using UnityEngine;

namespace HutongGames.PlayMaker.Actions.uPano.HotSpots
{
    [ActionCategory(uPanoCategories.HOTSPOTS)]
    [Tooltip("Handle HotSpot Events")]
    public class HandleHotSpotEvents : FsmStateAction
    {
        [RequiredField]
        public FsmObject hotSpot;

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

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

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

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

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

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