using LineWebService;
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;
using System.ServiceModel.Web;
using System.ServiceModel;
using System.Linq.Expressions;
using BLL;

namespace WebServiceTest
{
    public partial class Form1 : Form//, IWebService_Callback
    {
        public Form1()
        {
            InitializeComponent();
        }
        private WebServiceHost _serviceHost;

        public void HostOpen()
        {
            try
            {
                ClsWebService service = new ClsWebService();
                _serviceHost = new WebServiceHost(typeof(ClsWebService));//service, new Uri(url)
                if (_serviceHost.State != CommunicationState.Opened)//判断服务是否关闭
                {
                    _serviceHost.Open();
                }

                label1.Text="Web服务已开启";
            }
            catch (Exception ex)
            {
                //AGVControl.Common.log.Error("Open", ex);
                MessageBox.Show(ex.ToString());
            }
        }

        public void HostClose()
        {
            try
            {
                if (_serviceHost.State != CommunicationState.Closed)//判断服务是否关闭
                {
                    _serviceHost.Close();//关闭服务
                }
                //_serviceHost.Close();
               label1.Text="Web服务已关闭";
            }
            catch (Exception ex)
            {
                //AGVControl.Common.log.Error("Close", ex);
                MessageBox.Show(ex.ToString());
            }

        }

        //public void GetEmptyRecycleTask(string lineName)
        //{
        //    label1.Text = lineName;
        //    MessageBox.Show(lineName);
        //}
        public void RecycleTask(string lineName)
        {
            label1.Text = lineName;
            MessageBox.Show(lineName);
            //if (AGVControl.Common.agvProductionLine.TryGetValue(lineName, out string value))
            //{
            //    AGVControl.Common.LogInfo("WebService Response OK");

            //    //加到任务
            //    //int idx = AGVControl.Common.nodeInfo.FindIndex(s => s.Name == value);
            //    //if (idx > -1)
            //    //    AGVControl.Common.linePlace.Add(value);
            //    //else
            //    //    AGVControl.Common.log.Error("CreateEmptyRecycleTask " + value + "不存在");
            //    if (!Common.AddLinePlace(value))
            //        Common.log.Error("CreateEmptyRecycleTask 节点【" + value + "】不存在");
            //    else
            //    {
            //        Common.LogInfo("任务:" + value + " 出空料架 【" + lineName + "】");
            //    }
            //    //System.IO.File.WriteAllLines(Common.CONFIG_PATH + "LinePlace.txt", Common.linePlace);
            //}
            //else
            //{
            //    AGVControl.Common.LogInfo("WebService Response false");
            //}
        }
        private void button1_Click(object sender, EventArgs e)
        {
            HostOpen();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            HostClose();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            textBox3.Text=AGVManager.CreateEmptyTask(textBox1.Text,textBox2.Text);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            AGVManager.GetWebservice(textBox1.Text, textBox2.Text);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            bool qq = AGVManager.GetRackBy(textBox1.Text, out string kk);
            textBox2.Text = qq.ToString() +" "+kk;
        }

        private void button6_Click(object sender, EventArgs e)
        {
            AGVManager.UpdateStatus("C6", "D8");
        }
    }
}