using Itrycn_Project2;
using ryCommon;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;

namespace Itrycn
{
    public partial class Frmmain : Form
    {
        readonly string[] prog_args;
        public Frmmain(string[] args)
        {
            InitializeComponent();
            prog_args = args;
        }
        public TreeNode AddToTreeView(TreeNode parent, Type id, string text, string imageKey)
        {
            return AddToTreeView(parent, id, text, imageKey, "");
        }
        public TreeNode AddToTreeView(TreeNode parent, Type id, string text, string imageKey, string ToolTipText)
        {
            TreeNode btn = new TreeNode()
            {
                SelectedImageKey = "selected.ico",
                ImageKey = imageKey,
                Text = text,
                Tag = id,
                ToolTipText = ToolTipText
            };
            parent.Nodes.Add(btn);
            return btn;
        }

        readonly Dictionary<Type, Form> dict_form = new Dictionary<Type, Form>();
        /// <summary>
        /// 显示子窗体
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public Form ShowForm(Type type)
        {
            if (this.ActiveMdiChild != null)
            {
                if (this.ActiveMdiChild.GetType() == type) { return this.ActiveMdiChild; }
            }
            if (dict_form.ContainsKey(type))
            {
                dict_form[type].Show();
                dict_form[type].BringToFront();
                return dict_form[type];
            }
            else
            {
                Form frm = (Form)Activator.CreateInstance(type);
                frm.Icon = Icon;
                frm.MdiParent = this;
                frm.BackColor = Color.White;
                frm.FormBorderStyle = FormBorderStyle.None;
                frm.Dock = DockStyle.Fill;//完全填充到应用程序           
                                          //frm.WindowState = FormWindowState.Maximized;
                frm.Show();
                dict_form.Add(type, frm);
                return frm;
            }
        }
        DateTime dt_lastupdate = DateTime.Now;
        protected override void WndProc(ref System.Windows.Forms.Message msg)
        {
            switch (msg.Msg)
            {
                case 17189: //处理消息
                    {
                        #region 激活窗体
                        switch (msg.WParam.ToInt32())
                        {
                            case 100://
                                #region 显示界面
                                if ((int)msg.LParam.ToInt32() == 100)
                                {
                                    this.WindowState = FormWindowState.Normal;
                                    this.Top = (Screen.PrimaryScreen.WorkingArea.Height - this.Height) / 2;
                                    this.Left = (Screen.PrimaryScreen.WorkingArea.Width - this.Width) / 2;
                                    //RyForm.SetActiveWindow(Handle);
                                    RyForm.BringToTop(Handle);
                                } 
                                #endregion
                                break;
                        } 
                        #endregion
                    }
                    break;
                default:
                    base.WndProc(ref msg);//调用基类函数处理非自定义消息。
                    break;
            }
        }
        SysFuns.WinHotReg winHotKey;
        private void WinHotKey_OnHotkey(int HotKeyID)
        {
            switch (HotKeyID)
            {
                case 101:
                    MessageBox.Show("这是一个测试", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    break;
            }
        }
        private void Frmmain_Load(object sender, EventArgs e)
        {
            ChinaDate.GetYear(2020,2,1);
            winHotKey = new SysFuns.WinHotReg(Handle);
            winHotKey.RegHotKey(101, SysFuns.KeyModifiers.Alt, Keys.F11);
            winHotKey.OnHotkey += WinHotKey_OnHotkey;
            string txt = RyFiles.ReadAllText(@"E:\我的代码\毕方项目\C#\当游自动同步系统\Bin\Debug\AutoCutCopy\del.txt");
            //System.Drawing.Imaging.ImageCodecInfo[] encoders = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders();
            //for (int j = 0; j < encoders.Length; ++j)
            //{
            //    MessageBox.Show(encoders[j].MimeType);
            //}
            //ctMsgList1.AddItem("(" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ")", list);
            //ctMsgList1.AddItem("(" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ")", list);
            Itrycn_Info.OnLeftTreeAdd(this);
            //MessageBox.Show(ChinaDate.Get3Fu(2019, 2).ToString());
            //object mainHandle = ryCommon.Model.ryMemoryShare.ReadFromMemory(1024, typeof(string), "3H3BrowserHandle");
            //MessageBox.Show(mainHandle.ToString());
            isExpand = false;
            PicExpand_Click(picExpand,new EventArgs());
            Text = Itrycn_Info.Soft_Title+ " V" + ryCommon.RySoft.VersionStr;
            notifyIcon1.Icon = this.Icon;
            notifyIcon1.Text = Itrycn_Info.Soft_Title;
            lblTitle.Text = Itrycn_Info.Soft_Title;
            picExpand.Left = treeView1.Left + treeView1.Width + 1;
            pnlLeft.Width = picExpand.Left + picExpand.Width + 1;
            picExpand.Top = (pnlLeft.Height - picExpand.Height) / 2;
            treeView1.ExpandAll();
            this.SetBevel(false);
            dt_lastupdate = DateTime.Now;
            LiveUpdate.RyUpdate update = new LiveUpdate.RyUpdate(Itrycn_Info.Update_Url);
            update.CheckUpdate();
            treeView1.SelectedNode = treeView1.Nodes[0].Nodes[0];
            foreach (Control ctl in this.Controls)
            {
                try
                {
                    switch(ctl)
                    {
                        case MdiClient ctlMDI:
                            ctlMDI.BackColor = this.BackColor;
                            break;
                    }
                }
                catch (InvalidCastException)
                {
                }
            }
            timer1.Enabled = true;
        }

        bool isExpand = true;
        private void TreeView1_BeforeSelect(object sender, TreeViewCancelEventArgs e)
        {
            if (e.Node.Tag == null) { e.Cancel = true; return; }
            if (!Itrycn_Info.isCanCloseForm) { e.Cancel = true; return; }
        }

        private void TreeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Tag == null) { return; }
            Itrycn_Info.OnTreeClick(this, e);
        }

        private void PicExpand_Click(object sender, EventArgs e)
        {
            if (isExpand)
            {
                picExpand.Image = Itrycn_Project2.Properties.Resources.opentree;
                toolTip1.SetToolTip(picExpand,"展开列表");
                treeView1.Visible = false;
                picExpand.Left = 0;
                pnlLeft.Width = picExpand.Width;
            }
            else
            {
                picExpand.Image = Itrycn_Project2.Properties.Resources.closetree;
                toolTip1.SetToolTip(picExpand, "关闭列表");
                treeView1.Visible = true;
                picExpand.Left = treeView1.Left + treeView1.Width + 1;
                pnlLeft.Width = picExpand.Left + picExpand.Width + 1;
            }
            isExpand = !isExpand;
        }

        private void Frmmain_Resize(object sender, EventArgs e)
        {
            picExpand.Top = (pnlLeft.Height - picExpand.Height) / 2;
        }
        private void MenuHomeUrl_Click(object sender, EventArgs e)
        {
           ryCommon.RyFiles.OpenUrl(Itrycn_Info.Soft_Url);
        }

        private void BtnSetting_Click(object sender, EventArgs e)
        {
            FrmSetting frm = new FrmSetting();
            if(frm.ShowDialog()==DialogResult.OK)
            {
                Itrycn_Info.LoadSetting();
            }
        }

        private void Timer1_Tick(object sender, EventArgs e)
        {
            if (dt_lastupdate.AddMinutes(30) < DateTime.Now)
            {
                dt_lastupdate = DateTime.Now;
                LiveUpdate.RyUpdate update = new LiveUpdate.RyUpdate(Itrycn_Info.Update_Url);
                update.CheckUpdate();
            }
        }

        private void BtnAbout_Click(object sender, EventArgs e)
        {
            FrmAbout frm = new FrmAbout();
            frm.ShowDialog();
        }

        private void Frmmain_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!Itrycn_Info.isCanCloseForm)
            {
                e.Cancel = true;
                MessageBox.Show("当前还有操作正在运行,请稍后关闭软件。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void Frmmain_Shown(object sender, EventArgs e)
        {
            ryCommon.Ini ini = new Ini(Itrycn_Info.UserDataFolder + "\\Win.dat");
            ini.WriteIni(Itrycn_Info.Soft_Id, "hwnd", Handle.ToInt32());
            if (prog_args.Length >= 1)
            {
                if (prog_args[0] ==Itrycn_Info.Soft_Pram)
                    this.Hide();
            }
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void 显示主界面ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Normal;
            this.Top = (Screen.PrimaryScreen.WorkingArea.Height - this.Height) / 2;
            this.Left = (Screen.PrimaryScreen.WorkingArea.Width - this.Width) / 2;
            RyForm.Show(this);
        }

        private void ButtonEx2_Click(object sender, EventArgs e)
        {
            ryControls.Skin.SkinCommon.ButtonSkin.BackColor = Color.Red;
        }

        private void LableX1_DoubleClick(object sender, EventArgs e)
        {
            MessageBox.Show("这是一个测试", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
}
