using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ZedGraph;
using System.Diagnostics;

namespace GcDevicePc.CK_UI
{
    public partial class UserCtl : UserControl
    {
        private MasterPane mster;
        ColorSymbolRotator rotator;
        PointPairList listNmoc = new PointPairList();
        PointPairList listTHC = new PointPairList();
        PointPairList listCH4 = new PointPairList();

        PointPairList listben = new PointPairList();  //苯
        PointPairList listjiaben = new PointPairList();//甲苯
        PointPairList listerjiaben = new PointPairList();//二甲苯
        GraphPane myPaneNmoc ;
        GraphPane myPaneThc ;
        GraphPane myPaneCh4 ;
        GraphPane myPaneben;
        GraphPane myPanejiaben;
        GraphPane myPaneerjiaben;
        public int Systemtype { get; set; }
        public UserCtl()
        {
            InitializeComponent();
        }

        private void UserCtl_Load(object sender, EventArgs e)
        {
            try
            {
                Systemtype =  CKVocAnalyzer.GlobalCKV.Systemtype;

                switch (Systemtype)
                {
                    default:                                 //非甲烷总烃
                        InitZedgrap(zedGraphControl1);
                        data();
                        datadisp();
                        UIVisible(false);
                        break;
                    case 1:
                        InitZedgrapben(zedGraphControl1);
                        data2();
                        datadisp2();
                        break;
                }

            }
            catch (Exception ex)
            {

                Trace.Write(ex);
            }
           
           
           
        }
        private void UIVisible(bool visible)
        {

            label1.Visible = visible;
            textBox4.Visible = visible;
            textBox5.Visible = visible;
            textBox6.Visible = visible;
            label5.Visible = visible;
            label6.Visible = visible;
          

        }

        private void InitZedgrap(ZedGraphControl zedgraph)
        {
            try
            {
                mster = zedgraph.MasterPane;
                mster.Fill = new Fill(Color.White, Color.FromArgb(220, 220, 255), 45.0f);
                mster.PaneList.Clear();
                mster.Title.IsVisible = true;
                mster.Title.Text = "12小时非甲烷总烃趋势图";
                mster.Margin.All = 10;
                mster.InnerPaneGap = 0;
                //zedgraph.GraphPane.XAxis.CrossAuto = true;
                //zedgraph.GraphPane.YAxis.CrossAuto = true;
                 rotator = new ColorSymbolRotator();
                 myPaneNmoc = new GraphPane(new Rectangle(40, 40, 600, 400),
                   "",
                   "",
                   "");
                 myPaneThc = new GraphPane(new Rectangle(40, 40, 600, 400),
                   "",
                   "",
                   "");
                 myPaneCh4 = new GraphPane(new Rectangle(40, 40, 600, 400),
                   "",
                   "",
                   "");

                 setpane(myPaneNmoc,CKVocAnalyzer.GlobalCKV.IngredientList[0].name); //NMHC
                 setpane(myPaneThc, CKVocAnalyzer.GlobalCKV.IngredientList[1].name); //thc
                 setpane(myPaneCh4, CKVocAnalyzer.GlobalCKV.IngredientList[2].name); //CH4

                 using (Graphics g = zedgraph.CreateGraphics())
                 {
                     ZedGraphControl z1 = zedgraph;

                     mster.SetLayout(g, PaneLayout.SingleColumn);
                     z1.AxisChange();                   

                 }

               
                 zedgraph.AxisChange();

            }
            catch(Exception ex)
            {
                Trace.Write(ex);
            }
        }
        private void InitZedgrapben(ZedGraphControl zedgraph)
        {
            try
            {
                mster = zedgraph.MasterPane;
                mster.Fill = new Fill(Color.White, Color.FromArgb(220, 220, 255), 45.0f);
                mster.PaneList.Clear();
                mster.Title.IsVisible = true;
                mster.Title.Text = "12小时趋势图";
                mster.Margin.All = 10;
                mster.InnerPaneGap = 0;
                //zedgraph.GraphPane.XAxis.CrossAuto = true;
                //zedgraph.GraphPane.YAxis.CrossAuto = true;
                rotator = new ColorSymbolRotator();
                myPaneNmoc = new GraphPane(new Rectangle(40, 40, 600, 400),
                  "",
                  "",
                  "");
                myPaneThc = new GraphPane(new Rectangle(40, 40, 600, 400),
                  "",
                  "",
                  "");
                myPaneCh4 = new GraphPane(new Rectangle(40, 40, 600, 400),
                  "",
                  "",
                  "");
                myPaneben = new GraphPane(new Rectangle(40, 40, 600, 400),
                 "",
                 "",
                 "");
                myPanejiaben = new GraphPane(new Rectangle(40, 40, 600, 400),
                 "",
                 "",
                 "");
                myPaneerjiaben = new GraphPane(new Rectangle(40, 40, 600, 400),
                 "",
                 "",
                 "");

                setpane(myPaneNmoc, CKVocAnalyzer.GlobalCKV.IngredientList[0].name);
                setpane(myPaneThc, CKVocAnalyzer.GlobalCKV.IngredientList[1].name);
                setpane(myPaneCh4, CKVocAnalyzer.GlobalCKV.IngredientList[2].name);
                setpane(myPaneben, CKVocAnalyzer.GlobalCKV.IngredientList[3].name);
                setpane(myPanejiaben, CKVocAnalyzer.GlobalCKV.IngredientList[4].name);
                setpane(myPaneerjiaben, CKVocAnalyzer.GlobalCKV.IngredientList[5].name);

                using (Graphics g = zedgraph.CreateGraphics())
                {
                    ZedGraphControl z1 = zedgraph;

                    mster.SetLayout(g, PaneLayout.SingleColumn);
                    z1.AxisChange();

                }


                zedgraph.AxisChange();

            }
            catch (Exception ex)
            {
                Trace.Write(ex);
            }
        }
        private void setpane(GraphPane myPaneT, string text)
        {
            try
            {
              
                
                myPaneT.Fill.IsVisible = false;
                myPaneT.YAxis.Title.Text = text;
                myPaneT.Chart.Fill = new Fill(Color.White, Color.LightYellow, 45.0F);
                myPaneT.BaseDimension = 3.0F;
                myPaneT.XAxis.Title.IsVisible = false;
                myPaneT.XAxis.Scale.IsVisible = false;
                myPaneT.Legend.IsVisible = false;
                myPaneT.Border.IsVisible = false;
                myPaneT.Title.IsVisible = false;
                myPaneT.XAxis.MajorTic.IsOutside = false;
                myPaneT.XAxis.MinorTic.IsOutside = false;
                myPaneT.XAxis.MajorGrid.IsVisible = true;
                myPaneT.XAxis.MinorGrid.IsVisible = true;
                myPaneT.XAxis.Type = AxisType.Date;
               // myPaneT.Margin.All = 0;
               
                   // myPaneT.Margin.Top = 20;
              
                    myPaneT.YAxis.Scale.IsSkipLastLabel = true;
                    LineItem myCurve = myPaneT.AddCurve("",
                      null, Color.Black, SymbolType.None);
                    myCurve.Symbol.Fill = new Fill(Color.White);
             
                //myPaneT.YAxis.MinSpace = 60;
                //myPaneT.Y2Axis.MinSpace = 20;
                mster.Add(myPaneT);
              
               

            }
            catch(Exception ex)
            {
                Trace.Write(ex);
            }
        }
        private void datadisp()
        {
            try
            {
                if (myPaneNmoc == null || myPaneCh4 == null || myPaneThc == null|| listNmoc.Count<1||listCH4.Count<1||listTHC.Count<1)
                {
                   // Trace.WriteLine("跳出", "sys");
                   // Trace.WriteLine(string.Format("NMHC-{0},ch4={1},thc-{2}",listNmoc.Count,listCH4.Count,listTHC.Count),"sys");
                    return;
                   
                }
                textBox1.Text =Math.Round(listNmoc[listNmoc.Count - 1].Y,2).ToString() + "ppm";
                textBox2.Text =Math.Round(listTHC[listTHC.Count - 1].Y,2).ToString() + "ppm";
                textBox3.Text =Math.Round(listCH4[listCH4.Count - 1].Y,2).ToString() + "ppm";
                mster.PaneList.Clear();
                myPaneCh4.CurveList.Clear();
                myPaneNmoc.CurveList.Clear();
                myPaneThc.CurveList.Clear();
                
                //DateTime dt = DateTime.Now;
                //double minx = (double)new XDate(dt.AddHours(-5));
                //double maxx = (double)new XDate(dt);
                //myPaneNmoc.XAxis.Scale.Min = listNmoc[0].X-100;
                //myPaneNmoc.XAxis.Scale.Max = listNmoc[listNmoc.Count - 1].X+1000;
                //myPaneNmoc.XAxis.Scale.Min = minx;
                //myPaneNmoc.XAxis.Scale.Max = maxx;
                myPaneNmoc.XAxis.CrossAuto = true;
                myPaneNmoc.YAxis.CrossAuto = true;
                LineItem myCurve = myPaneNmoc.AddCurve("",
                        listNmoc, Color.Black, SymbolType.None);
                myCurve.Symbol.Fill = new Fill(Color.White);
                mster.Add(myPaneNmoc);
               // myPaneThc.XAxis.Scale.Min = minx;
               // myPaneThc.XAxis.Scale.Max = maxx;
                myPaneThc.XAxis.CrossAuto = true;
                myPaneThc.YAxis.CrossAuto = true;
                LineItem myCurve1 = myPaneThc.AddCurve("",
                       listTHC, Color.Black, SymbolType.None);
                myCurve1.Symbol.Fill = new Fill(Color.White);
                mster.Add(myPaneThc);
               // myPaneCh4.XAxis.Scale.Min = minx;
               // myPaneCh4.XAxis.Scale.Max = maxx;
                myPaneCh4.XAxis.CrossAuto = true;
                myPaneCh4.YAxis.CrossAuto = true;
                LineItem myCurve2 = myPaneCh4.AddCurve("",
                       listCH4, Color.Black, SymbolType.None);
                myCurve2.Symbol.Fill = new Fill(Color.White);
                mster.Add(myPaneCh4);
                zedGraphControl1.AxisChange();
                zedGraphControl1.Refresh();
               
               
            }
            catch(Exception ex)
            {
                Trace.Write(ex);
            }
          //  zedgraph.AxisChange();
        }

        private void datadisp2()  //苯系物
        {
            try
            {
                if (myPaneNmoc == null || myPaneCh4 == null || myPaneThc == null || myPaneben == null || myPanejiaben == null || myPaneerjiaben == null || listNmoc.Count < 1 || listCH4.Count < 1 || listTHC.Count < 1 || listben.Count < 1 || listjiaben.Count < 1 || listerjiaben.Count < 1)
                {
                    return;
                }
                textBox1.Text = Math.Round(listNmoc[listNmoc.Count - 1].Y, 2).ToString() + "ppm";
                textBox2.Text = Math.Round(listTHC[listTHC.Count - 1].Y, 2).ToString() + "ppm";
                textBox3.Text = Math.Round(listCH4[listCH4.Count - 1].Y, 2).ToString() + "ppm";
                textBox4.Text = Math.Round(listben[listben.Count - 1].Y, 2).ToString() + "ppm";
                textBox5.Text = Math.Round(listjiaben[listjiaben.Count - 1].Y, 2).ToString() + "ppm";
                textBox6.Text = Math.Round(listerjiaben[listerjiaben.Count - 1].Y, 2).ToString() + "ppm";
                mster.PaneList.Clear();
                myPaneCh4.CurveList.Clear();
                myPaneNmoc.CurveList.Clear();
                myPaneThc.CurveList.Clear();

                myPaneben.CurveList.Clear();
                myPanejiaben.CurveList.Clear();
                myPaneerjiaben.CurveList.Clear();

                //DateTime dt = DateTime.Now;
                //double minx = (double)new XDate(dt.AddHours(-5));
                //double maxx = (double)new XDate(dt);
                //myPaneNmoc.XAxis.Scale.Min = listNmoc[0].X-100;
                //myPaneNmoc.XAxis.Scale.Max = listNmoc[listNmoc.Count - 1].X+1000;
                //myPaneNmoc.XAxis.Scale.Min = minx;
                //myPaneNmoc.XAxis.Scale.Max = maxx;
                myPaneNmoc.XAxis.CrossAuto = true;
                myPaneNmoc.YAxis.CrossAuto = true;
                LineItem myCurve = myPaneNmoc.AddCurve("",
                        listNmoc, Color.Black, SymbolType.None);
                myCurve.Symbol.Fill = new Fill(Color.White);
                mster.Add(myPaneNmoc);
                // myPaneThc.XAxis.Scale.Min = minx;
                // myPaneThc.XAxis.Scale.Max = maxx;
                myPaneThc.XAxis.CrossAuto = true;
                myPaneThc.YAxis.CrossAuto = true;
                LineItem myCurve1 = myPaneThc.AddCurve("",
                       listTHC, Color.Black, SymbolType.None);
                myCurve1.Symbol.Fill = new Fill(Color.White);
                mster.Add(myPaneThc);
                // myPaneCh4.XAxis.Scale.Min = minx;
                // myPaneCh4.XAxis.Scale.Max = maxx;
                myPaneCh4.XAxis.CrossAuto = true;
                myPaneCh4.YAxis.CrossAuto = true;
                LineItem myCurve2 = myPaneCh4.AddCurve("",
                       listCH4, Color.Black, SymbolType.None);
                myCurve2.Symbol.Fill = new Fill(Color.White);
                mster.Add(myPaneCh4);


                myPaneben.XAxis.CrossAuto = true;
                myPaneben.YAxis.CrossAuto = true;
                LineItem myCurve3 = myPaneben.AddCurve("",
                       listben, Color.Black, SymbolType.None);
                myCurve3.Symbol.Fill = new Fill(Color.White);
                mster.Add(myPaneben);

                myPanejiaben.XAxis.CrossAuto = true;
                myPanejiaben.YAxis.CrossAuto = true;
                LineItem myCurve4 = myPanejiaben.AddCurve("",
                       listjiaben, Color.Black, SymbolType.None);
                myCurve4.Symbol.Fill = new Fill(Color.White);
                mster.Add(myPanejiaben);

                myPaneerjiaben.XAxis.CrossAuto = true;
                myPaneerjiaben.YAxis.CrossAuto = true;
                LineItem myCurve5 = myPaneerjiaben.AddCurve("",
                       listerjiaben, Color.Black, SymbolType.None);
                myCurve5.Symbol.Fill = new Fill(Color.White);
                mster.Add(myPaneerjiaben);
                zedGraphControl1.AxisChange();
                zedGraphControl1.Refresh();

            }
            catch (Exception ex)
            {
                Trace.Write(ex);
            }
            //  zedgraph.AxisChange();
        }
        private void data()
        {
            try
            {
                //double x, y;
                ////int j = 0;
                ////if (listNmoc.Count > 0)
                ////{
                ////    j = (int)listNmoc[listNmoc.Count - 1].X;
                ////}
                //for (int i = 0; i < 36 ; i++)
                //{
                //    x = (double)new XDate(DateTime.Now.AddSeconds(2));
                //    y = 3.0 * (1.5 + Math.Sin((double)i * 0.2));
                //    listNmoc.Add(x, y);
                //    listCH4.Add(x, y);
                //    listTHC.Add(x, y);
                //}

                SQLiteOperate.VOCSqlite sql = new SQLiteOperate.VOCSqlite(CKVocAnalyzer.GlobalCKV.DataBasePath);
                sql.open();
                DateTime dt = DateTime.Now;
                string start=dt.AddHours(-12).ToString("yyyy-MM-dd HH:mm:ss");
                string end=dt.ToString("yyyy-MM-dd HH:mm:ss");
                sql.selectTimes(start,end );
                sql.close();
                if(sql.ReturnData==null)
                {
                    return;
                }
                listNmoc.Clear();
                listCH4.Clear();
                listTHC.Clear();
                for (int i = 0; i < sql.ReturnData.Count; ++i)
                {
                    if (sql.ReturnData[i].name == CKVocAnalyzer.GlobalCKV.IngredientList[0].name)
                    {
                        double times = (double)new XDate(Convert.ToDateTime(sql.ReturnData[i].Time));
                        double v = sql.ReturnData[i].value;
                        listNmoc.Add(times,v);
                    }
                    if (sql.ReturnData[i].name == CKVocAnalyzer.GlobalCKV.IngredientList[1].name)
                    {
                        double times1 = (double)new XDate(Convert.ToDateTime(sql.ReturnData[i].Time));
                        double v1 = sql.ReturnData[i].value;
                        listTHC.Add(times1, v1);
                    }
                    if (sql.ReturnData[i].name == CKVocAnalyzer.GlobalCKV.IngredientList[2].name)
                    {
                        double times2 = (double)new XDate(Convert.ToDateTime(sql.ReturnData[i].Time));
                        double v2 = sql.ReturnData[i].value;
                        listCH4.Add(times2, v2);
                    }

                }
              //  Trace.WriteLine(string.Format("读取个数{0}",sql.ReturnData.Count),"sys");
               
               
            }catch(Exception ex)
            {
                Trace.Write(ex);
            }
        }

        private void data2()
        {
            try
            {
                SQLiteOperate.VOCSqlite sql = new SQLiteOperate.VOCSqlite(CKVocAnalyzer.GlobalCKV.DataBasePath);
                sql.open();
                DateTime dt = DateTime.Now;
                string start = dt.AddHours(-12).ToString("yyyy-MM-dd HH:mm:ss");
                string end = dt.ToString("yyyy-MM-dd HH:mm:ss");
                sql.selectTimes(start, end);
                sql.close();
                if (sql.ReturnData == null)
                {
                    return;
                }
                listNmoc.Clear();
                listCH4.Clear();
                listTHC.Clear();
                listben.Clear();
                listjiaben.Clear();
                listerjiaben.Clear();
                for (int i = 0; i < sql.ReturnData.Count; ++i)
                {
                    double times = (double)new XDate(Convert.ToDateTime(sql.ReturnData[i].Time));
                    double v = sql.ReturnData[i].value;
                    if (sql.ReturnData[i].name == CKVocAnalyzer.GlobalCKV.IngredientList[0].name)
                    {                       
                        listNmoc.Add(times, v);
                    }
                    if (sql.ReturnData[i].name == CKVocAnalyzer.GlobalCKV.IngredientList[1].name)
                    {

                        listTHC.Add(times, v);
                    }
                    if (sql.ReturnData[i].name == CKVocAnalyzer.GlobalCKV.IngredientList[2].name)
                    {

                        listCH4.Add(times, v);
                    }
                    if (sql.ReturnData[i].name == CKVocAnalyzer.GlobalCKV.IngredientList[3].name)
                    {
                       
                        listben.Add(times, v);
                    }
                    if (sql.ReturnData[i].name == CKVocAnalyzer.GlobalCKV.IngredientList[4].name)
                    {

                        listjiaben.Add(times, v);
                    }
                    if (sql.ReturnData[i].name == CKVocAnalyzer.GlobalCKV.IngredientList[5].name)
                    {

                        listerjiaben.Add(times, v);
                    }

                }


            }
            catch (Exception ex)
            {
                Trace.Write(ex);
            }
        }


        private void timer1_Tick(object sender, EventArgs e)
        {
            try
            {
                switch (Systemtype)
                {
                    default:                                 //非甲烷总烃

                        data();
                        datadisp();
                        UIVisible(false);
                        break;
                    case 1:

                        data2();
                        datadisp2();
                        break;
                }
               // Trace.WriteLine("绘图","sys");
            }
            catch(Exception ex)
            {
                Trace.Write(ex);
            }
           
           
        }
    }
}
