using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ryCommon
{
    /// <summary>
    /// 自动排列控件
    /// </summary>
    public class ListX
    {
        Control parent = null;
        private int _maxColCount = 1;
        /// <summary>
        /// 最大列数
        /// </summary>
        public int MaxColCount
        {
            get { return _maxColCount>0? _maxColCount:1; }
            set { _maxColCount = value; }
        }
        /// <summary>
        /// 开始排列的坐标
        /// </summary>
        public Point StartPoint{ get; private set; } = new Point(0, 0);
        /// <summary>
        /// 下一项的位置
        /// </summary>
        public Point NowPoint { get; private set; } = new Point(0, 0);
        /// <summary>
        /// 下一项的右下角坐标
        /// </summary>
        public Point RightBottomPoint { get; private set; } = new Point(0, 0);
        /// <summary>
        /// 竖向间隔像素
        /// </summary>
        public int spaceY = 0;
        /// <summary>
        /// 横向间隔像素
        /// </summary>
        public int spaceX = 0;
        /// <summary>
        /// 最小保留右边距,只对ReDraw2有效
        /// </summary>
        public int RightSpace = 20;
        /// <summary>
        /// 自动排列控件构造函数
        /// </summary>
        /// <param name="_parent"></param>
        public ListX(Control _parent)
        {  parent = _parent; }
        /// <summary>
        /// 自动排列控件构造函数
        /// </summary>
        /// <param name="_parent"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        public ListX(Control _parent,int x,int y)
        { parent = _parent; StartPoint= new Point(x, y); NowPoint = StartPoint; RightBottomPoint = StartPoint; }
        /// <summary>
        /// 开始排列控件
        /// </summary>
        public void ReDraw()
        {            
            NowPoint = StartPoint;
            RightBottomPoint = StartPoint;
            InitAdd(true);
            for (int i=0;i<ControlList.Count;i++)
            {
                int curCol = i % MaxColCount;
                if (i == 0)
                {
                    ControlList[i].Location = new Point(StartPoint.X, StartPoint.Y) ;                    
                }
                else
                {
                    ControlList[i].Location =NowPoint;
                }
                if (RightBottomPoint.X < NowPoint.X + ControlList[i].Width)
                { RightBottomPoint = new Point(NowPoint.X + ControlList[i].Width, RightBottomPoint.Y); }
                if (RightBottomPoint.Y < NowPoint.Y + ControlList[i].Height)
                { RightBottomPoint = new Point(RightBottomPoint.X, NowPoint.Y + ControlList[i].Height); }
                if (curCol == MaxColCount - 1)
                {
                    NowPoint = new Point(StartPoint.X, NowPoint.Y + spaceY + ControlList[i].Height);
                }
                else
                {
                    NowPoint = new Point(NowPoint.X+ spaceX+ ControlList[i].Width, NowPoint.Y);
                }
            }
            InitAdd(false);
        }
        /// <summary>
        /// 开始排列控件,当列数超过显示空间时，自动换行
        /// </summary>
        public void ReDraw2()
        {
            NowPoint = StartPoint;
            RightBottomPoint = StartPoint;
            InitAdd(true);
            int max_item_width = 0;
            int max_item_height = 0;
            int curCol = 0;
            for (int i = 0; i < ControlList.Count; i++)
            {
                if(ControlList[i].Width> max_item_width) { max_item_width = ControlList[i].Width; }
                if (ControlList[i].Height > max_item_height) { max_item_height = ControlList[i].Height; }
                if (i == 0)
                {
                    ControlList[i].Location = new Point(StartPoint.X, StartPoint.Y);
                }
                else
                {
                    ControlList[i].Location = NowPoint;
                }
                if (RightBottomPoint.X < NowPoint.X + ControlList[i].Width)
                { RightBottomPoint = new Point(NowPoint.X + ControlList[i].Width, RightBottomPoint.Y); }
                if (RightBottomPoint.Y < NowPoint.Y + ControlList[i].Height)
                { RightBottomPoint = new Point(RightBottomPoint.X, NowPoint.Y + ControlList[i].Height); }
                if (curCol >= MaxColCount - 1)
                {
                    NowPoint = new Point(StartPoint.X, NowPoint.Y + spaceY + max_item_height);//开始新的一行
                    max_item_width = 0;
                    max_item_height = 0;
                    curCol = 0;
                }
                else
                {
                    if (NowPoint.X + spaceX + ControlList[i].Width+ ControlList[i].Width >= parent.Width- RightSpace)//超出宽度
                    {
                        curCol = 0;
                        NowPoint = new Point(StartPoint.X, NowPoint.Y + spaceY + max_item_height);//开始新的一行
                        max_item_width = 0;
                        max_item_height = 0;
                    }
                    else
                    {
                        curCol++;
                        NowPoint = new Point(NowPoint.X + spaceX + ControlList[i].Width, NowPoint.Y);
                    }
                }
            }
            InitAdd(false);
        }
        /// <summary>
        /// 加入的控件列表
        /// </summary>
        public List<Control> ControlList
        { get;private set; } = new List<Control>();
        /// <summary>
        /// 初始化添加,自动将容器设置为滚动条自动显示
        /// </summary>
        /// <param name="isInit"></param>
        public void InitAdd(bool isInit)
        {
            if (parent is Form)
            {
                ((Form)parent).AutoScroll = !isInit;
            }
            else if (parent is Panel)
            {
                ((Panel)parent).AutoScroll = !isInit;
            }
            else if (parent is TabPage)
            {
                ((TabPage)parent).AutoScroll = !isInit;
            }
        }
        /// <summary>
        /// 添加控件
        /// </summary>
        /// <param name="ctl"></param>
        /// <returns></returns>
        public int Add(Control ctl)
        {
            InitAdd(true);
            ctl.Parent = parent;
            int curCol = ControlList.Count % MaxColCount;
            ctl.Location = NowPoint;
            parent.Controls.Add(ctl);
            ControlList.Add(ctl);
            InitAdd(false);
            if (RightBottomPoint.X< NowPoint.X+ctl.Width)
            { RightBottomPoint = new Point(NowPoint.X + ctl.Width, RightBottomPoint.Y); }
            if (RightBottomPoint.Y < NowPoint.Y + ctl.Height)
            { RightBottomPoint = new Point(RightBottomPoint.X, NowPoint.Y + ctl.Height); }
            if (curCol == MaxColCount - 1) //已经是该行最后一项
            {
                NowPoint = new Point(StartPoint.X, NowPoint.Y + spaceY + ctl.Height);               
            }
            else
            {
                NowPoint = new Point(NowPoint.X + spaceX + ctl.Width, NowPoint.Y);
            }
            return ControlList.Count;
        }
        /// <summary>
        /// 删除控件
        /// </summary>
        /// <param name="index"></param>
        public void Delete(int index)
        {
            parent.Controls.Remove(ControlList[index]);
            ControlList.RemoveAt(index);
            ReDraw();
            if (ControlList.Count == 0) { NowPoint = StartPoint; RightBottomPoint = StartPoint; }
        }
        /// <summary>
        /// 删除控件
        /// </summary>
        /// <param name="ctl"></param>
        public void Delete(Control ctl)
        {
            ControlList.Remove(ctl);
            parent.Controls.Remove(ctl);
            ReDraw();
            if (ControlList.Count == 0) { NowPoint = StartPoint; RightBottomPoint = StartPoint; }
        }
        /// <summary>
        /// 清空所有控件
        /// </summary>
        public void Clear()
        {
            parent.Controls.Clear();
            ControlList.Clear();
            NowPoint = StartPoint;
            RightBottomPoint = StartPoint;
        }
    }
}
