using System.Collections.Generic;
using RebootKit.Engine.Foundation;
using RebootKit.Engine.UI;
using UnityEditor.UIElements;
using UnityEngine;
using UnityEngine.UIElements;

namespace RebootKit.Editor.RebootWindow {
    public class GameServicesView : IView {
        VisualElement m_RootElement;

        readonly List<UnityEditor.Editor> m_ServiceEditors = new List<UnityEditor.Editor>();
        
        public void Dispose() {
            foreach (UnityEditor.Editor editor in m_ServiceEditors) {
                if (editor != null) {
                    Object.DestroyImmediate(editor);
                }
            }
            m_ServiceEditors.Clear();
        }

        public VisualElement Build() {
            m_RootElement = new ScrollView();

            // int servicesLength = m_EngineConfigAsset.services?.Length ?? 0;
            //
            // Label servicesAmountLabel = new($"Game services: {servicesLength}") {
            //     style = {
            //         color = new Color(0.7f, 0.9f, 0.9f),
            //         unityFontStyleAndWeight = FontStyle.Bold
            //     }
            // };
            // m_RootElement.Add(servicesAmountLabel);
            //
            // for (int i = 0; i < servicesLength; i++) {
            //     ServiceAsset serviceAsset = m_EngineConfigAsset.services[i];
            //
            //     VisualElement serviceView = CreateServicesView(serviceAsset);
            //     serviceView.style.backgroundColor = i % 2 == 0 ? new Color(0.1f, 0.1f, 0.1f) : new Color(0.2f, 0.2f, 0.2f);
            //     m_RootElement.Add(serviceView);
            // }

            return m_RootElement;
        }

        VisualElement CreateServicesView<T>(ServiceAsset<T> serviceAsset) where T : class, IService {
            VisualElement root = new VisualElement {
                style = {
                    paddingBottom = 4,
                    paddingTop = 4,
                    paddingLeft = 4,
                    paddingRight = 4,
                    borderBottomLeftRadius = 4,
                    borderBottomRightRadius = 4 
                }
            };

            VisualElement header = new VisualElement {
                style = {
                    backgroundColor = new Color(0.2f, 0.2f, 0.2f),
                    paddingLeft = 8,
                    paddingRight = 8,
                    paddingTop = 4,
                    paddingBottom = 4,
                    borderTopLeftRadius = 4,
                    borderTopRightRadius = 4,
                }
            };
            root.Add(header);

            Label nameLabel = new Label(serviceAsset.name) {
                style = {
                    color = new Color(0.7f, 0.9f, 0.9f),
                    unityFontStyleAndWeight = FontStyle.Bold
                }
            };
            header.Add(nameLabel);

            VisualElement editorView = new VisualElement {
                style = {
                    backgroundColor = new Color(0.3f, 0.3f, 0.3f),
                    paddingLeft = 10,
                    paddingRight = 10,
                    paddingTop = 5,
                    paddingBottom = 5,
                    minHeight = 50
                }
            };
            root.Add(editorView);
            
            UnityEditor.Editor editor = UnityEditor.Editor.CreateEditor(serviceAsset);
            m_ServiceEditors.Add(editor);

            InspectorElement inspectorElement = new InspectorElement(editor);
            editorView.Add(inspectorElement);
            
            return root;
        }
    }
}