using System;
using System.Collections.Generic;
using System.Text; 
using System.Windows.Forms;
using System.Drawing;
namespace DrawGraph
{
    public class EventObj
    {
        //事件对象向到麻醉单上的对象名前缀
        public enum TextPrefix
        {
            /// 
            /// 用量
            /// 
            DD,
            /// 
            /// 箭头
            /// 
            AR,
            /// 
            /// 事件对象名,如药名,液体名
            /// 
            DN,
            /// 
            /// 单位
            /// 
            DU,
            /// 
            /// 总剂量
            /// 
            DT,
            /// 
            /// 图片
            /// 
            IM,
            /// 
            /// 麻醉单曲线上的点
            /// 
            PI,
            /// 
            /// 可编辑区域
            /// 
            ED,
            /// 
            /// 长方形
            /// 
            BT,
        }
        /// 
        /// 麻醉单窗体
        /// 
        public ZedGraphControl zgcAnas = null;
        /// 
        /// 中间麻醉图左边界
        /// 
        protected double leftColRatio;
        /// 
        /// 中间麻醉图右边界
        /// 
        protected double rightColRatio;
        /// 
        /// 麻醉单开始时间
        /// 
        protected DateTime pageBegin;
        /// 
        /// 麻醉单结束时间
        /// 
        protected DateTime pageEnd;
        private DateTime begin;
        private bool isSelected = false;
        /// 
        /// 是否被选中
        /// 
        [NoCreatControlAttributs]
        public bool IsSelected
        {
            get { return isSelected; }
            set { isSelected = value; }
        }
        /// 
        /// 加对象的开始时间
        /// 
        [NoCreatControlAttributs]
        public DateTime Begin
        {
            get { return begin; }
            set { begin = value; }
        }
        private DateTime end;
        /// 
        /// 加对象的结束时间
        /// 
        [NoCreatControlAttributs]
        public DateTime End
        {
            get { return end; }
            set { end = value; }
        }
        /// 
        /// 设置麻醉单的基本参数
        /// 
        /// 麻醉单
        /// 麻醉单中部图的左边界
        /// 麻醉单中部图的右边界
        /// 页面的开始时间
        /// 页面的结束时间
        public void setAnasArr(ZedGraphControl zgc, double leftRatio, double rightRatio, DateTime pageBegin, DateTime pageEnd)
        {
            this.zgcAnas = zgc;
            this.leftColRatio = leftRatio;
            this.rightColRatio = rightRatio;
            this.pageBegin = pageBegin;
            this.pageEnd = pageEnd;
        }
        #region 基类为子类提供的基本方法
        /// 
        /// 删除文本对象
        /// 
        /// 标记名
        public void delAddObj(string tagName)
        {
            try
            {
                TextObj text = (TextObj)zgcAnas.MasterPane.GraphObjList[tagName];
                if (text != null)
                    zgcAnas.MasterPane.GraphObjList.Remove(text);
            }
            catch (Exception)
            {
            }
        }
        /// 
        /// 删除控件里对象
        /// 
        /// 标记名
        protected void delLineObj(string tagName)
        {
            GraphObj text = zgcAnas.MasterPane.GraphObjList[tagName];
            if (text != null) zgcAnas.MasterPane.GraphObjList.Remove(text);
        }
        /// 
        /// 删除文本上的箭头
        /// 
        /// 标记名
        protected void delAddArrows(string tagName)
        {
            try
            {
                if (zgcAnas != null && zgcAnas.MasterPane != null)
                {
                    ArrowObj arrow = (ArrowObj)zgcAnas.MasterPane.GraphObjList[tagName];
                    if (arrow != null) zgcAnas.MasterPane.GraphObjList.Remove(arrow);
                }
            }
            catch (Exception  )
            {
                throw;
            }
        }
        /// 
        /// 删除图片
        /// 
        /// 标记名
        protected void delAddImg(string tagName)
        {
            GraphObj gos = zgcAnas.MasterPane.GraphObjList[tagName];
            if (gos != null)
            {
                zgcAnas.MasterPane.GraphObjList.Remove(gos);
            }
        }
        protected void delADDCurve(string tagName)
        {
            if (zgcAnas.GraphPane.CurveList == null) return;
            foreach (LineItem li in zgcAnas.GraphPane.CurveList)
            {
                if (tagName.Equals(li.Tag.ToString()))
                {
                    li.Clear();
                    zgcAnas.GraphPane.CurveList.Remove(li);
                    break;
                }
            }
        }
        /// 
        /// 根据所给时间值计算其中在ZedGraph图中x的CoordType.PaneFraction浮点坐标
        /// 
        /// 日期时间
        /// x
        public double getXPositinByXDate(DateTime eDate)
        {
            double xMinPF = leftColRatio;//xMinPF = 0.15
            double xMaxPF = rightColRatio;//xMaxPF = 0.87
            double xMinDate = zgcAnas.GraphPane.X2Axis.Scale.Min;//xMinDate = 41132.75
            double xMaxDate = zgcAnas.GraphPane.X2Axis.Scale.Max;//xMaxDate = 41132.916666666511
            double eXDate = new XDate(eDate);//eXDate = 41132.760335647967
            float x = 0;
            x = (float)((((xMaxPF - xMinPF) * (eXDate - xMinDate)) / (xMaxDate - xMinDate)) + xMinPF);  //根据eXDate的值,确定该点在X坐标轴的位置(X坐标轴全长为(xMaxPF - xMinPF))
            return x;//x = 0.19465   0.72  		eXDate - xMinDate	0.010335647966712713;
            //xMaxDate - xMinDate	0.16666666651144624	;xMinPF = 0.15  ???
        }
         
        /// 
        /// 根据所给时间值计算其中在ZedGraph图中x的CoordType.PaneFraction浮点坐标
        /// 
        /// 日期时间
        /// x
        public double getYPositinByValue(double value, double ymax, double minnumber)
        {
            double yMinPF = zgcAnas.GraphPane.Chart.Rect.Top / zgcAnas.GraphPane.Rect.Height;//0.42799998814260976
            double yMaxPF = (zgcAnas.GraphPane.Chart.Rect.Top + zgcAnas.GraphPane.Chart.Rect.Height) / zgcAnas.GraphPane.Rect.Height;//0.72199998905471674
            double endvalue = (yMaxPF - yMinPF) / (ymax - minnumber) * (value - minnumber);
            return yMaxPF - endvalue - 0.001;
        }
         
        /// 
        /// 画添加药品线,及加药量   
        /// 
        /// 从哪行开始
        /// 标记名称
        /// 加事件开始时间
        /// 加事件结束时间
        /// 用量
        public void drawDoseObj(double y, string tagName, DateTime eventBegin, DateTime eventEnd, DateTime drugBegin, string Dose, float fontSize = 8.0f, bool isContinued = false)//
        {
            if (Dose.Length >= 9 && drugBegin != eventBegin) drugBegin = drugBegin.AddMinutes(-3);
            else if (Dose.Length >= 14 && drugBegin != eventBegin) drugBegin = drugBegin.AddMinutes(-6);
            //中间单次加药剂量信息
            double x1 = getXPositinByXDate(eventBegin);
            double x2 = getXPositinByXDate(eventEnd);
            double xAvg = getXPositinByXDate(drugBegin);
            double yAvg = y;
            DateTime ps = pageBegin;
            DateTime pe = pageEnd;//pageBegin.AddMinutes(frmAnasRecord.EVERY_PAGE_TIME_SPAN);
            DateTime ads = eventBegin;
            DateTime ade = eventEnd;
            double x3 = getXPositinByXDate(ps);//x3 = 0.15000000596046448
            double x4 = getXPositinByXDate(pe);//x4 = 0.87000000476837158
            if (drugBegin >= ps && drugBegin < pe)
            {
                ZUtil.DrawDrugText(Dose, xAvg, yAvg, zgcAnas, TextPrefix.DD + tagName, Color.Black, fontSize - 1f);
            }
            else
            {
                ZUtil.DrawText(Dose, xAvg, yAvg, zgcAnas, TextPrefix.DD + tagName, false);
            }
            //由于箭头可能会跨越页面,因此首先删除该箭头控件,再重新描绘。
            if (zgcAnas.MasterPane.GraphObjList[TextPrefix.AR + tagName] != null)
            {
                zgcAnas.MasterPane.GraphObjList.Remove(zgcAnas.MasterPane.GraphObjList[TextPrefix.AR + tagName]);
                zgcAnas.MasterPane.GraphObjList.Remove(zgcAnas.MasterPane.GraphObjList["A" + TextPrefix.AR + tagName]);
                zgcAnas.MasterPane.GraphObjList.Remove(zgcAnas.MasterPane.GraphObjList["B" + TextPrefix.AR + tagName]);
            }
            //double yAr = y + 0.002;
            //double yAr = y + 0.009;
            double yAr = y + 0.005;
            //当加药开始时间和结束时间相等
            if (ade.Equals(ads))
            {
                if (ads >= ps && ads < pe)
                {
                    ZUtil.DrawArrow(Color.Blue, fontSize, x1, yAr, x2, yAr, zgcAnas, false, TextPrefix.AR + tagName, isContinued);
                }
                else
                {
                    ZUtil.DrawArrow(x1, yAr, x2, yAr, zgcAnas, TextPrefix.AR + tagName);
                }
            }
            else
            {
                //判断箭头的长度                
                if (ade <= ps || ads >= pe)  //在两尾
                {
                    ZUtil.DrawArrow(x1, yAr, x2, yAr, zgcAnas, TextPrefix.AR + tagName);
                }
                else if (ads < ps && ade > ps && ade < pe)  //左跨越
                {
                    ZUtil.DrawArrow(Color.Blue, fontSize, x3, yAr, x2, yAr, zgcAnas, true, TextPrefix.AR + tagName, isContinued);
                }
                else if (ads >= ps && ade <= pe)  //全部包含在其中
                {
                    ZUtil.DrawArrow(Color.Blue, fontSize, x1, yAr, x2, yAr, zgcAnas, true, TextPrefix.AR + tagName, isContinued);
                }
                else if (ads >= ps && ads < pe && ade > pe) //右跨越
                {
                    ZUtil.DrawArrow(Color.Blue, fontSize, x1, yAr, x4, yAr, zgcAnas, false, TextPrefix.AR + tagName, isContinued);
                }
                else  //包含整个页面
                {
                    ZUtil.DrawArrow(Color.Blue, fontSize, x3, yAr, x4, yAr, zgcAnas, false, TextPrefix.AR + tagName, isContinued);
                }
            }
        }
        public void drawSapObj(double y, string tagName, DateTime eventBegin, DateTime eventEnd, DateTime drugBegin, string Dose, float fontSize = 8.0f, bool isContinued = false)//
        {
            //中间单次加药剂量信息
            double x1 = getXPositinByXDate(eventBegin);
            double x2 = getXPositinByXDate(eventEnd);
            double xAvg = getXPositinByXDate(drugBegin);
            double yAvg = y;
            DateTime ps = pageBegin;
            DateTime pe = pageEnd;//pageBegin.AddMinutes(frmAnasRecord.EVERY_PAGE_TIME_SPAN);
            DateTime ads = eventBegin;
            DateTime ade = eventEnd;
            double x3 = getXPositinByXDate(ps);//x3 = 0.15000000596046448
            double x4 = getXPositinByXDate(pe);//x4 = 0.87000000476837158
            if (drugBegin >= ps && drugBegin < pe)
            {
                ZUtil.DrawDrugText(Dose, xAvg, yAvg, zgcAnas, TextPrefix.DD + tagName, Color.Black, fontSize - 1f);
            }
            else
            {
                ZUtil.DrawText(Dose, xAvg, yAvg, zgcAnas, TextPrefix.DD + tagName, false);
            }
            //由于箭头可能会跨越页面,因此首先删除该箭头控件,再重新描绘。
            if (zgcAnas.MasterPane.GraphObjList[TextPrefix.AR + tagName] != null)
            {
                zgcAnas.MasterPane.GraphObjList.Remove(zgcAnas.MasterPane.GraphObjList[TextPrefix.AR + tagName]);
                zgcAnas.MasterPane.GraphObjList.Remove(zgcAnas.MasterPane.GraphObjList["A" + TextPrefix.AR + tagName]);
                zgcAnas.MasterPane.GraphObjList.Remove(zgcAnas.MasterPane.GraphObjList["B" + TextPrefix.AR + tagName]);
            }
            //double yAr = y + 0.002;
            //double yAr = y + 0.009;
            double yAr = y + 0.005;
            //当加药开始时间和结束时间相等
            if (ade.Equals(ads))
            {
                if (ads >= ps && ads < pe)
                {
                    ZUtil.DrawArrow(Color.Blue, fontSize, x1, yAr, x2, yAr, zgcAnas, false, TextPrefix.AR + tagName, isContinued);
                }
                else
                {
                    ZUtil.DrawArrow(x1, yAr, x2, yAr, zgcAnas, TextPrefix.AR + tagName);
                }
            }
            else
            {
                //判断箭头的长度                
                if (ade <= ps || ads >= pe)  //在两尾
                {
                    ZUtil.DrawArrow(x1, yAr, x2, yAr, zgcAnas, TextPrefix.AR + tagName);
                }
                else if (ads < ps && ade > ps && ade < pe)  //左跨越
                {
                    ZUtil.DrawArrow(Color.Blue, fontSize, x3, yAr, x2, yAr, zgcAnas, true, TextPrefix.AR + tagName, isContinued);
                }
                else if (ads >= ps && ade <= pe)  //全部包含在其中
                {
                    ZUtil.DrawArrow(Color.Blue, fontSize, x1, yAr, x2, yAr, zgcAnas, true, TextPrefix.AR + tagName, isContinued);
                }
                else if (ads >= ps && ads < pe && ade > pe) //右跨越
                {
                    ZUtil.DrawArrow(Color.Blue, fontSize, x1, yAr, x4, yAr, zgcAnas, false, TextPrefix.AR + tagName, isContinued);
                }
                else  //包含整个页面
                {
                    ZUtil.DrawArrow(Color.Blue, fontSize, x3, yAr, x4, yAr, zgcAnas, false, TextPrefix.AR + tagName, isContinued);
                }
            }
        }
        /// 
        /// 画添加药品线,及加药量   
        /// 
        /// 从哪行开始
        /// 标记名称
        /// 加事件开始时间
        /// 加事件结束时间
        /// 用量
        public void drawBloodGasDoseObj(double y, string tagName, DateTime eventBegin, DateTime eventEnd, string Dose, float fontSize = 8.0f)
        {
            //中间单次加药剂量信息
            double x1 = getXPositinByXDate(eventBegin);
            double x2 = getXPositinByXDate(eventEnd);
            double xAvg = x1;
            double yAvg = y;
            DateTime ps = pageBegin;
            DateTime pe = pageEnd;//pageBegin.AddMinutes(frmAnasRecord.EVERY_PAGE_TIME_SPAN);
            DateTime ads = eventBegin;
            DateTime ade = eventEnd;
            double x3 = getXPositinByXDate(ps);//x3 = 0.15000000596046448
            double x4 = getXPositinByXDate(pe);//x4 = 0.87000000476837158
            if (ads >= ps && ads < pe)
            {
                ZUtil.DrawText(Dose, xAvg, yAvg, zgcAnas, TextPrefix.DD + tagName, Color.Blue, fontSize - 1f);
            }
            else
            {
                ZUtil.DrawText(Dose, xAvg, yAvg, zgcAnas, TextPrefix.DD + tagName, false);
            }
        }
        #endregion
        #region IAddEventObj 成员
        public virtual void drawText(double x, double y)
        {
        }
        public virtual void drawText(double x, double y, string totleDone, params string[] RoutesofUSEName)
        {
        }
        public virtual void clearAddObj(ZedGraphControl zgc)
        {
        }
        #endregion
        public virtual void drawSpicle(double x, double y, string content, ZedGraphControl zgc)
        {
        }
        /// 
        /// 画添加事件,底部加事件的序号  
        /// 
        /// 
        /// 
        public virtual void paintAdd(double y, int index)
        {
        }
        public virtual void clearDNAndDT(ZedGraphControl zgc) { }
        #region IComparable 成员
        public int CompareTo(object obj)
        {
            EventObj aeo = obj as EventObj;
            if (aeo == null)
            {
                //throw new Exception("传入对象类型不正确");
                return 0;
            }
            if (aeo.Begin > this.Begin)
            {
                return -1;
            }
            else if (aeo.Begin < this.Begin)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }
        #endregion
    }
}