using ryCommon;
using ryCommonDb;
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 FrmTip : Form
    {
        public FrmTip()
        {
            InitializeComponent();
            ryComboBox1.Items.AddRange(new NextTipInfo[] {
                new NextTipInfo { Title = "无需推迟提醒",minute=0 },
                new NextTipInfo { Title = "推迟5分钟提醒",minute=5 },
                new NextTipInfo { Title = "推迟10分钟提醒",minute=10},
                new NextTipInfo { Title = "推迟15分钟提醒",minute=15 },
                new NextTipInfo { Title = "推迟半小时提醒",minute=30 },
                new NextTipInfo { Title = "推迟1小时提醒",minute=60 },
                new NextTipInfo { Title = "推迟到明天提醒",minute=60*24 },
            });
            isProcUse = true;
            ryComboBox1.SelectedIndex = 0;
            LblTimeStr.Text = "弹窗时间:" + DateTime.Now.ToString("yyyy年MM月dd日 HH:mm");
            _CloseMinute = Itrycn_Info.AutoCloseMinute * 60;
            _HaveCloseMinute = Itrycn_Info.AutoCloseMinute > 0;
            if (!_HaveCloseMinute) { LblCloseTime.Visible = false; }
            sound.Volume = Itrycn_Info.MusicVolume / 100f;
            isProcUse = false;
        }
        API.SoundPlay sound = new API.SoundPlay();
        public void PlaySound(string path)
        {
            string _path = ryCommon.RyFiles.GetRealPath(path);
            if (_path.IndexOf("\\") == -1) //如果只是文件名，则播放默认目录下的
            {
                _path = Itrycn_Info.SysDbFolder + @"\animation\sound\" + _path;
            }
            Thread th = new Thread(delegate () { sound.Play(_path); });
            th.Start();
        }
        private int _CloseMinute = -1;
        private bool _HaveCloseMinute = false;
        private bool isProcUse = false;
        public int Id { get; set; } = 0;
        public string RunTimeStr { get; set; } = "";
        public int IsTemp { get; set; } = 0;
        private void BtnOK_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FrmTip_Resize(object sender, EventArgs e)
        {
            panelEx1.Refresh();
        }

        private void ryComboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (isProcUse) { return; }
            NextTipInfo item = (NextTipInfo)ryComboBox1.SelectedItem;
            DataProvider mydb = new DataProvider();
            IDbInterface db = TimeClock.Itrycn_Db.CreateDataProvider(TimeClock.Itrycn_Db.dataType);
            if (db.ConnDb(TimeClock.Itrycn_Db.SQLConnStr) == 1)
            {
                if (item.minute != 0)
                {
                    Itrycn_Db.AddTip(TxtTitle.Text, labelTx1.Text, DateTime.Now.AddMinutes(item.minute), 1, RunTimeStr);
                    this.Close();
                }
            }
        }

        private void FrmTip_Load(object sender, EventArgs e)
        {
            
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (_HaveCloseMinute)
            {
                _CloseMinute--;
                if (_CloseMinute > 0)
                {
                    LblCloseTime.Text = "本界面将在 " + GetDateStr(_CloseMinute) + " 后关闭";
                }
                else
                {
                    timer1.Enabled = false;
                    this.Close();
                }
            }
            string GetDateStr(int size)
            {
                string date_str = "";
                int hour = size / 60 / 60;
                if (hour > 0) { date_str = hour + "小时"; }
                int minute = (size - hour * 60) / 60;
                if (minute > 0) { date_str += minute + "分钟"; }
                int second = (size - hour * 60 - minute * 60);
                if (second > 0) { date_str += second + "秒"; }
                return date_str;
            }
        }

        private void FrmTip_FormClosing(object sender, FormClosingEventArgs e)
        {
            sound.Stop();
        }
    }
    public class NextTipInfo
    {
        public int minute = 0;
        public string Title = "";
        public override string ToString()
        {
            return Title;
        }
    }
}
