using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
using System.Globalization;
using System.Windows.Forms.DataVisualization.Charting;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TaskbarClock;

namespace WindowsFormsApp1
{
  public partial class Record : Form
  {
    public Record()
    {
      InitializeComponent();
    }

    public string RecordFile = null;
    private double GraphMin, GraphMax;

    private void Record_Load(object sender, EventArgs e)
    {
      if (RecordFile == null) return;

      Text = Path.GetFileName(RecordFile);

      string[] record = File.ReadAllLines(RecordFile);

      if (record.Length < 3) return;
      if (record[0] != "Telem v1") return;

      string[] head = record[1].Split('|');
      if (head.Length < 2) return;

      for (int a = 1; a < head.Length; a++)
      {
        var s = chart_Graph.Series.Add(head[a]);
        s.ChartType = SeriesChartType.FastLine;
        s.XValueType = ChartValueType.DateTime;
        int i = checkedListBox_Series.Items.Add(head[a]);
        checkedListBox_Series.SetItemChecked(i, true);
      }
      chart_Graph.ChartAreas[0].AxisX.LabelStyle.Format = "mm:ss";

      int count = head.Length;

      double Max = 0, Min = 0;
      double time = 0;

      for (int a = 2; a < record.Length; a++)
      {
        string[] value = record[a].Split('|');
        if (value.Length != count) break;
        time = double.Parse(value[0]);

        if (a == 2) chart_Graph.ChartAreas[0].AxisX.Minimum = time;

        for (int b = 1; b < value.Length; b++)
        {
          float v = float.Parse(value[b], CultureInfo.InvariantCulture.NumberFormat);
          chart_Graph.Series[b - 1].Points.AddXY(time, v);
          if (v > Max) Max = v;
          if (v < Min) Min = v;
        }
      }
      Max += 100;
      Min -= 100;

      GraphMin = Min; GraphMax = Max;

      chart_Graph.ChartAreas[0].AxisX.Maximum = time;

      chart_Graph.ChartAreas[0].AxisY.Maximum = Max;
      chart_Graph.ChartAreas[0].AxisY.Minimum = Min;

      trackBar_GraphY_Left.Minimum = (int)Min;
      trackBar_GraphY_Left.Maximum = (int)Max;
      trackBar_GraphY_Left.Value = (int)Min;

      trackBar_GraphY_Right.Minimum = (int)Min;
      trackBar_GraphY_Right.Maximum = (int)Max;
      trackBar_GraphY_Right.Value = (int)Max;
    }

    private void trackBar_GraphY_Left_Scroll(object sender, EventArgs e)
    {
      if (checkBox_AutoY.Checked) return;

      int min = trackBar_GraphY_Left.Value;
      int max = trackBar_GraphY_Right.Value;
      if (min > max)
      {
        int sub = min;
        min = max;
        max = sub;
      }

      if (max == min) max += 1;

      chart_Graph.ChartAreas[0].AxisY.Minimum = min;
      chart_Graph.ChartAreas[0].AxisY.Maximum = max;
    }

    private void checkedListBox_Series_SelectedIndexChanged(object sender, EventArgs e)
    {
      if (checkedListBox_Series.SelectedIndex == -1) return;
      int index = checkedListBox_Series.SelectedIndex;
      button_Color.BackColor = chart_Graph.Series[index].Color;
      //---
      for (int a = 0; a < checkedListBox_Series.Items.Count; a++)
      {
        chart_Graph.Series[a].Enabled = checkedListBox_Series.GetItemChecked(a);
      }
    }

    private void button_Color_Click(object sender, EventArgs e)
    {
      if (colorDialog_Color.ShowDialog() == DialogResult.Cancel) return;
      if (checkedListBox_Series.SelectedIndex == -1) return;
      //---
      int index = checkedListBox_Series.SelectedIndex;
      chart_Graph.Series[index].Color = colorDialog_Color.Color;
      button_Color.BackColor = colorDialog_Color.Color;
    }

    private void checkBox_AutoY_CheckedChanged(object sender, EventArgs e)
    {
      if(checkBox_AutoY.Checked)
      {
        chart_Graph.ChartAreas[0].AxisY.Maximum = double.NaN;
        chart_Graph.ChartAreas[0].AxisY.Minimum = double.NaN;
      }
      else
      {
        trackBar_GraphY_Left_Scroll(null, null);
      }
    }
  }
}
