using NAudio.Wave;
using ryCommon;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using TimeClock.DbOp;

namespace MyTimeClock
{
    public partial class Frmmain : Form
    {
        public Frmmain(string[] args)
        {
            InitializeComponent();
        }
        protected override void WndProc(ref System.Windows.Forms.Message msg)
        {
            switch (msg.Msg)
            {
                case 17189: //处理消息
                    {
                        #region 激活窗体
                        switch (msg.WParam.ToInt32())
                        {
                            case 100://对象添加
                                if ((int)msg.LParam.ToInt32() == 100)
                                {
                                    ryTimeClock1.ShowMsgManage();
                                }
                                break;
                        }
                        #endregion
                    }
                    break;
                default:
                    base.WndProc(ref msg);//调用基类函数处理非自定义消息。
                    break;
            }
        }
        private void ryTimeClock1_OnTimeTip(object sender, TimeClock.TimeInfo e)
        {
           // e.Cancel = true;
            //MessageBox.Show(e.MsgText);
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            TimeClock.Itrycn_Info.SysDbFolder = Application.StartupPath + "\\SysDb";
            TimeClock.Itrycn_Info.UserDbFolder = Application.StartupPath + "\\UserDb";
            TimeClock.DbOp.Custom.WorkQuickTip = false;
            TimeClock.DbOp.Custom.AppTitle = "睿元定时提醒专家";
            TimeClock.DbOp.Custom.DonateVisabled = true;
            捐助我们ToolStripMenuItem.Visible = TimeClock.DbOp.Custom.DonateVisabled;
            //节假日设置ToolStripMenuItem.Visible = false;
            LblVer.Text = "V"+RySoft.VersionStr;
            Text = TimeClock.DbOp.Custom.AppTitle;
            LblName.Text = TimeClock.DbOp.Custom.AppTitle;
            LiveUpdate.RyUpdate update = new LiveUpdate.RyUpdate(Itrycn_Info.Update_Url);
            update.CheckUpdate();
            timer1.Enabled = true;
            ryTimeClock1.LoadSticky();
            ryTimeClock1.OnRestDataChanged += RyTimeClock1_OnRestDataChanged;
        }

        private void RyTimeClock1_OnRestDataChanged(object sender, EventArgs e)
        {
            var forms = Application.OpenForms;
            foreach (Form form in forms)
            {
                if (form is FrmTimeView frm)
                {
                    frm.LoadRestRecord();
                    break;
                }
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {

        }

        private void 显示主窗体ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ryTimeClock1.ShowMsgManage();
        }

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

        private void Form1_Shown(object sender, EventArgs e)
        {
            ryCommon.Ini ini = new Ini(Itrycn_Info.UserDataFolder + "\\Win.dat");
            ini.WriteIni(Itrycn_Info.Soft_Id, "hwnd", Handle.ToInt32());
        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            ryTimeClock1.ShowMsgManage();
        }

        private void Timer1_Tick(object sender, EventArgs e)
        {
            timer1.Stop();
            LblState.Text = "正在加载报时声音配置文件...";Application.DoEvents();
            TimeClock.Itrycn_Info.AddSoundToDb();
            LblState.Text = "正在启动..."; Application.DoEvents();
            ryTimeClock1.Enabled = true;
            this.Hide();
        }

        private void 设置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ryTimeClock1.ShowSetting();
        }

        private void 捐助我们ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TimeClock.FrmAbout frm = new TimeClock.FrmAbout();
            frm.StartPosition = FormStartPosition.CenterScreen;
            frm.ShowDialog();
        }

        private void 节假日设置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TimeClock.DbOp.FrmHolidayView frm = new TimeClock.DbOp.FrmHolidayView();
            RyForm.ShowOne(frm);
        }
    }
}
