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

namespace MeterModuleLibrary.uc
{
    public partial class ucCarFormTop : UserControl
    {
        public ucCarFormTop()
        {
            InitializeComponent();
        }

        private delegate void UpdateUIEventHander(object sender, UpdateUIArgs args); //自定义事件用来从线程中更新控件的值
        public class UpdateUIArgs : EventArgs
        {
            public string textValue { get; private set; }
            public UpdateUIArgs(string textValue)
            {
                this.textValue = textValue;
            }
        }

        private void UpdateUI_Method(object sender, UpdateUIArgs args)
        {
            if (sender is Label)
            {
                if (args.textValue == "VisibleTrue")
                {
                    ((Label)sender).Visible = true;
                }
                else if (args.textValue == "VisibleFalse")
                {
                    ((Label)sender).Visible = false;
                }
                else if (args.textValue == "ColorRed")
                    ((Label)sender).ForeColor = Color.Red;
                else
                {
                    ((Label)sender).Text = args.textValue;
                }
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            try
            {
                DateTime databaseTime = DateTime.Now;//本地时间
                string showTime = databaseTime.ToString("yyyy年MM月dd日 HH:mm:ss") + " " + System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetDayName(DateTime.Now.DayOfWeek);

                if (lblNowTime.IsHandleCreated)
                {
                    lblNowTime.Invoke(new UpdateUIEventHander(UpdateUI_Method), lblNowTime, new UpdateUIArgs(showTime));
                }
            }
            catch (Exception exp)
            {
                WriteLog("顶部样式时间显示异常！" + exp.Message);
            }
        }

        private void WriteLog(string str)
        {
            string m_szRunPath;
            m_szRunPath = System.Environment.CurrentDirectory;
            if (System.IO.Directory.Exists(m_szRunPath + "\\log") == false)
            {
                System.IO.Directory.CreateDirectory(m_szRunPath + "\\log");
            }
            string strDate = System.DateTime.Now.ToString("yyyyMMdd");
            string strPathFile = m_szRunPath + "\\log\\" + strDate;
            if (!Directory.Exists(strPathFile))//如果不存在就创建file文件夹
            {
                Directory.CreateDirectory(strPathFile);
            }
            System.IO.TextWriter tw = new System.IO.StreamWriter(strPathFile + "\\顶部样式_" + strDate + ".log", true);

            tw.WriteLine(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            tw.WriteLine(str);
            tw.WriteLine("\r\n");

            tw.Close();
        }

        private void pictureBox1_DoubleClick(object sender, EventArgs e)
        {
            fromLoginOut fo = new fromLoginOut();
            fo.ShowDialog();
        }
    }
}
