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

namespace DeviceRepairAndOptimization.Common
{
    public static class ControlExtended
    {
        /// <summary>
        /// 整体居中
        /// </summary>
        /// <param name="control"></param>
        public static Control ToOmnibearingCenter(this Control control)
        {
            control.ToVerticalCenter().ToHorizontalCenter();
            return control;
        }

        /// <summary>
        /// 控件垂直居中
        /// </summary>
        /// <param name="control"></param>
        public static Control ToVerticalCenter(this Control control)
        {
            int Y = (control.Parent.Height - control.Height) / 2;
            control.Location = new System.Drawing.Point(control.Location.X, Y);
            return control;
        }

        /// <summary>
        /// 控件水平居中
        /// </summary>
        /// <param name="control"></param>
        public static Control ToHorizontalCenter(this Control control)
        {
            int X = (control.Parent.Width - control.Width) / 2;
            control.Location = new System.Drawing.Point(X, control.Location.Y);
            return control;
        }

        /// <summary>
        /// 水平填充满父级控件
        /// </summary>
        /// <param name="control"></param>
        public static Control FillInHorizontal(this Control control)
        {
            int W = control.Parent.Width;
            control.Width = W;
            return control;
        }

        /// <summary>
        /// 水平填充满父级控件
        /// </summary>
        /// <param name="control"></param>
        public static Control FillInVertical(this Control control)
        {
            int H = control.Parent.Height;
            control.Height = H;
            return control;
        }

        /// <summary>
        /// 填充满父级
        /// </summary>
        /// <param name="control"></param>
        public static Control FillInParent(this Control control)
        {
            control.FillInHorizontal().FillInVertical();
            return control;
        }

        /// <summary>
        /// 控件组设置控件水平间隙
        /// </summary>
        /// <param name="Value"></param>
        /// <param name="controls"></param>
        public static void SetGroupControlHorizontalMarggin(int Value, params Control[] controls)
        {
            List<Control> lst = controls.ToList();
            lst.Sort(new Comparison<Control>(delegate (Control c1, Control c2)
            {
                return c1.Location.X.CompareTo(c2.Location.X);
            }));

            for (int i = 0; i < lst.Count; i++)
            {
                if (i == 0)
                    continue;
                lst[i].Location =
                    new System.Drawing.Point(lst[i - 1].Location.X + lst[i - 1].Width + Value, lst[i].Location.Y);
            }
        }

        /// <summary>
        /// 控件组设置控件垂直间隙
        /// </summary>
        /// <param name="Value"></param>
        /// <param name="controls"></param>
        public static void SetGroupControlVerticalMarggin(int Value, params Control[] controls)
        {
            List<Control> lst = controls.ToList();
            lst.Sort(new Comparison<Control>(delegate (Control c1, Control c2)
            {
                return c1.Location.Y.CompareTo(c2.Location.Y);
            }));

            for (int i = 0; i < lst.Count; i++)
            {
                if (i == 0)
                    continue;
                lst[i].Location =
                    new System.Drawing.Point(lst[i].Location.X, lst[i - 1].Location.Y + lst[i - 1].Height + Value);
            }
        }

        /// <summary>
        /// 水平居中
        /// </summary>
        /// <param name="controls"></param>
        public static void GroupHorizontalConter(params Control[] controls)
        {
            List<Control> lst = controls.ToList();
            lst.Sort(new Comparison<Control>(delegate (Control c1, Control c2)
            {
                return c1.Location.X.CompareTo(c2.Location.X);
            }));

            int minX = lst.Min(x => x.Location.X);
            int maxX = lst.Max(x => x.Location.X);

            int totalWidth = maxX - minX + lst.LastOrDefault().Width;
            int difference = (lst.FirstOrDefault().Parent.Width - totalWidth) / 2 - lst.FirstOrDefault().Location.X;

            foreach (Control item in controls)
            {
                item.Location = new System.Drawing.Point(item.Location.X + difference, item.Location.Y);
            }
        }

        /// <summary>
        /// 垂直居中
        /// </summary>
        /// <param name="controls"></param>
        public static void GroupVerticalConter(params Control[] controls)
        {
            List<Control> lst = controls.ToList();
            lst.Sort(new Comparison<Control>(delegate (Control c1, Control c2)
            {
                return c1.Location.Y.CompareTo(c2.Location.Y);
            }));

            int minY = lst.Min(x => x.Location.Y);
            int maxY = lst.Max(x => x.Location.Y);

            int totalHeight = maxY - minY + lst.LastOrDefault().Height;
            int difference = (lst.FirstOrDefault().Parent.Height - totalHeight) / 2 - lst.FirstOrDefault().Location.Y;

            foreach (Control item in controls)
            {
                item.Location = new System.Drawing.Point(item.Location.X, item.Location.Y + difference);
            }
        }

        /// <summary>
        /// 设置宽度百分比
        /// </summary>
        /// <param name="percent"></param>
        /// <returns></returns>
        public static Control SetWidhtPercent(this Control control, int percent)
        {
            int width = control.Parent.Width * percent / 100;
            control.Width = width;
            return control;
        }

        public static void ControlBoardPoint(this Control sender, PaintEventArgs e, params Direction[] directions)
        {
            Rectangle tang = (sender).ClientRectangle; //获取窗口矩形 为了下面得到窗口的宽高
            Graphics g3 = e.Graphics; //新建一个画布
            Color c3 = Color.FromArgb(183, 183, 183); //声明一个 颜色
            Pen p3 = new Pen(c3); //新建一支画笔
            g3.SmoothingMode = SmoothingMode.HighQuality; //抗锯齿 使得线条变柔顺  在画斜线或者曲线的时候使用
            g3.InterpolationMode = InterpolationMode.HighQualityBicubic; //使得画出来的效果高质量
            g3.CompositingQuality = CompositingQuality.HighQuality; //高质量画图

            foreach (Direction item in directions)
            {
                switch (item)
                {
                    case Direction.Up:
                        g3.DrawLine(p3, tang.Width - 1, 0, 0, 0);
                        break;
                    case Direction.Down:
                        g3.DrawLine(p3, 0, tang.Height - 1, tang.Width - 1, tang.Height - 1);
                        break;
                    case Direction.Left:
                        g3.DrawLine(p3, 0, 0, 0, tang.Height - 1);
                        break;
                    case Direction.Right:
                        g3.DrawLine(p3, tang.Width - 1, tang.Height - 1, tang.Width - 1, 0);
                        break;
                    default:
                        break;
                }
            }
        }

        public static void FormBoardPoint(this Form form, params Direction[] directions)
        {
            Rectangle tang = (form).ClientRectangle; //获取窗口矩形 为了下面得到窗口的宽高
            Graphics g3 = form.CreateGraphics(); //新建一个画布
            Color c3 = Color.FromArgb(183, 183, 183); //声明一个 颜色
            Pen p3 = new Pen(c3, 2); //新建一支画笔
            g3.SmoothingMode = SmoothingMode.HighQuality; //抗锯齿 使得线条变柔顺  在画斜线或者曲线的时候使用
            g3.InterpolationMode = InterpolationMode.HighQualityBicubic; //使得画出来的效果高质量
            g3.CompositingQuality = CompositingQuality.HighQuality; //高质量画图

            foreach (Direction item in directions)
            {
                switch (item)
                {
                    case Direction.Up:
                        g3.DrawLine(p3, tang.Width, 0, 0, 0);
                        break;
                    case Direction.Down:
                        g3.DrawLine(p3, 0, tang.Height, tang.Width, tang.Height);
                        break;
                    case Direction.Left:
                        g3.DrawLine(p3, 0, 0, 0, tang.Height);
                        break;
                    case Direction.Right:
                        g3.DrawLine(p3, tang.Width, tang.Height, tang.Width, 0);
                        break;
                    default:
                        break;
                }
            }
        }

        public static void ToYearStyle(this DevExpress.XtraEditors.DateEdit dateEdit, bool touchUI = false)
        {
            if (touchUI)
            {
                dateEdit.Properties.CalendarView = DevExpress.XtraEditors.Repository.CalendarView.TouchUI;
            }
            else
                dateEdit.Properties.CalendarView = DevExpress.XtraEditors.Repository.CalendarView.Vista;

            dateEdit.Properties.ShowToday = false;
            dateEdit.Properties.ShowMonthHeaders = false;
            dateEdit.Properties.VistaCalendarInitialViewStyle =
                DevExpress.XtraEditors.VistaCalendarInitialViewStyle.YearsGroupView;
            dateEdit.Properties.VistaCalendarViewStyle = DevExpress.XtraEditors.VistaCalendarViewStyle.YearsGroupView;
            dateEdit.Properties.Mask.EditMask = "yyyy";
            dateEdit.Properties.Mask.UseMaskAsDisplayFormat = true;
        }
    }


    public enum Direction
    {
        Up,
        Down,
        Left,
        Right
    }
}
