using System;
using UnityEngine;
using UnityEngine.Serialization;
using UnityEngine.UI;

namespace AlicizaX.UI
{
    [Serializable]
    [ControlerStateName("GameObject/Visiblity")]
    public class GameObjectPropertyStateBase : ControllerStateBase
    {
        public override void Init(UXControllerStateRecorder recorder)
        {
        }

        public override void Execute(UXControllerStateRecorder recorder, int entryIndex, int selectionIndex)
        {
            if (recorder != null && recorder.gameObject != null)
            {
                var visible = entryIndex == selectionIndex;
                recorder.gameObject.SetActive(visible);
            }
        }

        public override bool Valid(UXControllerStateRecorder recorder)
        {
            return recorder != null;
        }
    }

    [Serializable]
    [ControlerStateName("Transform/Position")]
    public class TransformPostionPropertyStateBase : ControllerStateBase
    {
        [SerializeField] private Vector2 _pos;
        [HideInInspector] [SerializeField] private Vector2 _cachePos;

        public override void Init(UXControllerStateRecorder recorder)
        {
            _cachePos = recorder.gameObject.GetComponent<RectTransform>().anchoredPosition;
        }

        public override void Execute(UXControllerStateRecorder recorder, int entryIndex, int selectionIndex)
        {
            if (recorder != null && recorder.gameObject != null)
            {
                var pos = entryIndex != selectionIndex ? _cachePos : _pos;
                recorder.GetComponent<RectTransform>().anchoredPosition = pos;
            }
        }

        public override bool Valid(UXControllerStateRecorder recorder)
        {
            return recorder != null;
        }
    }

    [Serializable]
    [ControlerStateName("Transform/Scale")]
    public class TransformScalePropertyStateBase : ControllerStateBase
    {
        [SerializeField] private Vector3 _scale;
        [HideInInspector] [SerializeField] private Vector3 _cacheScale;


        public override void Init(UXControllerStateRecorder recorder)
        {
            _cacheScale = recorder.gameObject.GetComponent<RectTransform>().localScale;
        }

        public override void Execute(UXControllerStateRecorder recorder, int entryIndex, int selectionIndex)
        {
            if (recorder != null && recorder.gameObject != null)
            {
                var scale = entryIndex != selectionIndex ? _cacheScale : _scale;
                recorder.GetComponent<RectTransform>().localScale = scale;
            }
        }

        public override bool Valid(UXControllerStateRecorder recorder)
        {
            return recorder != null;
        }
    }

    [Serializable]
    [ControlerStateName("Text/Color")]
    public class TextColorPropertyStateBase : ControllerStateBase
    {
        [SerializeField] private Color _color = Color.white;
        [HideInInspector] [SerializeField] private Color _cacheColor;

        public override void Init(UXControllerStateRecorder recorder)
        {
            _cacheColor = recorder.GetComponent<Text>().color;
        }

        public override void Execute(UXControllerStateRecorder recorder, int entryIndex, int selectionIndex)
        {
            var t = recorder.GetComponent<Text>();
            if (t != null)
            {
                var color = entryIndex != selectionIndex ? _cacheColor : _color;
                t.color = color;
            }
        }

        public override bool Valid(UXControllerStateRecorder recorder)
        {
            return recorder != null && recorder.GetComponent<Text>() != null;
        }
    }
}
