using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ServiceProcess;
using System.Diagnostics;

namespace PumpDataUI
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        //安装
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string CurrentDirectory = System.Environment.CurrentDirectory;
                System.Environment.CurrentDirectory = CurrentDirectory + "\\Service";
                Process process = new Process();
                process.StartInfo.UseShellExecute = false;
                process.StartInfo.FileName = "Install.bat";
                process.StartInfo.CreateNoWindow = true;
                process.Start();
                System.Environment.CurrentDirectory = CurrentDirectory;
                MessageBox.Show("安装成功");
            }
            catch (Exception ex)
            {
                MessageBox.Show("安装失败：" + ex.ToString());
            }
        }

        //卸载
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            try
            {
                string CurrentDirectory = System.Environment.CurrentDirectory;
                System.Environment.CurrentDirectory = CurrentDirectory + "\\Service";
                Process process = new Process();
                process.StartInfo.UseShellExecute = false;
                process.StartInfo.FileName = "Uninstall.bat";
                process.StartInfo.CreateNoWindow = true;
                process.Start();
                System.Environment.CurrentDirectory = CurrentDirectory;
                MessageBox.Show("卸载成功");
            }
            catch (Exception ex)
            {
                MessageBox.Show("卸载失败：" + ex.ToString());
            }
        }

        //启动
        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            try
            {
                ServiceController serviceController = new ServiceController("DinghePumpDataService");
                if (serviceController.Status != ServiceControllerStatus.Running)
                    serviceController.Start();
                MessageBox.Show("启动成功");
            }
            catch (Exception ex)
            {
                MessageBox.Show("启动失败：" + ex.ToString());
            }
        }

        //停止
        private void Button_Click_3(object sender, RoutedEventArgs e)
        {
            try
            {
                ServiceController serviceController = new ServiceController("DinghePumpDataService");
                if (serviceController.CanStop)
                    serviceController.Stop();
                MessageBox.Show("停止成功");
            }
            catch (Exception ex)
            {
                MessageBox.Show("停止失败：" + ex.ToString());
            }
        }
        
        //暂停/继续
        private void Button_Click_4(object sender, RoutedEventArgs e)
        {
            try
            {
                ServiceController serviceController = new ServiceController("DinghePumpDataService");
                if (serviceController.CanPauseAndContinue)
                {
                    if (serviceController.Status == ServiceControllerStatus.Running)
                        serviceController.Pause();
                    else if (serviceController.Status == ServiceControllerStatus.Paused)
                        serviceController.Continue();
                    MessageBox.Show("暂停/继续成功");
                }
                else
                {
                    MessageBox.Show("服务不支持暂停/继续");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("暂停/继续失败：" + ex.ToString());
            }

        }

        //检查状态
        private void Button_Click_5(object sender, RoutedEventArgs e)
        {
            //TimeSpan ts1 = new TimeSpan(DateTime.Now.Ticks);
            //TimeSpan ts2 = new TimeSpan(DateTime.Today.Ticks);
            //TimeSpan ts = ts1.Subtract(ts2).Duration();
            //bool istoday = ts.Days == 0;

            //string s1 = "3:15";
            //string s2 = "1:40";
            ////int a = s1.IndexOf(s2);
            //int a = string.Compare(s1, s2);

            ServiceController serviceController = new ServiceController("DinghePumpDataService");
            string status = serviceController.Status.ToString();
            MessageBox.Show(status);

            //string sql = string.Format("select * from vdncallrecord where s_date>='{0}' and s_date<'{1}'", DateTime.Now.ToString("yyyy-MM-dd"), DateTime.Today.AddDays(1).ToString("yyyy-MM-dd"));
            //DateTime dt = GetWeekFirstDayMon(DateTime.Now);

            //string kk = "1234";
            //string bb = kk.Substring(0, kk.Length - 1);

        }

        #region 测试
        /// <summary>
        /// 得到本周第一天(以星期一为第一天)
        /// </summary>
        /// <param name="datetime"></param>
        /// <returns></returns>
        private DateTime GetWeekFirstDayMon(DateTime datetime)
        {
            //星期一为第一天
            int weeknow = Convert.ToInt32(datetime.DayOfWeek);

            //因为是以星期一为第一天，所以要判断weeknow等于0时，要向前推6天。
            weeknow = (weeknow == 0 ? (7 - 1) : (weeknow - 1));
            int daydiff = (-1) * weeknow;

            //本周第一天
            string FirstDay = datetime.AddDays(daydiff).ToString("yyyy-MM-dd");
            return Convert.ToDateTime(FirstDay);
        }
        #endregion
    }
}
