using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace TimeClock.DbOp
{
    public partial class FrmRest : Form
    {
        public FrmRest()
        {
            InitializeComponent();
            dt_rest_end = DateTime.Now.AddMinutes(Itrycn_Info.Rest);
            LblTime.Text = ryCommon.RyDate.DateDiff(dt_rest_end);
        }
        /// <summary>
        /// 仅仅作为多显示器支持,不提供关闭等操作
        /// </summary>
        bool IsOnlyShow = false;
        public FrmRest(DateTime dt,bool _IsOnlyShow)
        {
            InitializeComponent();
            dt_rest_end = dt;
            LblTime.Text = ryCommon.RyDate.DateDiff(dt_rest_end);
            IsOnlyShow = _IsOnlyShow;
            Opacity = Itrycn_Info.RestOpacity/100d;
        }
        DateTime dt_rest_end = DateTime.Now;
        API.SoundPlay sound = null;
        private void FrmRest_Load(object sender, EventArgs e)
        {
            LblTime.Location = new Point((Width-LblTime.Width)/2,(Height-LblTime.Height)/2);
            label1.Left = (Width - label1.Width) / 2;
            timer1.Start();
            if(!IsOnlyShow)
            {
                API.Hook.Hook_Start();
                if (Itrycn_Info.RestMusic_On)
                {
                    sound = new API.SoundPlay
                    {
                        Volume = Itrycn_Info.MusicVolume / 100f
                    };
                    Thread th = new Thread(PlayMusic);
                    th.Start();
                }
            }
        }
        bool IsExit = false;
        private void PlayMusic()
        {
            if(Itrycn_Info.MusicType==0)
            {
                if (!System.IO.File.Exists(Itrycn_Info.MusicPath)) { return; }
                while (true)
                {
                    if (IsExit) { break; }
                    sound.Play(Itrycn_Info.MusicPath);
                }
            }
            else if (Itrycn_Info.MusicType == 1)
            {
                if (!System.IO.Directory.Exists(Itrycn_Info.MusicPath)) { return; }
                string[] files = System.IO.Directory.GetFiles(Itrycn_Info.MusicPath,"*.mp3");
                if(files==null || files.Length < 1) { return; }
                while (true)
                {
                    if (IsExit) { break; }
                    Random rd = new Random();
                    sound.Play(files[rd.Next(0, files.Length-1)]);
                }
            }
        }
        int time_index = 60;
        private void Timer1_Tick(object sender, EventArgs e)
        {
            LblNowTime.Text = DateTime.Now.ToString("HH:mm:ss");
            if (Itrycn_Info.RestMode == 0)
            {
                time_index--;
                if (time_index == 0)
                {
                    pictureBox1.Visible = true;
                }
            }
            if (dt_rest_end<=DateTime.Now)
            {
                IsExit = true;
                if (sound != null) { sound.Stop(); }
                timer1.Stop();
                if (!IsOnlyShow)
                {
                    Itrycn_Info.InitRest();
                }
                this.Close();
            }
            else
            {
                if(WinAPI.User32.GetForegroundWindow()!=Handle)
                {
                    ryCommon.RyForm.SetTopMost(Handle,true);
                }
                LblTime.Text = ryCommon.RyDate.DateDiff(dt_rest_end);
            }
            if(!Itrycn_Info.IsRestNowing)
            {
                IsExit = true;
                if (sound != null) { sound.Stop(); }
                timer1.Stop();
                this.Close();
            }
        }

        private void FrmRest_FormClosing(object sender, FormClosingEventArgs e)
        {
            IsExit = true;
            if (!IsOnlyShow)
            {
                Itrycn_Info.IsRestNowing = false;
                API.Hook.Hook_Clear();
            }
        }

        private void FrmRest_KeyDown(object sender, KeyEventArgs e)
        {
            if ((e.KeyCode == Keys.F4) && (e.Alt == true))  //屏蔽ALT+F4
            {
                e.Handled = true;
            }
        }

        private void PictureBox1_Click(object sender, EventArgs e)
        {
            IsExit = true;
            if (sound != null) { sound.Stop(); }
            timer1.Stop();
            Itrycn_Info.InitRest();
            this.Close();
        }

        private void FrmRest_Resize(object sender, EventArgs e)
        {
            label1.Left = (Width - label1.Width) / 2;
        }
    }
}
