using AIMSExtension;
using DevComponents.Editors.DateTimeAdv;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using static System.Net.Mime.MediaTypeNames;
namespace DrawGraph
{
    /// 
    /// 麻醉前访视单
    /// 
    public class TempDataManage : AreaManageBase
    {
        private OperationRecord myOpeRecord = null;
        AbleEditPackObj ableEdit2;
        AbleEditPackObj ableEdit3;
        public TempDataManage() { }
        public TempDataManage(object _operationRecor, DrawGraph.ZedGraphControl _zedControl, TemplateManage _template, string _name) : base(_operationRecor, _zedControl, _template, _name)
        { }
        public void init()
        {
            //自己要用的手术对象
            myOpeRecord = OpeRecord as OperationRecord;
            ableEdit2 = template.GetPackObjectOTag("TempDataManage_AbleEditPackObj_357_OperationRecord_InstrumentList_TagPicturePanel");
            ableEdit3 = template.GetPackObjectOTag("TempDataManage_AbleEditPackObj_397_OperationRecord_InstrumentList_PackPanel");
        }
        #region 重写的事件
        /// 
        /// 鼠标点击画板
        /// 
        /// 
        /// 
        public override void MouseDown(ZedGraphControl sender, MouseEventArgs e)
        {
        }
        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 editAr_Click(object sender, EventArgs e)
        {
            SelectDictText.Hidden();
            //传过来的数据是不是可编辑的
            AbleEditPackObj editPack = sender as AbleEditPackObj;
            if (editPack == null) return;
            //找到这个组件
            AbleEditPackObj aEdit1 = PackManage.ListPob.FirstOrDefault(s => s is AbleEditPackObj && s.PackTag == editPack.PackTag) as AbleEditPackObj;
            if (aEdit1 == null) return;
            txt_DoubleClick(aEdit1.CControl, null);
        }
        public SelectDictText aSyncSelectDict = null;
        public override void Bind()
        {
            aSyncSelectDict = SelectDictText.CreateInstance();
            //SelectDictText.SetValue += ASyncSelectDict_SetValue;
            base.Bind();
            init();
            if (ableEdit2 != null)
            {
                GraphObj gos = ZedControl.MasterPane.GraphObjList[TextPrefix.IM + "InstrumentListTagPicturePanel2"];
                if (gos == null)
                {
                    ZUtil.DrawImage("bjtp", ableEdit2.RealEndX, ableEdit2.RealY, (ableEdit2.RealEndX - ableEdit2.RealX), (ableEdit2.RealEndY - ableEdit2.RealY) / 2, ZedControl, TextPrefix.IM + "InstrumentListTagPicturePanel2");
                }
            }
            if (ableEdit3 != null)
            {
                for (int i = 593; i < 617; i++)
                {
                    TextPackObj packObj = template.GetPackObjectOTag("TempDataManage_TextPackObj_" + i);
                    if (packObj != null)
                    {
                        packObj.PackText = "";
                        packObj.PackValue = "";
                        packObj.Draw();
                    }
                }
            }
            List ables = PackManage.ListPob.Where(s => s is AbleEditPackObj).ToList();
            foreach (PackObjBase pack in ables)
            {
                AbleEditPackObj ableEdit = pack as AbleEditPackObj;
                if (ableEdit != null)
                {
                    ableEdit.IsVisible = true;
                    ableEdit.CControl.Click -= new EventHandler(txt_Click);
                    ableEdit.CControl.Click += new EventHandler(txt_Click);
                    if (ableEdit.ControlType == EControlType.DateTimePicker)
                    {
                        ableEdit.CControl.Leave -= new EventHandler(txt_Leave);
                        ableEdit.CControl.Leave += new EventHandler(txt_Leave);
                    }
                    else if (ableEdit.ControlType == EControlType.Directory)
                    {
                        ableEdit.CControl.DoubleClick -= new EventHandler(txt_DoubleClick);
                        ableEdit.CControl.DoubleClick += new EventHandler(txt_DoubleClick);
                        ableEdit.CControl.Leave -= new EventHandler(txt_Leave);
                        ableEdit.CControl.Leave += new EventHandler(txt_Leave);
                    }
                    else if (ableEdit.ControlType == EControlType.CheckBox)
                    {
                        Panel pan = ableEdit.CControl as Panel;
                        if (pan != null)
                        {
                            foreach (Control conl in pan.Controls)
                            {
                                CheckBox chBox = conl as CheckBox;
                                if (chBox != null)
                                {
                                    chBox.CheckedChanged -= new EventHandler(txt_Leave);
                                    chBox.CheckedChanged += new EventHandler(txt_Leave);
                                }
                            }
                        }
                    }
                    else if (ableEdit.ControlType == EControlType.RadioButtonList)
                    {
                        Panel pan = ableEdit.CControl as Panel;
                        if (pan != null)
                        {
                            foreach (Control conl in pan.Controls)
                            {
                                CheckBox chBox = conl as CheckBox;
                                if (chBox != null)
                                {
                                    chBox.CheckedChanged -= new EventHandler(txt_Leave);
                                    chBox.CheckedChanged += new EventHandler(txt_Leave);
                                }
                            }
                        }
                    }
                    else    if (ableEdit.ControlType == EControlType.TextBox)
                    {
                        ableEdit.CControl.KeyDown -= new KeyEventHandler(text_keyDown);
                        ableEdit.CControl.KeyDown += new KeyEventHandler(text_keyDown); 
                        ableEdit.CControl.GotFocus -= new EventHandler(txt_Focus);
                        ableEdit.CControl.GotFocus += new EventHandler(txt_Focus);
                        ((TextBox)ableEdit.CControl).BorderStyle = BorderStyle.Fixed3D;
                        ableEdit.CControl.MouseWheel += new MouseEventHandler(numericUpDown1_MouseWheel);
                        ableEdit.CControl.Leave -= new EventHandler(txt_Leave);
                        ableEdit.CControl.Leave += new EventHandler(txt_Leave);
                    }
                    else
                    {
                        ableEdit.CControl.Leave -= new EventHandler(txt_Leave);
                        ableEdit.CControl.Leave += new EventHandler(txt_Leave);
                    }
                    SetAbleEditView(ableEdit);
                }
            }
        }
         
        ////取消滚轮事件
        //void numericUpDown1_MouseWheel(object sender, MouseEventArgs e)
        //{
        //    base.MouseWheelParam();
        //}
        private void txt_Focus(object sender, EventArgs e)
        {
            TextBox control = (TextBox)sender;
            if (control == null) return;
            AbleEditPackObj ableEdit = control.Tag as AbleEditPackObj;
            if (ableEdit == null) return;
            if (control.Focused == true && (ableEdit.PackValue == ableEdit.DefaultValue || ableEdit.PackValue == ""))
                control.Clear();
        } 
        private void text_keyDown(object sender, KeyEventArgs e)
        {
            ((TextBox)sender).Enabled = true;
            if (e.Modifiers.CompareTo(Keys.Control) == 0 && e.KeyCode == Keys.V)
            {
                string val = Clipboard.GetText();
                val = val.Replace("\r", "").Replace("\n", "");
                Clipboard.SetText(val);
            }
        }
        /// 
        /// 在画完表单后,显示多选单选的默认项
        /// 
        public override void FollowUpMethod()
        {
            base.FollowUpMethod();
            List ables = PackManage.ListPob.Where(s => s is AbleEditPackObj).ToList();
            foreach (PackObjBase pack in ables)
            {
                AbleEditPackObj ableEdit = pack as AbleEditPackObj;
                SetAbleEditView(ableEdit);
            }
        }
        public override void BindTempData()
        {
            if (myOpeRecord != null && myOpeRecord.Id != null)
            {
                if (ableEdit2 != null)
                {
                    GraphObj gos = ZedControl.MasterPane.GraphObjList[TextPrefix.IM + "InstrumentListTagPicturePanel"];
                    if (gos != null)
                    {
                        ZedControl.MasterPane.GraphObjList.Remove(gos);
                    }
                    if (myOpeRecord.InstrumentList != null && myOpeRecord.InstrumentList.TagPicture != null && myOpeRecord.InstrumentList.TagPicture != "")
                    {
                        MemoryStream ms = new MemoryStream(Convert.FromBase64String(myOpeRecord.InstrumentList.TagPicture));
                        System.Drawing.Image image = System.Drawing.Image.FromStream(ms);
                        ZUtil.DrawImage(image, ableEdit2.RealX, ableEdit2.RealY, (ableEdit2.RealEndX - ableEdit2.RealX), (ableEdit2.RealEndY - ableEdit2.RealY), ZedControl, TextPrefix.IM + "InstrumentListTagPicturePanel");
                    }
                }
                if (ableEdit3 != null && myOpeRecord.InstrumentList != null && myOpeRecord.InstrumentList.JsonTextData != null && myOpeRecord.InstrumentList.JsonTextData != "")
                {
                    //593-616
                    List list = JsonConvert.DeserializeObject>(myOpeRecord.InstrumentList.JsonTextData);
                    foreach (JsonTextForDoc m in list)
                    {
                        if (m.Id.Trim() == "")
                            continue;
                        TextPackObj packObj = template.GetPackObjectOTag(m.Tag);
                        packObj.PackText = m.Value;
                        packObj.PackValue = m.Value;
                        packObj.Draw();
                    }
                }
            }
            List ables = PackManage.ListPob.Where(s => s is AbleEditPackObj).ToList();
            foreach (PackObjBase pack in ables)
            {
                pack.Draw();
            }
        }
        /// 
        /// 公共验证方法
        /// 
        /// 
        public override bool Verification()
        {
            List editPack = PackManage.ListPob.Where(s => s is AbleEditPackObj).ToList();
            foreach (PackObjBase pack in editPack)
            {
                AbleEditPackObj ablePack = pack as AbleEditPackObj;
                if (ablePack != null)
                {
                    if (ablePack.ValidType == EValidType.IsRequired)
                    {
                        if (ablePack.PackValue.Trim() == "")
                        {
                            MessageBox.Show(pack.Descript + "不能为空");
                            return false;
                        }
                    };
                    if (ablePack.PackValue != "" && ablePack.ValidType == EValidType.IsNumber)
                    {
                        try
                        {
                            int.Parse(ablePack.PackValue);
                        }
                        catch (Exception)
                        {
                            MessageBox.Show(pack.Descript + "不能为空且必须是数值型");
                            return false;
                        }
                    };
                    if (ablePack.TextLength != 0)
                    {
                        if (ablePack.PackValue.Length >= ablePack.TextLength)
                        {
                            MessageBox.Show(ablePack.Descript + "长度不能超过" + ablePack.TextLength + "字");
                        }
                    }
                }
            }
            return true;
        }
        #endregion 重写的事件结束
        /// 
        /// 设置可编辑组件的显示样式
        /// 
        /// 
        private void SetAbleEditView(AbleEditPackObj ableEdit, bool isUpdate = false)
        {
            string span = "";
            double spanSum = Math.Round((float)(ableEdit.OneUnitCount / 18));
            for (int i = 0; i < spanSum; i++)
            {
                if (ableEdit.IsHorizontal == ELineType.Horizontal)
                {
                    span += " ";
                }
                else
                {
                    span += "\n";
                }
            }
            string text = "", value = "";
            if (ableEdit != null && ableEdit.CControl != null)
            {
                switch (ableEdit.ControlType)
                {
                    case EControlType.RadioButtonList:
                        foreach (Control conl in ableEdit.CControl.Controls)
                        {
                            string spanT = span.Clone().ToString();
                            if (ableEdit.DfArrangement == EIsBool.True)
                            {
                                if (spanT.Length - conl.Text.Length >= conl.Text.Length)
                                {
                                    spanT = spanT.Remove(spanT.Length - conl.Text.Length, conl.Text.Length);
                                }
                            }
                            CheckBox radio = conl as CheckBox;
                            if (radio.Checked)
                            {
                                text += "☑" + conl.Text + spanT;
                                value = conl.Text;
                            }
                            else
                            {
                                text += "□" + conl.Text + spanT;
                            }
                        }
                        break;
                    case EControlType.CheckBox:
                        foreach (Control conl in ableEdit.CControl.Controls)
                        {
                            string spanT = span.Clone().ToString();
                            if (ableEdit.DfArrangement == EIsBool.True)
                            {
                                if (spanT.Length - conl.Text.Length >= conl.Text.Length)
                                {
                                    spanT = spanT.Remove(spanT.Length - conl.Text.Length, conl.Text.Length);
                                }
                            }
                            CheckBox radio = conl as CheckBox;
                            if (radio.Checked)
                            {
                                text += "☑" + conl.Text + spanT;
                                value += conl.Text + ",";
                            }
                            else
                            {
                                text += "□" + conl.Text + spanT;
                            }
                        }
                        value = value.TrimEnd(',');
                        break;
                    case EControlType.Directory:
                        text = ableEdit.CControl.Text;
                        value = ableEdit.CControl.Text;
                        break;
                    default:
                        text = ableEdit.CControl.Text;
                        value = ableEdit.CControl.Text;
                        break;
                }
                //设置属性的值
                template.SetObjValue(OpeRecord, ableEdit.ClassDataSourceName, text, value, isUpdate);
                //如果有逻辑区域,则区域进行更新。
                template.NotificationAreaBindingUpdate(template.ProjectName + "逻辑");
            }
        }
        private void txt_DoubleClick(object sender, EventArgs e)
        {
            Control control = (Control)sender;
            if (control == null) return;
            AbleEditPackObj ableEdit = control.Tag as AbleEditPackObj;
            if (ableEdit == null) return;
            string DataSourceName = ableEdit.ClassDataSourceName;
            try
            {
                string text = "", value = "";
                switch (ableEdit.ControlType)
                {
                    case EControlType.DateTimePicker:
                        text = DateTime.Now.ToString();
                        value = DateTime.Now.ToString();
                        control.Text = text;
                        break;
                    case EControlType.Directory:
                        TYZD_Click(ableEdit, e, true);
                        break;
                    default:
                        text = ableEdit.CControl.Text;
                        value = ableEdit.CControl.Text;
                        break;
                }
            }
            catch (Exception ex)
            {
                PublicMethod.WriteLog(ex);
            }
            finally
            {
                template.ZedControl.Refresh();
            }
        }
        private void txt_Click(object sender, EventArgs e)
        {
            Control control = (Control)sender;
            if (control == null) return;
            AbleEditPackObj ableEdit = control.Tag as AbleEditPackObj;
            if (ableEdit == null) return;
            string DataSourceName = ableEdit.ClassDataSourceName;
            if (SelectDictText.dgvZd.Visible == true)
            {
                SelectDictText.Hidden();
            }
        }
        private void txt_Leave(object sender, EventArgs e)
        {
            Control control = (Control)sender;
            if (control == null) return;
            AbleEditPackObj ableEdit = control.Tag as AbleEditPackObj;
            if (ableEdit == null) return;
            try
            {
                if (ableEdit.ControlType == EControlType.RadioButtonList)
                {
                    if ((sender as CheckBox).Checked == true)
                    {
                        foreach (CheckBox chk in (sender as CheckBox).Parent.Controls)
                        {
                            chk.CheckedChanged -= new EventHandler(txt_Leave);
                            if (chk != sender)
                            {
                                chk.Checked = false;
                            }
                            chk.CheckedChanged += new EventHandler(txt_Leave);
                        }
                    }
                }
                SetAbleEditView(ableEdit, true);
            }
            catch (Exception ex)
            {
                //写日志
                PublicMethod.WriteLog(ex);
            }
            finally
            {
            }
        }
        //设置打印模式,组件不显示
        public override void setPrint(bool isVisible)
        {
            //预览状态时所可编辑组件的组件隐藏
            List ables = PackManage.ListPob.Where(s => s is AbleEditPackObj).ToList();
            foreach (PackObjBase pack1 in ables)
            {
                AbleEditPackObj ableEdit = pack1 as AbleEditPackObj;
                if (ableEdit != null)
                {
                    ableEdit.IsVisible = isVisible;
                    if (isVisible == true)
                        ableEdit.IsViewBoard = EIsBool.True;
                    else
                        ableEdit.IsViewBoard = EIsBool.False;
                }
            }
        }
        //通用字典分组窗体打开
        private void TYZD_Click(AbleEditPackObj sender, EventArgs e, bool isRadio = false)
        {
            AbleEditPackObj ableEdit = sender;
            if (ableEdit == null) return;
            try
            {
                if (myOpeRecord != null)
                {
                    SelDict(sender, e, ableEdit.ControlTitleText, isRadio);
                }
            }
            catch (Exception ex)
            {
                AIMSExtension.PublicMethod.WriteLog(ex);
            }
        }
        private void SelDict(AbleEditPackObj sender, EventArgs e, string _DictType, bool isRadio = false)
        {
            AbleEditPackObj ableEdit = sender;
            if (ableEdit == null) return;
            ableEdit.CControl.Leave -= new EventHandler(txt_Leave);
            ableEdit.CControl.KeyUp -= new KeyEventHandler(CControl_KeyUp);
            ableEdit.CControl.KeyUp += new KeyEventHandler(CControl_KeyUp);
            ableEdit.CControl.KeyPress -= new KeyPressEventHandler(CControl_KeyPress);
            ableEdit.CControl.KeyPress += new KeyPressEventHandler(CControl_KeyPress);
            if (myOpeRecord != null)
            {
                Control conl = sender.CControl;
                if (conl is TextBox)
                {
                    ((TextBox)conl).BorderStyle = BorderStyle.Fixed3D;
                    ((TextBox)conl).Focus();
                    if (sender.PackValue != null && sender.PackValue.Trim() != "" && isRadio == false)
                    {
                        if (((TextBox)conl).Text.LastIndexOf(',') != sender.PackText.Length)
                            ((TextBox)conl).Text = ((TextBox)conl).Text + ",";
                    }
                        ((TextBox)conl).Select(((TextBox)conl).Text.Length, 1);
                }
                aSyncSelectDict.Show(template, OpeRecord, sender, _DictType, isRadio);
                ableEdit.CControl.Leave += new EventHandler(txt_Leave);
            }
        }
        private void ASyncSelectDict_SetValue(string Text, string Key, AbleEditPackObj aEdit)
        {
            aEdit.PackValue = Text;
            aEdit.PackText = Text;
            template.SetObjValue(OpeRecord, aEdit.ClassDataSourceName, Text, Text, true);
            aEdit.CControl.Leave -= new EventHandler(txt_Leave);
            aEdit.CControl.Leave += new EventHandler(txt_Leave);
        }
        private void CControl_KeyUp(object sender, KeyEventArgs e)
        {
            if (myOpeRecord != null)
            {
                Control conl = sender as Control;
                if (e.KeyCode == Keys.Down)
                {
                    if (SelectDictText.dgvZd.CurrentRow.Index + 1 != SelectDictText.dgvZd.Rows.Count - 1)
                    {
                        SelectDictText.dgvZd.CurrentCell = SelectDictText.dgvZd.Rows[SelectDictText.dgvZd.CurrentRow.Index + 1].Cells[1];
                    }
                }
                else if (e.KeyCode == Keys.Up)
                {
                    if (SelectDictText.dgvZd.CurrentRow.Index != 0)
                    {
                        SelectDictText.dgvZd.CurrentCell = SelectDictText.dgvZd.Rows[SelectDictText.dgvZd.CurrentRow.Index - 1].Cells[1];
                    }
                }
            }
        }
        private void CControl_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar.ToString() == "\r")
            {
                if (SelectDictText.dgvZd.SelectedRows.Count > 0)
                {
                    SelectDictText.SetContent(SelectDictText.dgvZd.SelectedRows[0].Index);
                }
            }
        }
    }
}