using HelperDB;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace DrawGraph
{
    /// 
    /// 监测管理类
    /// 
    public class MonitorManage : AreaManageBase
    {
        /// 
        /// 监测文本的对象
        /// 
        TextPackObj janCePack = null;
        /// 
        /// 图表的对象
        /// 
        ChartPackObj chartPack = null;
        /// 
        /// 当前手术对象
        /// 
        private OperationRecord myOpeRecord = null;
        public MonitorManage()
        {
            init();
        }
        public MonitorManage(object _operationRecor, ZedGraphControl _zedControl, TemplateManage _template, string _name) : base(_operationRecor, _zedControl, _template, _name)
        {
            init();
        }
        public void init()
        {
            //监测文本的对象
            janCePack = GetPackObjectOTag("PhysioDataManage_TextPackObj_17") as TextPackObj;
            //图表的对象
            chartPack = GetPackObjectOTag("PhysioDataManage_ChartPackObj_6") as ChartPackObj;
            //自己要用的手术对象
            myOpeRecord = OpeRecord as OperationRecord;
        }
        #region 重写的事件
        /// 
        /// 鼠标点击画板
        /// 
        /// 
        /// 
        public override void MouseDown(ZedGraphControl sender, MouseEventArgs e)
        {
            //if (e.Button == System.Windows.Forms.MouseButtons.Left)
            //{
            //    MessageBox.Show(this.GetType().Name + "is Click Left Button");
            //}
        }
        public override void MouseMove(ZedGraphControl sender, MouseEventArgs e)
        {
        }
        public override void MouseUp(ZedGraphControl sender, MouseEventArgs e)
        {
        }
        public override void MouseDoubleClick(ZedGraphControl sender, MouseEventArgs e)
        {
        }
        public override void KeyUp(ZedGraphControl sender, KeyEventArgs e)
        {
        }
        public override void Bind()
        {
            try
            {
                init();
                List ables = PackManage.ListPob.Where(s => s is AbleEditPackObj).ToList();
                foreach (PackObjBase pack in ables)
                {
                    AbleEditPackObj ableEdit = pack as AbleEditPackObj;
                    if (ableEdit != null)
                    {
                        if (ableEdit.ControlType == EControlType.Panel)
                        {
                            ableEdit.IsVisible = true;
                            Panel pan = ableEdit.CControl as Panel;
                            if (pan != null)
                            {
                                if (pan.Controls.Count == 0)
                                {
                                    Font font = new System.Drawing.Font("微软雅黑", 7f);
                                    int pyl = 1;
                                    if (myOpeRecord.ComputerRect.Width >= 3840)
                                    {
                                        font = new System.Drawing.Font("微软雅黑", 11f);
                                    }
                                    else if (myOpeRecord.ComputerRect.Width >= 2560)
                                    {
                                        font = new System.Drawing.Font("微软雅黑", 9f);
                                    }
                                    else if (myOpeRecord.ComputerRect.Width >= 1920)
                                    {
                                        font = new System.Drawing.Font("宋体", 9f);
                                    }
                                    else if (myOpeRecord.ComputerRect.Width >= 1600)
                                    {
                                        font = new System.Drawing.Font("宋体", 6f);
                                    }
                                    else if (myOpeRecord.ComputerRect.Width >= 1024)
                                    {
                                        font = new System.Drawing.Font("宋体", 5f);
                                    }
                                    //pan.Controls.Clear();
                                    ToolTip toolTip1 = new System.Windows.Forms.ToolTip();
                                    int Width = pan.Width;
                                    int Height = pan.Height / 4;
                                    List panPanels = new List();
                                    //循环加载数值生命体征
                                    for (int x = 0; x < myOpeRecord.PhysioConfigList.Count; x++)
                                    {
                                        PhysioDataConfig ade = myOpeRecord.PhysioConfigList[x];
                                        if (ade.IsDefalultShow == false) continue;
                                        if (!ade.ShowText) continue;
                                        System.Windows.Forms.Panel panel = new System.Windows.Forms.Panel();
                                        panel.Tag = ade;
                                        panel.Dock = System.Windows.Forms.DockStyle.Top;
                                        panel.Size = new System.Drawing.Size(Width, Height);
                                        for (int i = (240 / 5); i >= 0; i--)
                                        {
                                            DateTime SpanTime = getInRoomSpanTime(myOpeRecord.PageBegin.AddMinutes(i * 5));
                                            PhysioData pdNew = PhysioDataConfig.newPhysioData(ade, myOpeRecord.Id.Value, SpanTime, -1);
                                            System.Windows.Forms.TextBox textNum = new System.Windows.Forms.TextBox();
                                            textNum.Name = pdNew.config.Id + "" + SpanTime.ToString("HHmm");
                                            textNum.Tag = pdNew;
                                            textNum.Font = font;
                                            textNum.Multiline = true;
                                            textNum.Width = Width / (240 / 5) + pyl;
                                            textNum.Height = panel.Height;
                                            textNum.Dock = System.Windows.Forms.DockStyle.Left;
                                            textNum.Leave += TextNum_Leave;
                                            textNum.DoubleClick += TextNum_DoubleClick;
                                            toolTip1.SetToolTip(textNum, SpanTime.ToString("HH:mm"));
                                            panel.Controls.Add(textNum);
                                        }
                                        panPanels.Add(panel);
                                    }
                                    for (int x = 0; x < myOpeRecord.PhysioAnesConfigList.Count; x++)
                                    {
                                        PhysioDataConfig ade = myOpeRecord.PhysioAnesConfigList[x];
                                        if (ade.IsDefalultShow == false) continue;
                                        System.Windows.Forms.Panel panel = new System.Windows.Forms.Panel();
                                        panel.Tag = ade;
                                        panel.Dock = System.Windows.Forms.DockStyle.Top;
                                        panel.Size = new System.Drawing.Size(Width, Height);
                                        for (int i = (240 / ade.YAisx); i >= 0; i--)
                                        {
                                            DateTime SpanTime = getInRoomSpanTime(myOpeRecord.PageBegin.AddMinutes(i * ade.YAisx));
                                            PhysioData pdNew = PhysioDataConfig.newPhysioData(ade, myOpeRecord.Id.Value, SpanTime, -1);
                                            //if (ade.ImgPath != null && ade.ImgPath != "")
                                            //{
                                            //    System.Windows.Forms.ComboBox textNum = new System.Windows.Forms.ComboBox();
                                            //    textNum.Items.AddRange(ade.ImgPath.Split('|'));
                                            //    textNum.Name = pdNew.config.Id + "" + SpanTime.ToString("HHmm");
                                            //    textNum.Tag = pdNew;
                                            //    textNum.Font = new System.Drawing.Font("微软雅黑", fontSize);
                                            //    textNum.Width = Width / (240 / ade.YAisx) + 1;
                                            //    textNum.Height = panel.Height;
                                            //    textNum.Dock = System.Windows.Forms.DockStyle.Left;
                                            //    textNum.DropDownStyle = ComboBoxStyle.DropDownList;
                                            //    textNum.Leave += ComeBoxNum_Leave;
                                            //    toolTip1.SetToolTip(textNum, SpanTime.ToString("HH:mm"));
                                            //    panel.Controls.Add(textNum);
                                            //} 
                                            System.Windows.Forms.TextBox textNum = new System.Windows.Forms.TextBox();
                                            textNum.Name = pdNew.config.Id + "" + SpanTime.ToString("HHmm");
                                            textNum.Tag = pdNew;
                                            textNum.Font = font;
                                            textNum.Multiline = true;
                                            textNum.Width = Width / (240 / ade.YAisx) + pyl;
                                            textNum.Height = panel.Height;
                                            textNum.Dock = System.Windows.Forms.DockStyle.Left;
                                            textNum.Leave += TextNum_Leave;
                                            textNum.DoubleClick += TextNum_DoubleClick;
                                            toolTip1.SetToolTip(textNum, SpanTime.ToString("HH:mm"));
                                            panel.Controls.Add(textNum);
                                        }
                                        panPanels.Add(panel);
                                    }
                                    System.Windows.Forms.Panel wmpanel = new System.Windows.Forms.Panel();
                                    wmpanel.Size = pan.Size;
                                    wmpanel.Dock = DockStyle.Fill;
                                    for (int i = 3; i >= 0; i--)
                                    {
                                        if (i > panPanels.Count - 1)
                                        {
                                            System.Windows.Forms.Panel panel = new System.Windows.Forms.Panel();
                                            panel.Tag = null;
                                            panel.Dock = System.Windows.Forms.DockStyle.Top;
                                            panel.Size = new System.Drawing.Size(Width, Height);
                                            for (int j = (240 / 5); j >= 0; j--)
                                            {
                                                DateTime SpanTime = getInRoomSpanTime(myOpeRecord.PageBegin.AddMinutes(j * 5));
                                                System.Windows.Forms.TextBox textNum = new System.Windows.Forms.TextBox();
                                                textNum.Font = font;
                                                textNum.Multiline = true;
                                                textNum.Width = Width / (240 / 5) + pyl;
                                                textNum.ReadOnly = true;
                                                textNum.Height = panel.Height;
                                                textNum.Dock = System.Windows.Forms.DockStyle.Left;
                                                toolTip1.SetToolTip(textNum, SpanTime.ToString("HH:mm"));
                                                panel.Controls.Add(textNum);
                                            }
                                            panPanels.Add(panel);
                                        };
                                        wmpanel.Controls.Add(panPanels[i]);
                                    }
                                    pan.Controls.Clear();
                                    pan.Controls.Add(wmpanel);
                                }
                            }
                        }
                    }
                }
                DrawPhysioData();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// 
        /// 画生理数据
        /// 
        private void DrawPhysioData()
        {
            DataTable dtPhysioData = getByOpeIDIsPhysioData(myOpeRecord.Id.Value);
            if (dtPhysioData.Rows.Count <= 0)
            {
                foreach (PhysioDataConfig item in myOpeRecord.PhysioConfigList)
                {
                    item.clearAddObj(ZedControl);
                }
                foreach (PhysioDataConfig item in myOpeRecord.PhysioAnesConfigList)
                {
                    item.clearAddObj(ZedControl);
                }
                return;
            }
            if (dtPhysioData != null && dtPhysioData.Rows.Count > 0)
            {
                foreach (PhysioDataConfig addPP in myOpeRecord.PhysioConfigList)
                {
                    addPP.clearAddObj(ZedControl);
                    if (addPP.IsDefalultShow == false) continue;
                    for (int i = 0; i < dtPhysioData.Rows.Count; i++)
                    {
                        if (dtPhysioData.Rows[i]["Name"].ToString().Equals(addPP.Name.ToString()))
                        {
                            PhysioData pdTemp = PhysioDataConfig.newPhysioData(addPP, myOpeRecord.Id.Value, DateTime.Parse(dtPhysioData.Rows[i]["RecordTime"].ToString()), dtPhysioData.Rows[i]["Value"].ToString());
                            addPP.AddPointItem(pdTemp, false);
                        }
                    }
                }
                foreach (PhysioDataConfig addPP in myOpeRecord.PhysioAnesConfigList)
                {
                    addPP.clearAddObj(ZedControl);
                    if (addPP.IsDefalultShow == false) continue;
                    for (int i = 0; i < dtPhysioData.Rows.Count; i++)
                    {
                        if (dtPhysioData.Rows[i]["Name"].ToString().Equals(addPP.Name.ToString()))
                        {
                            PhysioData pdTemp = PhysioDataConfig.newPhysioData(addPP, myOpeRecord.Id.Value, DateTime.Parse(dtPhysioData.Rows[i]["RecordTime"].ToString()), dtPhysioData.Rows[i]["Value"].ToString());
                            addPP.AddText(pdTemp);
                        }
                    }
                }
            }
        }
        public DateTime getInRoomSpanTime(DateTime time)
        {
            DateTime resultTime = time;
            //for (int i = (240 / 5); i >= 0; i--)
            //{
            //    DateTime spanTime = myOpeRecord.InRoomTime.Value.AddMinutes(i * 5);
            //    if (spanTime >= resultTime && spanTime < resultTime.AddMinutes(5))
            //    {
            //        resultTime = spanTime;
            //        break;
            //    } 
            //}
            return resultTime;
        }
        public string TempValue;
        private void TextNum_Leave(object sender, EventArgs e)
        {
            TextBox txtNum = sender as TextBox;
            PhysioData pdNew = txtNum.Tag as PhysioData;
            if (txtNum.Text.Trim() == "" && pdNew.Value > -1)
            {
                if (pdNew.config.ConfigType == "麻醉体征")
                    pdNew.config.EditDelItem(pdNew);
                else
                    pdNew.config.DelItem(pdNew);
                pdNew.Value = -1;
                txtNum.Tag = pdNew;
                TempValue = "";
            }
            else if (txtNum.Text.Trim() != "" && pdNew.Value > -1)
            {
                if (pdNew.Value.ToString() != txtNum.Text)
                {
                    string value = checkValue(txtNum, pdNew);
                    if (value != "")
                    {
                        pdNew.ValueString = value;
                        if (pdNew.config.ConfigType == "麻醉体征")
                            pdNew.config.EditModifyItem(pdNew, pdNew);
                        else
                            pdNew.config.ModifyItem(pdNew, pdNew);
                        txtNum.Tag = pdNew;
                        TempValue = txtNum.Text.Trim();
                    }
                }
            }
            else if (txtNum.Text.Trim() != "")
            {
                string value = checkValue(txtNum, pdNew);
                if (value != "")
                {
                    pdNew.ValueString = value;
                    if (pdNew.config.ConfigType == "麻醉体征")
                        pdNew.config.EditAddItem(pdNew);
                    else
                        pdNew.config.AddPointItem(pdNew);
                    txtNum.Tag = pdNew;
                    TempValue = txtNum.Text.Trim();
                }
            }
        }
        private void ComeBoxNum_Leave(object sender, EventArgs e)
        {
            ComboBox txtNum = sender as ComboBox;
            PhysioData pdNew = txtNum.Tag as PhysioData;
            if (txtNum.Text.Trim() == "" && pdNew.Value > -1)
            {
                pdNew.config.EditDelItem(pdNew);
                pdNew.Value = -1;
                txtNum.Tag = pdNew;
                TempValue = "";
            }
            else if (txtNum.Text.Trim() != "" && pdNew.Value > -1)
            {
                if (pdNew.Value.ToString() != txtNum.Text)
                {
                    pdNew.ValueString = txtNum.Text.Trim();
                    pdNew.config.EditModifyItem(pdNew, pdNew);
                    txtNum.Tag = pdNew;
                    TempValue = txtNum.Text.Trim();
                }
            }
            else if (txtNum.Text.Trim() != "")
            {
                pdNew.ValueString = txtNum.Text.Trim();
                pdNew.config.EditAddItem(pdNew);
                txtNum.Tag = pdNew;
                TempValue = txtNum.Text.Trim();
            }
        }
        public string checkValue(TextBox txtNum, PhysioData pdNew)
        {
            string Result = "";
            double value = -1;
            if (pdNew.config.ConfigType == "生命体征" || pdNew.config.ShowText == true)
            {
                if (!double.TryParse(txtNum.Text.ToString(), out value))
                {
                    MessageBox.Show("请填写正确数值!", "系统提示");
                    txtNum.Focus();
                }
                else
                {
                    if (value < pdNew.config.WarningLowLimit || value > pdNew.config.WarningHighLimit)
                    {
                        DialogResult dr = MessageBox.Show("数值:" + value + " 超出 " + pdNew.config.Name + "[" + pdNew.config.WarningLowLimit + "-" + pdNew.config.WarningHighLimit + "] 报警范围! ", "系统提示");//, MessageBoxButtons.YesNo); //if (dr != DialogResult.Yes) { txtNum.Focus(); }
                        if (value < pdNew.config.WarningLowLimit)
                        {
                            value = pdNew.config.WarningLowLimit;
                        }
                        if (value > pdNew.config.WarningHighLimit)
                        {
                            value = pdNew.config.WarningHighLimit;
                        }
                    }
                }
                if (value > 0) Result = value.ToString();
            }
            else
            {
                Result = txtNum.Text.ToString();
            }
            return Result;
        }
        private void TextNum_DoubleClick(object sender, EventArgs e)
        {
            TextBox txtNum = sender as TextBox;
            if (TempValue != "")
            {
                txtNum.Text = TempValue;
            }
        }
        #endregion 重写的事件结束
        public static DataTable getByOpeIDIsPhysioData(int operationId)
        {
            string sqlStr = "SELECT [PhysioDataConfig].Name , PhysioData.RecordTime, PhysioData.[Value]   FROM [dbo].[PhysioDataConfig] INNER JOIN  PhysioData ON [PhysioDataConfig].Id = PhysioData.PhysioDataConfigId  where PatientId = " + operationId + " order by PhysioDataConfigId,PhysioData.RecordTime ASC";
            return DBHelper.GetDataTable(sqlStr);
        }
        public double getYPositionByListIndex(double index, double yTop, double yBottom, int rowCount)
        {
            double heightT = ZedControl.Height * (yBottom - yTop);
            double setpTemp = heightT / rowCount;
            //求一格在实际高度中的百分比
            double bfb = (setpTemp / heightT);
            //两线之间度*百分比得到一格的百分比高度
            double ygBFB = (yBottom - yTop) * bfb;
            double y = ygBFB * index;
            return y;
        }
    }
}