using OnlineMaps;
using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
    [ActionCategory(OnlineMapsCategories.DRAWING)]
    [Tooltip("Draw rectangle on map.")]
    public class DrawRectangle : FsmStateAction
    {
        [RequiredField]
        [Tooltip("Longitude of top-left angle.")]
        public FsmFloat x = 0;

        [RequiredField]
        [Tooltip("Latitude of top-left angle.")]
        public FsmFloat y = 0;

        [RequiredField]
        [Tooltip("Width in arc angles.")]
        public FsmFloat width = 1;

        [RequiredField]
        [Tooltip("Height in arc angles.")]
        public FsmFloat height = 1;

        [Tooltip("Border color.")]
        public FsmColor borderColor = Color.black;

        [Tooltip("Border weight.")]
        public FsmInt borderWeight = 1;

        [Tooltip("Background color.")]
        public FsmColor backgroundColor = new Color(1, 1, 1, 0);

        [Tooltip("Y Offset.")]
        public FsmFloat yOffset = 0;

        [Tooltip("Drawing element instance.")]
        [UIHint(UIHint.Variable)]
        public FsmObject storeInstance;

        public override void Reset()
        {
            x = 0;
            y = 0;
            width = 1;
            height = 1;
            borderColor = Color.black;
            borderWeight = 1;
            backgroundColor = new Color(1, 1, 1, 0);
            yOffset = 0;
            storeInstance = null;
        }

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

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

            Rectangle rect = new Rectangle(x.Value, y.Value, width.Value, height.Value);
            if (!borderColor.IsNone) rect.backgroundColor = borderColor.Value;
            if (!borderWeight.IsNone) rect.borderWidth = borderWeight.Value;
            if (!backgroundColor.IsNone) rect.backgroundColor = backgroundColor.Value;
            if (!yOffset.IsNone) rect.yOffset = yOffset.Value;

            DrawingElementManager.AddItem(rect);

            if (!storeInstance.IsNone) storeInstance.Value = new DrawingElementWrapper(rect);
        }
    }
}