﻿using OnlineMaps;
using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
    [ActionCategory(OnlineMapsCategories.GENERAL)]
    [Tooltip("Handle tileset events.")]
    public class HandleTilesetEvents : FsmStateAction
    {
        [Tooltip("Tileset OnCameraControl event.")]
        public FsmEvent OnCameraControl;

        [Tooltip("Tileset OnSmoothZoomBegin event.")]
        public FsmEvent OnSmoothZoomBegin;

        [Tooltip("Tileset OnSmoothZoomProcess event.")]
        public FsmEvent OnSmoothZoomProcess;

        [Tooltip("Tileset OnSmoothZoomFinish event.")]
        public FsmEvent OnSmoothZoomFinish;

        public override void Reset()
        {
            OnCameraControl = null;
            OnSmoothZoomBegin = null;
            OnSmoothZoomProcess = null;
            OnSmoothZoomFinish = null;
        }

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

        private void DoEnter()
        {
            if (!Map.instance)
            {
                Debug.LogError("Online Maps not found.");
                return;
            }

            TileSetControl control = TileSetControl.instance;
            if (!control)
            {
                Debug.Log("Need tileset control.");
                return;
            }

            if (OnCameraControl != null)
            {
                if (CameraOrbit.instance)
                {
                    CameraOrbit.instance.OnCameraControl += () => Fsm.Event(OnCameraControl);
                }
            }

            MouseController mc = Map.instance.GetComponent<MouseController>();
            if (mc != null)
            {
                if (OnSmoothZoomBegin != null) mc.OnSmoothZoomBegin += () => Fsm.Event(OnSmoothZoomBegin);
                if (OnSmoothZoomProcess != null) mc.OnSmoothZoomProcess += () => Fsm.Event(OnSmoothZoomProcess);
                if (OnSmoothZoomFinish != null) mc.OnSmoothZoomFinish += () => Fsm.Event(OnSmoothZoomFinish);
            }
        }
    }
}