using System.Collections.Generic;
using RebootKit.Engine.UI;
using UnityEngine;
using UnityEngine.Assertions;
using UnityEngine.UIElements;

namespace RebootKit.Editor.RebootWindow {
    public class TabView : IView {
        struct Tab {
            public string name;
            public IView view;
            public VisualElement container;
        }

        readonly List<Tab> m_Tabs = new List<Tab>();
        readonly List<VisualElement> m_TabContents = new List<VisualElement>();
        readonly List<Button> m_TabButtons = new List<Button>();
        int m_CurrentTabIndex = -1;

        public void Dispose() {
        }

        public VisualElement Build() {
            VisualElement rootContainer = new VisualElement {
                style = {
                    flexDirection = FlexDirection.Row,
                    flexGrow = 1,
                    width = new StyleLength(new Length(100, LengthUnit.Percent)),
                    height = new StyleLength(new Length(100, LengthUnit.Percent)),
                    backgroundColor = RTheme.s_BackgroundPrimaryColor
                }
            };

            VisualElement tabContainer = new VisualElement {
                style = {
                    flexDirection = FlexDirection.Row,
                    width = new StyleLength(new Length(100, LengthUnit.Percent)),
                }
            };
            rootContainer.Add(tabContainer);

            VisualElement tabButtonsContainer = new VisualElement {
                style = {
                    flexDirection = FlexDirection.Column,
                    borderRightWidth = 1,
                    borderRightColor = new Color(0.1f, 0.1f, 0.1f),
                    width = 100,
                    fontSize = 24,
                    backgroundColor = RTheme.s_BackgroundSecondaryColor
                }
            };
            tabContainer.Add(tabButtonsContainer);

            VisualElement contentContainer = new VisualElement {
                style = {
                    flexGrow = 1,
                    paddingLeft = 4,
                    paddingRight = 4,
                    paddingTop = 4,
                    paddingBottom = 4,
                }
            };
            tabContainer.Add(contentContainer);

            for (int i = 0; i < m_Tabs.Count; i++) {
                Tab tab = m_Tabs[i];
                int index = i;

                Button button = new Button(() => SetActiveTab(index)) {
                    text = tab.name,
                    style = {
                        paddingLeft = 8,
                        paddingRight = 8,
                        paddingTop = 4,
                        paddingBottom = 4,
                        borderTopLeftRadius = 0,
                        borderTopRightRadius = 0,
                        borderBottomLeftRadius = 0,
                        borderBottomRightRadius = 0,
                        backgroundColor = new Color(0.2f, 0.2f, 0.2f)
                    }
                };
                tabButtonsContainer.Add(button);
                m_TabButtons.Add(button);
                
                VisualElement tabContent = tab.container;
                tabContent.style.display = DisplayStyle.None;
                contentContainer.Add(tabContent);
                m_TabContents.Add(tabContent);
            }

            SetActiveTab(0);
            return rootContainer;
        }

        public void AddTab(string name, IView view) {
            VisualElement tabContainer = new VisualElement {
                name = "rr_tab__container",
                style = {
                    flexGrow = 1
                }
            };

            VisualElement viewVisualElement = view.Build();
            Assert.IsNotNull(viewVisualElement);

            tabContainer.Add(viewVisualElement);

            m_Tabs.Add(new Tab {
                name = name,
                view = view,
                container = tabContainer
            });
        }

        void SetActiveTab(int index) {
            if (m_CurrentTabIndex >= 0) {
                m_TabContents[m_CurrentTabIndex].style.display = DisplayStyle.None;
                m_TabButtons[m_CurrentTabIndex].style.backgroundColor = new Color(0.2f, 0.2f, 0.2f);
            }

            m_CurrentTabIndex = index;
            m_TabContents[index].style.display = DisplayStyle.Flex;
            m_TabButtons[index].style.backgroundColor = new Color(0.3f, 0.3f, 0.3f);
        }
    }
}