using UnityEngine.UI;
using UnityEngine;

namespace InterfaceOff
{
    [System.Serializable]
    public struct ImageSet
    {
        [field: SerializeField] public Sprite[] Images { get; set; }
    }

    [CreateAssetMenu(fileName = "Image Registry", menuName = "Custom/Create Image Registry")]
    public class ImageRegistry : ScriptableObject
    {
        [field: SerializeField] public ImageSet[] Registered { get; set; }
    }

    public class ImageWindow : WindowBase
    {
        private static Vector3[] Positions =
        {
            new(-45, 45f),
            new(45f, 45f),
            new(-45, -45),
            new(45f, -45)
        };
        
        public override void OnWindowInstantiation()
        {
            Sprite[] sprites = CanvasManager.Instance.Images.Registered[0].Images;
            
            for (int i = 0; i < 4; i++)
            {
                GameObject go = Instantiate(CanvasManager.Instance.ImagePrefab, transform);
                RectTransform t = go.GetComponent<RectTransform>();

                t.sizeDelta = new Vector2(80, 80);
                t.localPosition = Positions[i];
                t.eulerAngles = new Vector3(0, 0, Random.Range(0, 4) * 90);
                
                Image img = go.GetComponent<Image>();
                img.material = new Material(Shader.Find("UI/Default"));
                img.sprite = sprites[i];
                
                Button button = go.GetComponent<Button>();
                button.onClick.AddListener(() =>
                {
                    RectTransform rect = button.GetComponent<RectTransform>();
                    
                    if (!(rect.rotation.eulerAngles.z < 45 && rect.rotation.eulerAngles.z > -45))
                    {
                        Vector3 rot = rect.rotation.eulerAngles;
                        rot += new Vector3(0, 0, 90);
                        
                        rect.rotation = Quaternion.Euler(rot);
                    }
                });
            }
        }
    }
}