using FizzyLauncher.Models;
using RyzStudio.Windows.Forms;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace FizzyLauncher.Windows.Forms
{
    public class TileContainer : Panel
    {
        protected const int DEFAULT_COLUMN = 6;

        protected FlowLayoutPanel flowLayoutPanel1 = null;


        public TileContainer()
        {
            flowLayoutPanel1 = new FlowLayoutPanel();
            flowLayoutPanel1.AutoSize = true;
            flowLayoutPanel1.AutoSizeMode = AutoSizeMode.GrowAndShrink;
            flowLayoutPanel1.BackColor = Color.Transparent;
            flowLayoutPanel1.FlowDirection = FlowDirection.TopDown;
            flowLayoutPanel1.Location = new Point(10, 10);
            flowLayoutPanel1.Margin = new Padding(0);
            flowLayoutPanel1.Padding = new Padding(0, 0, 0, 20);
            flowLayoutPanel1.Size = new Size(0, 20);
            flowLayoutPanel1.WrapContents = false;

            this.AutoScroll = true;
            this.AutoSizeMode = AutoSizeMode.GrowAndShrink;
            this.BackColor = System.Drawing.Color.Transparent;
            this.Margin = new Padding(0);
            this.Name = "tileContainer1";
            this.Padding = new Padding(10, 10, 10, 20);
            this.Controls.Add(flowLayoutPanel1);
        }


        [Browsable(false)]
        public event EventHandler OnColumnSizeChanged;


        public int CalcWidth
        {
            get =>
                TilePanelLayout.CalcWidth(this.TileWidthCount) +
                this.Left + this.Padding.Horizontal + this.Margin.Horizontal +
                flowLayoutPanel1.Padding.Horizontal + flowLayoutPanel1.Margin.Horizontal;
        }

        public int GroupCount
        {
            get => flowLayoutPanel1.Controls.Count;
        }

        public IEnumerable<TilePanelLayout> Groups
        {
            get
            {
                for (int i = 0; i < flowLayoutPanel1.Controls.Count; i++)
                {
                    if (flowLayoutPanel1.Controls[i].GetType() != typeof(TilePanelLayout))
                    {
                        continue;
                    }

                    TilePanelLayout container = flowLayoutPanel1.Controls[i] as TilePanelLayout;
                    yield return container;
                }
            }
        }

        public IEnumerable<TileGroupModel> GroupModels
        {
            get
            {
                foreach (TilePanelLayout item in this.Groups)
                {
                    yield return item.Model;
                }
            }
        }

        public int TileWidthCount { get; private set; } = DEFAULT_COLUMN;


        public void Add()
        {
            this.Add(new TilePanelLayout(new TileGroupModel()
            {
                Title = "New Group",
                IsExpanded = true,
                GridSize = new Size(this.TileWidthCount, 1)
            }));
        }

        public void Add(TilePanelLayout tilePanelLayout)
        {
            UIControl.Add(flowLayoutPanel1, tilePanelLayout);

            this.InvalidateColumnSize();
        }

        public void Add(int columnCount)
        {
            this.TileWidthCount = ((columnCount <= 0) ? DEFAULT_COLUMN : columnCount);

            this.Add();
        }

        public void Clear()
        {
            UIControl.Clear(flowLayoutPanel1);

            this.TileWidthCount = DEFAULT_COLUMN;

            InvalidateColumnSize();
        }

        public void InvalidateColumnSize()
        {
            this.OnColumnSizeChanged?.Invoke(this, null);
        }

        public void Load(List<TileGroupModel> groupList)
        {
            this.Clear();

            if (groupList == null)
            {
                return;
            }

            foreach (TileGroupModel item in groupList)
            {
                this.TileWidthCount = Math.Max(this.TileWidthCount, item.GridSize.Width);

                TilePanelLayout panel = new TilePanelLayout(item);

                this.Add(panel);
            }

            InvalidateColumnSize();
        }

    }
}
