using System;
using System.Drawing;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using ZedGraph;
using CRVM.SIDExcuter;
using CRVM.Entity;

namespace CRVM
{
    public partial class Plot_energy : DockContent
    {
        private MasterPane master;
        private Machine mac = null;
        IViewHelper fileHelper = null;
        public Plot_energy(IViewHelper fileHelper, Machine mac)
        {
            this.fileHelper = fileHelper;
            this.mac = mac;
            InitializeComponent();
            timer1.Enabled = true;
            timer1.Interval = 2000;
            timer1.Start();
        }

        private void Plot_energy_Load(object sender, EventArgs e)
        {
            SubExcute();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            GraRefresh();
        }
        public void GraRefresh()
        {
            SubExcute();
        }

        private void SubExcute()
        {
            if (fileHelper != null)
            {
                master = new MasterPane();
                master.PaneList.Clear();
                if (SysParam.Instance.orderByChannel)
                {
                    for (int i = 0; i < SysParam.Instance.channelCount; i++)
                    {
                        if (SysParam.Instance.channel[i].ChChecked)
                        {
                            if (SysParam.Instance.checkBoxEnergy)
                                OrderByChannel_Energy(i);
                        }
                    }
                }
                else
                {
                    if (SysParam.Instance.checkBoxEnergy)
                        OrderByKind_Energy();
                }
                master.Border.IsVisible = false;
                master.Fill.Color = SysParam.Instance.colorBackgroud;
                master.Margin.All = 5f;
                master.InnerPaneGap = 5f;
                zedGraphControl1.MasterPane = master;
                Graphics gg1 = zedGraphControl1.CreateGraphics();
                master.AxisChange(gg1);
                if (SysParam.Instance.orderBySingleColumn)
                {
                    zedGraphControl1.MasterPane.SetLayout(gg1, PaneLayout.SingleColumn);
                }
                else
                {
                    zedGraphControl1.MasterPane.SetLayout(gg1, PaneLayout.SquareRowPreferred);
                }
                zedGraphControl1.Height = DockPanel.Height;
                zedGraphControl1.Refresh();
            }
        }
        private void OrderByChannel_Energy( int standNo)
        {

            if (SysParam.Instance.checkBoxEnergyAll)
            {
                energy_All(fileHelper.energyData, SysParam.Instance.comboBoxEnergyAll, standNo);
            }
            if (SysParam.Instance.checkBoxEnergyL)
            {
                energy_LMH(fileHelper.energyData, SysParam.Instance.comboBoxEnergyL, standNo, 0, "低频");
            }
            if (SysParam.Instance.checkBoxEnergyM)
            {
                energy_LMH(fileHelper.energyData, SysParam.Instance.comboBoxEnergyM, standNo, 1, "中频");
            }
            if (SysParam.Instance.checkBoxEnergyH)
            {
                energy_LMH(fileHelper.energyData, SysParam.Instance.comboBoxEnergyH, standNo, 2, "高频");
            }
            if (SysParam.Instance.checkBoxEnergySpeed && (standNo < SysParam.Instance.SpeedChannelCount))
            {
                energy_Speed(fileHelper.energyData, SysParam.Instance.comboBoxEnergySpeed, standNo);
            }
        }
        private void OrderByKind_Energy()
        {
            if (SysParam.Instance.checkBoxEnergyAll)
            {
                for (int i = 0; i < SysParam.Instance.channelCount; i++)
                {
                    if (SysParam.Instance.channel[i].ChChecked)
                    {
                        energy_All(fileHelper.energyData, SysParam.Instance.comboBoxEnergyAll, i);
                    }
                }
            }
            if (SysParam.Instance.checkBoxEnergyL)
            {
                for (int i = 0; i < SysParam.Instance.channelCount; i++)
                {
                    if (SysParam.Instance.channel[i].ChChecked)
                    {
                        energy_LMH(fileHelper.energyData, SysParam.Instance.comboBoxEnergyL, i,0,"低频");
                    }
                }
            }
            if (SysParam.Instance.checkBoxEnergyM)
            {
                for (int i = 0; i < SysParam.Instance.channelCount; i++)
                {
                    if (SysParam.Instance.channel[i].ChChecked)
                    {
                        energy_LMH(fileHelper.energyData, SysParam.Instance.comboBoxEnergyM, i, 1, "中频");
                    }
                }
            }
            if (SysParam.Instance.checkBoxEnergyH)
            {
                for (int i = 0; i < SysParam.Instance.channelCount; i++)
                {
                    if (SysParam.Instance.channel[i].ChChecked)
                    {
                        energy_LMH(fileHelper.energyData, SysParam.Instance.comboBoxEnergyH, i, 2, "高频");
                    }
                }
            }
            if (SysParam.Instance.checkBoxEnergySpeed)
            {
                for (int i = 0; i < SysParam.Instance.SpeedChannelCount; i++)
                {
                    if (SysParam.Instance.channel[i].ChChecked)
                    {
                        energy_Speed(fileHelper.energyData, SysParam.Instance.comboBoxEnergySpeed, i);
                    }
                }
            }
        }
        private void energy_All(double[,] lineList, string yAxisVal, int stdNo)
        {
            GraphPane gpt = new GraphPane();
            gpt.CurveList.Clear();
            int length = fileHelper.signalLengthEnergy;
            double[] tx = new double[length];
            double[] ty = new double[length];
            for (int i = 0; i < length; i++)
            {
                tx[i] = i * SysParam.Instance.reSamplePoint / Convert.ToDouble(SysParam.Instance.reSampleFre);
                ty[i] = lineList[stdNo * 8 + 1, i];
            }
            gpt.AddCurve("", tx, ty, SysParam.Instance.colorEnergy, SymbolType.None);
            gpt.XAxis.Scale.Min = 0.0;
            gpt.XAxis.Scale.Max = tx[length - 1];
            gpt.YAxis.Scale.Min = 0.0;
            gpt.YAxis.Scale.Max = Convert.ToDouble(yAxisVal);
            gpt.Title.Text = (stdNo + 1) + "号机架全局能量";
            GraPaneSetting(gpt);
            this.master.Add(gpt);
        }

        private void energy_LMH(double[,] lineList, string yAxisVal, int stdNo, int typeNo, string type)
        {
            GraphPane gpt = new GraphPane();
            gpt.CurveList.Clear();
            int length = fileHelper.signalLengthEnergy;
            double[] tx = new double[length];
            double[] ty = new double[length];
            double[] tyalarm = new double[length];
            for (int i = 0; i < length; i++)
            {
                tx[i] = i * SysParam.Instance.reSamplePoint / Convert.ToDouble(SysParam.Instance.reSampleFre);
                ty[i] = lineList[stdNo * 8 + 2 + typeNo, i];
                tyalarm[i] = lineList[stdNo * 8 + 5 + typeNo, i];
            }
            gpt.AddCurve("", tx, ty, SysParam.Instance.colorEnergy, SymbolType.None);
            gpt.AddCurve("", tx, tyalarm, SysParam.Instance.colorAlarm, SymbolType.None).Line.Width = 2f;

            gpt.XAxis.Scale.Min = 0.0;
            gpt.XAxis.Scale.Max = tx[length - 1];
            gpt.YAxis.Scale.Min = 0.0;
            gpt.YAxis.Scale.Max = Convert.ToDouble(yAxisVal);
            gpt.Title.Text = (stdNo + 1) + "号机架" + type + "能量";
            GraPaneSetting(gpt);
            this.master.Add(gpt);
        }

        private void energy_Speed(double[,] lineList, string yAxisVal, int stdNo)
        {
            GraphPane gpt = new GraphPane();
            gpt.CurveList.Clear();
            int length = fileHelper.signalLengthEnergy;
            double[] tx = new double[length];
            double[] ty = new double[length];
            for (int i = 0; i < length; i++)
            {
                tx[i] = i * SysParam.Instance.reSamplePoint / Convert.ToDouble(SysParam.Instance.reSampleFre);
                ty[i] = lineList[stdNo * 8, i];
            }
            gpt.AddCurve("", tx, ty, SysParam.Instance.colorSpeed, SymbolType.None).Line.Width = 2f;
            gpt.XAxis.Scale.Min = 0.0;
            gpt.XAxis.Scale.Max = tx[length - 1];
            gpt.YAxis.Scale.Min = 0.0;
            gpt.YAxis.Scale.Max = Convert.ToDouble(yAxisVal);
            gpt.Title.Text = (stdNo + 1) + "号机架速度";
            GraPaneSetting(gpt);
            this.master.Add(gpt);
        }

        private static void GraPaneSetting(GraphPane tempGraph)
        {
            float fontsize = SysParam.Instance.fontSize;
            Color myFontColor = SysParam.Instance.colorFont;
            //tempGraph.Margin.All = 2f;
            tempGraph.Fill.IsVisible = false;
            tempGraph.IsFontsScaled = false;
            tempGraph.TitleGap = 0f;
            tempGraph.Margin.All = 3f;
            tempGraph.Border.Color = Color.FromArgb(127, myFontColor);

            tempGraph.Chart.Border.Color = myFontColor;
            tempGraph.Chart.Fill.IsVisible = false;

            tempGraph.Title.FontSpec.FontColor = myFontColor;
            tempGraph.Title.FontSpec.Family = "微软雅黑";
            tempGraph.Title.FontSpec.IsBold = false;
            tempGraph.Title.FontSpec.Size = 1.5f * fontsize;

            tempGraph.Legend.Border.IsVisible = false;
            tempGraph.Legend.Position = LegendPos.InsideTopLeft;
            tempGraph.Legend.FontSpec.Size = fontsize;
            tempGraph.Legend.Fill.IsVisible = false;
            tempGraph.Legend.FontSpec.FontColor = myFontColor;
            tempGraph.Legend.FontSpec.Family = "微软雅黑";

            tempGraph.XAxis.Title.FontSpec.Family = "微软雅黑";
            tempGraph.XAxis.Title.FontSpec.IsBold = false;
            tempGraph.XAxis.Scale.FontSpec.FontColor = myFontColor;
            tempGraph.XAxis.Scale.FontSpec.Size = fontsize;
            tempGraph.XAxis.Title.IsVisible = false;
            tempGraph.XAxis.MajorTic.IsOutside = false;
            tempGraph.XAxis.MajorTic.Color = Color.FromArgb(127, myFontColor);
            tempGraph.XAxis.MinorTic.IsAllTics = false;

            tempGraph.YAxis.Title.FontSpec.Family = "微软雅黑";
            tempGraph.YAxis.Title.FontSpec.IsBold = false;
            tempGraph.YAxis.Scale.FontSpec.FontColor = myFontColor;
            tempGraph.YAxis.Scale.FontSpec.Size = fontsize;
            tempGraph.YAxis.Title.IsVisible = false;
            tempGraph.YAxis.MajorTic.IsOutside = false;
            tempGraph.YAxis.MajorTic.Color = Color.FromArgb(127, myFontColor);
            tempGraph.YAxis.MinorTic.IsAllTics = false;
            tempGraph.YAxis.MajorGrid.IsZeroLine = false;

            if (SysParam.Instance.BackgroundGridOn)
            {
                tempGraph.XAxis.MajorGrid.IsVisible = true;
                tempGraph.XAxis.MajorGrid.Color = Color.FromArgb(127, myFontColor);
                tempGraph.YAxis.MajorGrid.IsVisible = true;
                tempGraph.YAxis.MajorGrid.Color = Color.FromArgb(127, myFontColor);
            }
            else
            {
                tempGraph.XAxis.MajorGrid.IsVisible = false;
                tempGraph.YAxis.MajorGrid.IsVisible = false;
            }
        }


        private string zedGraphControl1_PointValueEvent(ZedGraphControl sender, GraphPane pane, CurveItem curve, int iPt)
        {
            if (pane.Title.Text.Contains("速度"))
            {
                PointPair pt = curve[iPt];
                return (pt.X.ToString("0.000") + "s   " + pt.Y.ToString("0.0") + "m/min");
            }
            else if (pane.Title.Text.Contains("频谱"))
            {
                PointPair pt = curve[iPt];
                return (pt.X.ToString("0.0") + "Hz   " + pt.Y.ToString("0.00000") + "g");
            }
            else
            {
                PointPair pt = curve[iPt];
                return (pt.X.ToString("0.000") + "s   " + pt.Y.ToString("0.00000") + "g");
            }
        }

        private void zedGraphControl1_ContextMenuBuilder(ZedGraphControl sender, ContextMenuStrip menuStrip, Point mousePt, ZedGraphControl.ContextMenuObjectState objState)
        {
            for (int i = 0; i < menuStrip.Items.Count; i++)
            {
                switch (menuStrip.Items[i].Tag.ToString().Trim())
                {
                    case "copy":
                        menuStrip.Items[i].Text = "复制图片";
                        break;
                    case "show_val":
                        menuStrip.Items[i].Text = "查看坐标";
                        break;
                    case "save_as":
                        menuStrip.Items[i].Text = "图片保存至...";
                        break;
                    case "page_setup":
                        menuStrip.Items[i].Text = "页面设置";
                        break;
                    case "print":
                        menuStrip.Items[i].Text = "打印";
                        break;
                    case "unzoom":
                        menuStrip.Items[i].Text = "还原缩放";
                        break;
                    case "undo_all":
                        menuStrip.Items[i].Text = @"还原缩放\移动";
                        break;
                    case "set_default":
                        menuStrip.Items[i].Text = "恢复默认设置";
                        break;
                }
            }
        }

    }

}
