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

namespace DayOf1440
{
    public partial class MainForm : Form
    {   
        Bitmap[] charNum=new Bitmap[10];
        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            loadChar();
            setPanelToCenter();
            // 让屏幕常亮
            SystemSleep.PreventForCurrentThread();
            timer1.Start();
        }

        private void MainForm_DoubleClick(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            updateTime();
        }

        private void updateTime()
        {
            // DateTime.Now 计算当天过去多少分钟
            DateTime now = DateTime.Now;

            int result= (int)(1440 - DateTime.Now.Subtract(DateTime.Today).TotalMinutes);
            int ge=result % 10;
            int shi = result / 10 % 10;
            int bai = result / 100 % 10;
            int qian = result / 1000 % 10;
            // 居中显示时间，加特效
            if (result > 1000)
            {
                pictureBox1.BackgroundImage = charNum[qian];
                pictureBox2.BackgroundImage = charNum[bai];
                pictureBox3.BackgroundImage = charNum[shi];
                pictureBox4.BackgroundImage = charNum[ge];
            }
            else if (result > 100) 
            {
                pictureBox1.BackgroundImage = null;
                pictureBox2.BackgroundImage = charNum[bai];
                pictureBox3.BackgroundImage = charNum[shi];
                pictureBox4.BackgroundImage = charNum[ge];
            }
            else if (result > 10)
            {
                pictureBox1.BackgroundImage = null;
                pictureBox2.BackgroundImage = charNum[shi];
                pictureBox3.BackgroundImage = charNum[ge];
                pictureBox4.BackgroundImage = null;
            } else
            {
                pictureBox1.BackgroundImage = null;
                pictureBox2.BackgroundImage = charNum[ge];
                pictureBox3.BackgroundImage = null;
                pictureBox4.BackgroundImage = null;
            }

        }

        private void loadChar()
        {
            charNum[0] = Properties.Resources._0;
            charNum[1] = Properties.Resources._1;
            charNum[2] = Properties.Resources._2;
            charNum[3] = Properties.Resources._3;
            charNum[4] = Properties.Resources._4;
            charNum[5] = Properties.Resources._5;
            charNum[6] = Properties.Resources._6;
            charNum[7] = Properties.Resources._7;
            charNum[8] = Properties.Resources._8;
            charNum[9] = Properties.Resources._9;
        }
        /// <summary>
        /// 居中
        /// </summary>
        private void  setPanelToCenter() {
            panelNum.Location = new Point((System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width-596)/2,(System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height-202)/2);
        }

        private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            // 取消常亮
            SystemSleep.RestoreForCurrentThread();
        }
    }
}
