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

namespace QMTask.Client
{
    public partial class MainForm : Form
    {
        //任务调度服务
        TaskService.TaskServiceClient tsc = ServiceUtil.GetTaskService();

        public MainForm()
        {
            InitializeComponent();
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            PlanListForm plf = new PlanListForm();
            plf.ShowDialog();
        }
        private void MainForm_Load(object sender, EventArgs e)
        {
            try
            {
                bool serverStatus = tsc.GetServerStatus();

                if (serverStatus == true)
                {
                    this.toolStripButton1.Enabled = false;
                    this.toolStripButton2.Enabled = true;
                }
                else
                {
                    this.toolStripButton1.Enabled = true;
                    this.toolStripButton2.Enabled = false;
                }

                this.timer1.Enabled = true;
            }
            catch(Exception ex)
            {
                MessageBox.Show("连接任务调度服务失败 - " + ex.Message);
            }
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            try
            {
                tsc.Start();
                this.toolStripButton1.Enabled = false;
                this.toolStripButton2.Enabled = true;
                this.textBox1.Text += string.Format("时间:{0}\r\n", DateTime.Now.ToString());
                this.textBox1.Text += string.Format("启动服务\r\n");
                this.timer1.Enabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("远程服务不可用 - "+ex.Message);
            }
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            try
            {
                tsc.Stop();
                this.toolStripButton2.Enabled = false;
                this.toolStripButton1.Enabled = true;
                this.textBox1.Text += string.Format("时间:{0}\r\n", DateTime.Now.ToString());
                this.textBox1.Text += string.Format("停止服务\r\n");
                this.timer1.Enabled = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show("远程服务不可用 - " + ex.Message);
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            try
            {
                bool serverStatus = tsc.GetServerStatus();

                if (serverStatus == true)
                {
                    this.toolStripButton1.Enabled = false;
                    this.toolStripButton2.Enabled = true;
                }
                else
                {
                    this.toolStripButton1.Enabled = true;
                    this.toolStripButton2.Enabled = false;
                }
            }
            catch (Exception ex)
            {
                this.textBox1.Text += string.Format("时间:{0}\r\n", DateTime.Now.ToString());
                this.textBox1.Text += string.Format("服务异常停止\r\n");
                this.toolStripButton1.Enabled = true;
                this.toolStripButton2.Enabled = false;
                this.timer1.Enabled = false;
            }
        }

        private void tsbMonitor_Click(object sender, EventArgs e)
        {
            PlanMonitorForm form = new PlanMonitorForm();
            form.ShowDialog();
        }

        private void cbSpeedWarn_CheckedChanged(object sender, EventArgs e)
        {
            ServiceUtil.SoundWarn = this.cbSpeedWarn.Checked;
        }
    }
}