using CSStudio6Lib;
using GraphControl;
using GraphControl.GraphElements;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TestLib
{
    public partial class MainForm : Form
    {
        private ICSStudio6 m_spDev = new CSStudio6();

        public Graph graph = new Graph();
        private GraphLine line1 = null;
        public Graph graph2 = new Graph();
        private GraphLine line2 = null;
        public MainForm()
        {
            InitializeComponent();
            InitGraph();
        }


        private void InitGraph()
        {
            graph.offsetLeft = 80;
            graph.offsetRight = 20;
            graph.offsetTop = 20;
            graph.offsetBottom = 50;
            graph.IsDrawCursor = true;
            graph.Dock = DockStyle.Fill;
            graph.YAxisTitleColor = Color.Red;
            line1 = new GraphLine(AxisAssosiatedType.Left);
            line1.lineColor = Color.Red;
            line1.markColor = Color.Red;
            line1.markStyle = PointStyle.cycle_empty;
            graph.YAxisTitle = line1.Name = line1.legend = "电位(V)";
            graph.XAxisTitle = "Time/s";
            line1.IsDrawLengend = false;
            graph.AxisManager.IsAutoAll = true;
            graph.AxisManager.IsShowXGrid = graph.AxisManager.IsShowYGrid = true;
            graph.AxisManager.YAxisColor = line1.lineColor;
            graph.AddNewGraphElement(line1);
            graph.SetActiveElement(line1.Name);
            graph.AxisManager.Axises[0].MinValue = 0;

            graph2.offsetLeft = 80;
            graph2.offsetRight = 20;
            graph2.offsetTop = 20;
            graph2.offsetBottom = 50;
            graph2.IsDrawCursor = true;
            graph2.Dock = DockStyle.Fill;
            graph2.YAxisTitleColor = Color.Red;
            line2 = new GraphLine(AxisAssosiatedType.Left);
            line2.lineColor = Color.Red;
            line2.markColor = Color.Red;
            line2.markStyle = PointStyle.cycle_empty;
            graph2.YAxisTitle = line2.Name = line2.legend = "电流(A)";
            graph2.XAxisTitle = "Time/s";
            line2.IsDrawLengend = false;
            graph2.AxisManager.IsAutoAll = true;
            graph2.AxisManager.IsShowXGrid = graph2.AxisManager.IsShowYGrid = true;
            graph2.AxisManager.YAxisColor = line2.lineColor;
            graph2.AddNewGraphElement(line2);
            graph2.SetActiveElement(line2.Name);
            graph2.AxisManager.Axises[0].MinValue = 0;

            pnlGraph.Controls.Add(graph);
            pnlGraph.Controls.Add(graph2);

            pnlGraph_SizeChanged(null, null);
        }

        private void btnSreach_Click(object sender, EventArgs e)
        {
            if (!m_spDev.FindDeviceInCom())
            {
                return;
            }
            //if (!m_spDev.FindDeviceByName("192.168.0.201:57700"))
            //{
            //    return;
            //}
            Text = m_spDev.GetSerialNumber();
        }

        private void btnOcp_Click(object sender, EventArgs e)
        {
            if (!m_spDev.DeviceIsOpen()) return;
            Clear();
            m_spDev.SetOcpParams(float.Parse(txtTime.Text), float.Parse(txtFreq.Text));
            m_spDev.DeviceStart();
            timer1.Start();
        }


        private object m_objLock = new object();
        private void Clear()
        {
            if (line1 == null) return;

            lock(m_objLock)
            {
                line1.Clear();
                line2.Clear();
            }
            this.graph.Invalidate();
            this.graph2.Invalidate();
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            if (!m_spDev.DeviceIsOpen()) return;

            m_spDev.DeviceStop();
            timer1.Stop();

            m_bIsEis = false;
        }

        private void pnlGraph_SizeChanged(object sender, EventArgs e)
        {
            int nWidth = pnlGraph.Width;
            int nHeith = pnlGraph.Height;

            if (this.graph == null) return;

            this.graph.Dock = DockStyle.None;
            this.graph2.Dock = DockStyle.None;

            this.graph.Location = new Point(0, 0);
            this.graph.Size = new Size(nWidth, nHeith / 2);

            this.graph2.Location = new Point(0, nHeith / 2 + 0);
            this.graph2.Size = new Size(nWidth, nHeith / 2);
            this.graph.Invalidate();
            this.graph2.Invalidate();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (m_spDev == null || m_spDev.DeviceIsOpen() == false) return;

            if(!m_bIsEis)
            {
                int nCount = m_spDev.GetValuesCount();
                if (nCount <= 0) return;

                object dEs = new object();
                object dIs = new object();
                object dTs = new object();
                m_spDev.GetValues(out dEs, out dIs, out dTs);

                List<double> listE = (dEs as double[]).ToList();
                List<double> listI = (dIs as double[]).ToList();
                List<double> listT = (dTs as double[]).ToList();

                line1.Add(listT, listE);
                line2.Add(listT, listI);
            }
            else
            {
                int nCount = m_spDev.GetEisValueCount();
                if (nCount <= 0) return;

                object dEs = new object();
                object dIs = new object();
                object dTs = new object();
                object dZs = new object();
                object dAs = new object();
                m_spDev.GetEisValues(out dEs, out dIs, out dTs,out dZs,out dAs);

                List<double> listE = (dEs as double[]).ToList();
                List<double> listI = (dZs as double[]).ToList();

                line1.Add(listE, listE);
                line2.Add(listE, listI);
            }


            this.graph.Invalidate();
            this.graph2.Invalidate();
        }

        private void btnPotS_Click(object sender, EventArgs e)
        {
            if (!m_spDev.DeviceIsOpen()) return;
            Clear();
            m_spDev.SetVoltRange(true, 2);
            m_spDev.SetPotStaticParams(3, float.Parse(txtE1.Text), float.Parse(txtTime.Text), float.Parse(txtFreq.Text));
            m_spDev.DeviceStart();
            timer1.Start();
        }

        private void btnGalS_Click(object sender, EventArgs e)
        {
            if (!m_spDev.DeviceIsOpen()) return;
            Clear();
            m_spDev.SetGalStaticParams(3, float.Parse(txtE1.Text)/1000, float.Parse(txtTime.Text), float.Parse(txtFreq.Text));
            m_spDev.DeviceStart();
            timer1.Start();
        }

        private void btnLSV_Click(object sender, EventArgs e)
        {
            if (!m_spDev.DeviceIsOpen()) return;
            Clear();
            m_spDev.SetLSVAParams(3, float.Parse(txtE1.Text), float.Parse(txtE4.Text), float.Parse(txtE5.Text)/1000, float.Parse(txtFreq.Text));
            m_spDev.DeviceStart();
            timer1.Start();
        }

        private void btnCV_Click(object sender, EventArgs e)
        {
            if (!m_spDev.DeviceIsOpen()) return;
            Clear();

            m_spDev.SetVoltRange(false, 2);
            m_spDev.SetCurrentRange(false, 7, 8, 0, true);

            m_spDev.SetCVParams(3, float.Parse(txtE1.Text), float.Parse(txtE2.Text), float.Parse(txtE3.Text), float.Parse(txtE4.Text), float.Parse(txtE5.Text) / 1000, float.Parse(txtFreq.Text),1);
            m_spDev.DeviceStart();
            timer1.Start();
        }

        private bool m_bIsEis = false;
        private void btnEis_Click(object sender, EventArgs e)
        {
            m_bIsEis = true;

            if (!m_spDev.DeviceIsOpen()) return;
            Clear();
            double fValue = 0;
            m_spDev.GetOcpValue(out fValue);
            m_spDev.SetEISVSfrqParams(100000,0.1f,(float)fValue,10,60);
            m_spDev.DeviceStart();
            timer1.Start();
        }

        private void btnEisG_Click(object sender, EventArgs e)
        {
            m_bIsEis = true;

            if (!m_spDev.DeviceIsOpen()) return;
            Clear();

            m_spDev.SetGNDMode(true);
            m_spDev.SetEisFilter(6, 6, 6);

            m_spDev.SetEISVSfrqGalParams(1000, 0.1f, 0, 2000, 40);
            m_spDev.DeviceStart();
            timer1.Start();

        }
    }
}
