using MGdu.WinFormUI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace RySkins
{
    [ComVisible(true)]
    public partial class SKinForm : GMForm
    {
        /// <summary>
        /// 无参数启动
        /// </summary>
        public SKinForm() : base()
        {
            InitializeComponent();
        }
        private IntPtr ActiveHandle = IntPtr.Zero;
        protected override void OnCreateControl()
        {
            //ActiveHandle= ryCommon.RyForm.GetActiveWindow();
            base.OnCreateControl();
            InitForm();
        }
        protected override void OnHandleCreated(EventArgs e)
        {
            ActiveHandle = ryCommon.RyForm.GetActiveWindow();
            base.OnHandleCreated(e);
        }
        /// <summary>
        /// 以参数方式启动
        /// </summary>
        /// <param name="args"></param>
        public SKinForm(string[] args) : base()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 以单参数方式启动
        /// </summary>
        /// <param name="arg"></param>
        public SKinForm(string arg) : base()
        {
            InitializeComponent();
        }
        private void InitForm()
        {
            if (this.Parent == null)
            {
                if (this.StartPosition == FormStartPosition.CenterParent)
                {
                    var handle = ActiveHandle;
                    var parent_handle = WinAPI.User32.GetParent(Handle);
                    //var dd= this.ParentForm;
                    if (parent_handle == IntPtr.Zero) { parent_handle = handle; }
                    var forms = Application.OpenForms;
                    for (int i = 0; i < forms.Count; i++)
                    {
                        if (forms[i].Handle == handle)
                        {
                            ShowInCenter(forms[i]);
                            break;
                        }
                    }
                }
                else if (this.StartPosition == FormStartPosition.CenterScreen)
                {
                    ShowInCenter(null);
                }
            }
        }
        /// <summary>
        /// 显示在父窗体中间,如果没有父窗体,则显示在显示器中间
        /// </summary>
        /// <param name="parent"></param>
        public void ShowInCenter(Form parent)
        {
            ShowInCenter(this, parent);
        }
        /// <summary>
        /// 显示在父窗体中间,如果没有父窗体,则显示在显示器中间
        /// </summary>
        /// <param name="CurForm"></param>
        /// <param name="parent"></param>
        public static void ShowInCenter(Form CurForm, Form parent)
        {
            CurForm.StartPosition = FormStartPosition.Manual;
            var screen = Screen.FromControl(CurForm);
            if (parent == null) //如果没有父窗体,则显示在显示器中间
            {
                CurForm.Location = new Point((screen.WorkingArea.Width - CurForm.Width) / 2, (screen.WorkingArea.Height - CurForm.Height) / 2);
                return;
            }
            var x = parent.Left + (parent.Width - CurForm.Width) / 2;
            var y = parent.Top + (parent.Height - CurForm.Height) / 2;
            if ((x + CurForm.Width) > screen.WorkingArea.X + screen.WorkingArea.Width)
            {
                x = screen.WorkingArea.X + screen.WorkingArea.Width - CurForm.Width;
            }
            if ((y + CurForm.Height) > screen.WorkingArea.Y + screen.WorkingArea.Height)
            {
                y = screen.WorkingArea.Y + screen.WorkingArea.Height - CurForm.Height;
            }
            CurForm.Location = new Point(x, y);
        }
        /// <summary>
        /// 激活窗体
        /// </summary>
        public void ActiveSkinForm()
        {
            ryCommon.RyForm.BringToTop(Handle);
            ryCommon.RyForm.SetActiveWindow(Handle);
        }
        /// <summary>
        /// 显示窗体
        /// </summary>
        /// <param name="type">指定要显示的窗体类型名</param>
        public static void ShowForm(Type type)
        {
            RySkins.SKinForm form = (RySkins.SKinForm)Activator.CreateInstance(type);
            form.Show();
        }
        /// <summary>
        /// 显示窗体
        /// </summary>
        /// <param name="type">指定要显示的窗体类型名</param>
        /// <param name="arg">参数</param>
        public static void ShowForm(Type type, string arg)
        {
            RySkins.SKinForm form = (RySkins.SKinForm)Activator.CreateInstance(type, new object[] { arg });
            form.Show();
        }
        /// <summary>
        /// 显示窗体
        /// </summary>
        /// <param name="type">指定要显示的窗体类型名</param>
        /// <param name="arg">参数</param>
        public static void ShowForm(Type type, string[] arg)
        {
            RySkins.SKinForm form = (RySkins.SKinForm)Activator.CreateInstance(type, arg);
            form.Show();
        }
        /// <summary>
        /// 显示模式窗体
        /// </summary>
        /// <param name="type">指定要显示的窗体类型名</param>
        /// <param name="arg">参数</param>
        public static void ShowDialogForm(Type type, string[] arg)
        {
            RySkins.SKinForm form = (RySkins.SKinForm)Activator.CreateInstance(type, arg);
            form.ShowDialog();
            form.Dispose();
        }
        /// <summary>
        /// 显示窗体,如果当前已经显示过,则激活窗体。
        /// </summary>
        /// <param name="type">指定要显示的窗体类型名</param>
        public static void ShowFormOne(Form parent, Type type)
        {
            ShowFormOne(parent, type, null);
        }
        /// <summary>
        /// 显示窗体,如果当前已经显示过,则激活窗体。
        /// </summary>
        /// <param name="type">指定要显示的窗体类型名</param>
        /// <param name="arg">参数</param>
        public static void ShowFormOne(Form parent, Type type, string[] arg)
        {
            var forms = Application.OpenForms;
            for (int i = 0; i < forms.Count; i++)
            {
                if (forms[i].GetType() == type)
                {
                    if (forms[i] is RySkins.SKinForm)
                    {
                        var skin_form = (RySkins.SKinForm)forms[i];
                        skin_form.ShowInCenter(parent);
                        skin_form.ActiveSkinForm();
                        skin_form.Show();
                        return;
                    }
                    else
                    {
                        forms[i].Show();
                    }
                }
            }
            Form form;
            if (arg == null)
            { form = (Form)Activator.CreateInstance(type); }
            else
            {
                form = (Form)Activator.CreateInstance(type, arg);
            }
            ShowInCenter(form, parent);
            ryCommon.RyForm.BringToTop(form.Handle);
            ryCommon.RyForm.SetActiveWindow(form.Handle);
            form.Show();
        }
        private void SKinForm_Load(object sender, EventArgs e)
        {
            if (!this.DesignMode)
            { base.XTheme = new ThemeForm(); }
            //Opacity = 0.97;
        }
    }
}
