using Apache.NMS;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using PushPlatform.Common.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Push.SyncDataForm
{
    public partial class SyncDataForm : Form
    {
        List<string> messageList = new List<string>();
        bool cancelClose = true;
        public SyncDataForm()
        {
            InitializeComponent();
        }

        private void SyncDataForm_SizeChanged(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.ShowInTaskbar = false;
            }
        }

        public void Data_Handle()
        {
            AddMessage("====1.开始监听随机系统推送数据到药物系统数据=====");
            MG.Core.Mipf.MgMethod md = new MG.Core.Mipf.MgMethod();
            md.Listener += new MG.Core.Mipf.MgListenerCollection.MgListener(MipfRandomChange);
            md.StartListener("MipfQueue_EdcRandomChange");
        }

        #region 随机系统推送数据到药物系统
        void MipfRandomChange(Apache.NMS.IMessage msgInfo)
        {
            try
            {
                AddMessage("====1.开始将数据传输给药物系统====");
                string text = ((ITextMessage)msgInfo).Text;
                LogHelper.Info(text);
                JObject param = JObject.Parse(text);

                using (PushPlatform.Common.MIPFService.EdcMipfServiceSoapClient mipfBll = new PushPlatform.Common.MIPFService.EdcMipfServiceSoapClient())
                {
                    string projectId = param.GetValue<string>("projectId") != null ? param.GetValue<string>("projectId").ToString() : "";
                    string hosId = param.GetValue<string>("hosId") != null ? param.GetValue<string>("hosId").ToString() : "";
                    string groupId = param.GetValue<string>("groupId") != null ? param.GetValue<string>("groupId").ToString() : "";
                    string patientName = param.GetValue<string>("patientName") != null ? param.GetValue<string>("patientName").ToString() : "";
                    string patientNumber = param.GetValue<string>("patientNumber") != null ? param.GetValue<string>("patientNumber").ToString() : "";
                    string gender = param.GetValue<string>("gender") != null ? param.GetValue<string>("gender").ToString() : "";
                    string createUser = param.GetValue<string>("createUser") != null ? param.GetValue<string>("createUser").ToString() : "";
                    DateTime changeDate = DateTime.Now;
                    DateTime? birthday = null;
                    DateTime inDate = DateTime.Now;
                    if (DateTime.TryParse(param.GetValue<string>("birthday") != null ? param.GetValue<string>("birthday").ToString() : "", out changeDate))
                    {
                        birthday = changeDate;
                    }
                    if (DateTime.TryParse(param.GetValue<string>("inDate") != null ? param.GetValue<string>("inDate").ToString() : "", out changeDate))
                    {
                        inDate = changeDate;
                    }
                    var result = mipfBll.InsertPatient("4e6985e0-6993-47d9-877a-e266236fb4f6", "5fa5432e-0856-48ac-a06a-0d3a15000740", groupId, patientName, patientNumber, gender, createUser, birthday, inDate);
                    if(result.Result == 1)
                    {
                        AddMessage("数据处理成功");
                    }
                    else
                    {
                        AddMessage(text + "数据处理成功"+DateTime.Now.ToString());
                    }
                }
                    
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                Console.WriteLine(ex.Message);
            }
        }
        #endregion

        private void SyncDataForm_Load(object sender, EventArgs e)
        {
            this.notify.ContextMenu = new ContextMenu();
            MenuItem item = new MenuItem();
            item.Text = "退出";
            item.Click += Item_Click;
            this.notify.ContextMenu.MenuItems.Add(item);

            Thread t = new Thread(new ThreadStart(Data_Handle));
            t.IsBackground = true;
            t.Start();
        }

        private void Item_Click(object sender, EventArgs e)
        {
            cancelClose = false;
            this.Close();
        }

        private void AddMessage(string message)
        {
            if (messageList != null && messageList.Count > 500)
                messageList.RemoveAt(0);

            messageList.Add(message);

            if(this.richTb.InvokeRequired)
            {
                this.richTb.Invoke(new Action(() =>
                {
                    this.richTb.Lines = messageList.ToArray();
                    this.richTb.SelectionStart = this.richTb.Text.Length;
                    this.richTb.ScrollToCaret();
                }));
            }
            else
            {
                this.richTb.Lines = messageList.ToArray();
                this.richTb.SelectionStart = this.richTb.Text.Length;
                this.richTb.ScrollToCaret();
            }
        }

        private void SyncDataForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Hide();
            this.ShowInTaskbar = false;
            e.Cancel = cancelClose;
        }

        private void notify_DoubleClick(object sender, EventArgs e)
        {
            this.Show();
            this.ShowInTaskbar = true;
        }
    }
}
