using DrawGraph;
using DrawGraph.AreaManage;
using DrawGraph.BoardPack;
using DrawGraph.GUtil;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace AIMS.EF
{
    public class IconManage : AreaManageBase
    {
        public RectangleFramePackObj IconPpack;
        public LinePackObj H5pack;
        /// 
        /// 当前手术对象
        /// 
        private OperationRecord myOpeRecord = null;
        private XmlUtil xmlOpe = new XmlUtil("");
        private string EVENTPARAMPATH = "main/eventParams/eventParam";
        public IconManage() { }
        public IconManage(object _operationRecor, ZedGraphControl _zedControl, TemplateManage _template, string _name) : base(_operationRecor, _zedControl, _template, _name)
        {
            init();
        }
        public void init()
        {
            //自己要用的手术对象
            myOpeRecord = OpeRecord as OperationRecord;
        }
        /// 
        /// 初始画的后置方法
        /// 
        public override void FollowUpMethod()
        {
            H5pack = template.GetPackObjectOTag("IconManage_LinePackObj_5");
            IconPpack = template.GetPackObjectOTag("IconManage_RectangleFramePackObj_2");
        }
        #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)
        {
        }
        #endregion 重写的事件结束
        public override void Bind()
        {
            init();
            //DrawEvent();
            //MessageBox.Show("RemarkManage重新绑定");
        }
         
        #region 格式化字符串长度
        /// 
        /// 格式化字符串长度
        /// 
        /// 输入的字符串
        /// 截取的长度
        /// 被截取完的字符串
        public static List stringformat(string str, int n)
        {
            ///
            ///格式化字符串长度,超出部分显示省略号,区分汉字跟字母。汉字2个字节,字母数字一个字节
            ///
            List strList = new List();
            string temp = string.Empty;
            if (System.Text.Encoding.Default.GetByteCount(str) <= n)//如果长度比需要的长度n小,返回原字符串
            {
                strList.Add(str);
            }
            else
            {
                int t = 0;
                char[] q = str.ToCharArray();
                for (int i = 0; i <= q.Length; i++)
                {
                    if (t > n)
                    {
                        strList.Add(temp);
                        temp = "";
                        t = 0;
                    }
                    if (i == q.Length)// - 1
                    {
                        strList.Add(temp);
                        break;
                    }
                    //判断是否汉字:
                    if ((int)q[i] >= 0x4E00 && (int)q[i] <= 0x9FA5)
                    {
                        temp += q[i];
                        t += 2;
                    }
                    //判断半角如下: 
                    else if (q[i].ToString().Length == Encoding.Default.GetByteCount(q[i].ToString()))
                    {
                        temp += q[i];
                        t++;
                    }
                    //判断全角如下: 
                    else if (2 * q[i].ToString().Length == Encoding.Default.GetByteCount(q[i].ToString()))
                    {
                        temp += q[i];
                        t += 2;
                    }
                    else
                    {
                        temp += q[i];
                        t++;
                    }
                }
            }
            return strList;
        }
        #endregion
        public bool isCgtime(string key, DateTime time)
        {
            bool b = false;
            try
            {
                if (key == "自主呼吸")
                {
                    if (myOpeRecord.InCGTime != null && myOpeRecord.OutCGTime != null)
                    {
                        if (time > myOpeRecord.InCGTime && time < myOpeRecord.OutCGTime)
                            b = true;
                    }
                    if (myOpeRecord.InCGTime != null && myOpeRecord.OutCGTime == null)
                    {
                        if (time > myOpeRecord.InCGTime)
                            b = true;
                    }
                }
                else
                {
                    b = false;
                }
            }
            catch (Exception ex)
            {
                throw;
            }
            return b;
        }
        private bool IfInTimeExist(DateTime Begin, DateTime End)
        {
            DateTime lastime = myOpeRecord.lastPageBegin.AddSeconds(59);
            bool b = false;
            if (End == null)
            {
                if (Begin > myOpeRecord.pageBegin && Begin < lastime)
                {
                    b = true;
                }
            }
            else
            {
                if (Begin >= myOpeRecord.pageBegin && Begin <= lastime)
                {
                    b = true;
                }
                else if (End >= myOpeRecord.pageBegin && End <= lastime)
                {
                    b = true;
                }
                else if (Begin < myOpeRecord.pageBegin && End > lastime)
                {
                    b = true;
                }
            }
            return b;
        }
        private DateTime GetInsertTime(DateTime pdTemp, bool IsInRToomTime = false)
        {
            DateTime insTime = pdTemp.Date.AddHours(pdTemp.Hour).AddMinutes(pdTemp.Minute);
            if (collectInterval == 5)
            {
                int minute = pdTemp.Minute % 5;//计算5的整数分钟
                if (minute >= 3)
                    insTime = insTime.AddMinutes(5 - minute);
                else if (minute != 0 && IsInRToomTime == true)
                    insTime = insTime.AddMinutes(5 - minute);
                else
                    insTime = insTime.AddMinutes(0 - minute);
            }
            else if (collectInterval == 10)
            {
                double minute = pdTemp.Minute % 10;//计算5的整数分钟
                if (minute >= 5)
                    insTime = insTime.AddMinutes(10 - minute);
                else
                    insTime = insTime.AddMinutes(0 - minute);
            }
            return insTime;
        } 
        /// 
        /// 根据顶部加药序号,确定数值生命体征的纵向位置(以1为单位)
        /// 
        /// 
        /// 
        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;
        }
        /// 
        /// 根据生理参数名称获取对应的目标值
        /// 
        /// 生理参数XML路径
        /// 生理参数名称
        /// 目标生理参数项目
        /// 生理参数项的值
        private string getSiblingName(string strPath, string typeValue, string targetName)
        {
            string result = "";
            try
            {
                result = xmlOpe.GetNode("name", typeValue, strPath, targetName);
            }
            catch (Exception)
            {
                throw new Exception("读取XML发生错误");
            }
            return result;
        }
    }
}