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;
using System.IO.Ports;
using System.Threading;
using System.Windows;
using System.Windows.Forms.DataVisualization.Charting;

namespace pokus01
{
    public struct Record
    {
        public DateTime datetime;  // Datum Cas
        public float temp, hum; // Teplota, Vlhkost
        public UInt16 co2;    // CO2

        public Record(DateTime DateTime, float Temperature, float Humidity, UInt16 CO2)
        {
             datetime = DateTime;
             temp = Temperature;
             hum = Humidity;
             co2 = CO2;
        }
    }
    
    public struct Log
    {
        public byte id;   // ID modulu
        public string name; // nazev modulu
        public List<Record> records;// = new List<Record>();   // seznam zaznamu logu

        public Log(byte ID, string Name/*, List<Record> Records*/)
        {
            id = ID;
            name = Name;
            records = new List<Record>();
        }
    }


    public partial class Form1 : Form
    {   //deklarace a inicializace proměnných třídy
        UInt16 YlineCount = 1;                  // čitač kreslených grafů
        List<Log> Logs = new List<Log>();       // seznam nactenych logu
      
        public Form1()
        {
            InitializeComponent();
            TminPicker.Format = DateTimePickerFormat.Custom;
            TminPicker.CustomFormat = "HH:mm";
            TminPicker.ShowUpDown = true;
            TmaxPicker.Format = DateTimePickerFormat.Custom;
            TmaxPicker.CustomFormat = "HH:mm"; 
            TmaxPicker.ShowUpDown = true;
            QuaBox.SetItemChecked(0, true);
            QuaBox.SetItemChecked(1, true);
            QuaBox.SetItemChecked(2, true);
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
        
        private void timer1_Tick(object sender, EventArgs e)
        {
            
        }

  /*      private void GraphButton_Click(object sender, EventArgs e)
        {

        }*/
        
        public void DrawChartW()
        {
          //  chart1.Series.Clear();
          //  chart1.Series.Add("zkusebni");//0
            for (int i = 0; i < chart1.Series.Count; i++)
            {
                chart1.Series[i].Points.Clear();
                chart1.Series[i].IsVisibleInLegend = false;
            }

            chart1.ChartAreas[0].AxisX.LabelStyle.Format = "yyyy-MM-dd\nHH:MM";

            for (int i = 0; i < Logs.Count; i++)    // vsechny logy
            {
                if (LogsBox.GetItemChecked(i))
                {
                    chart1.Series[i*3 + 1].Name = (Logs[i].name + " teplota");
                    chart1.Series[i*3 + 2].Name = (Logs[i].name + " vlhkost");
                    chart1.Series[i*3 + 3].Name = (Logs[i].name + " CO2");
                    for (int r = 0; r < Logs[i].records.Count; r++)    // vsechny zaznamy
                    {
                        double dt = Logs[i].records[r].datetime.ToOADate();
                        if (QuaBox.GetItemChecked(0) == true)
                        {
                            chart1.Series[i * 3 + 1].Points.AddXY(dt, Logs[i].records[r].temp);
                            chart1.Series[i * 3 + 1].IsVisibleInLegend = true;
                        }
                        if (QuaBox.GetItemChecked(1) == true)
                        {
                            chart1.Series[i * 3 + 2].Points.AddXY(dt, Logs[i].records[r].hum);
                            chart1.Series[i * 3 + 2].IsVisibleInLegend = true;
                        }
                        if (QuaBox.GetItemChecked(2) == true)
                        {
                            chart1.Series[i * 3 + 3].Points.AddXY(dt, Logs[i].records[r].co2);
                            chart1.Series[i * 3 + 3].IsVisibleInLegend = true;
                        }
                    }
                }
            }
            //DmaxPicker.Value = DateTime.FromOADate(chart1.ChartAreas[0].AxisX.Maximum).AddHours(2);
        }


        private void CalibButton_Click(object sender, EventArgs e)
        {
            Form2 frm2;
            if (true/*form not active*/)
            {
                frm2 = new Form2();
            }
            frm2.Show();
        }

        private void LogReadButton_Click(object sender, EventArgs e)
        {
            
            OpenFileDialog openFileDialog1 = new OpenFileDialog();              // Create an instance of the open file dialog box.
            openFileDialog1.Filter = "Text Files (.txt)|*.txt|All Files (*.*)|*.*";// Set filter options and filter index.
            openFileDialog1.FilterIndex = 1;
            openFileDialog1.Title = "Open Text File";
            openFileDialog1.Multiselect = false;// true;
            // Call the ShowDialog method to show the dialog box.// Process input if the user clicked OK.
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                // otevreni souboru, nacteni po radcich do pole stringu
                string filename = openFileDialog1.FileName;
                string[] filelines = System.IO.File.ReadAllLines(filename);
                Logs.Add(new Log(Convert.ToByte(filelines[0]), filelines[1]));              // prida novy log - ID, nazev modulu
                int logi = Logs.Count - 1;
                for (int a = 5; a < filelines.Length; a++)      // radky souboru. zaznamy zacinaji na radku 6
                {
                    // rozparsovani jednotlivych udaju
                    string[] splitLines = filelines[a].Split(' ');  // jednotlive udaje oddelene mezerou
                    DateTime dt = DateTime.ParseExact(splitLines[0]+" "+splitLines[1], "yyyy-M-d HH:mm", System.Globalization.CultureInfo.InvariantCulture);
                    float t = (float)Convert.ToDouble(splitLines[5], System.Globalization.CultureInfo.InvariantCulture);
                    float h = (float)double.Parse(splitLines[7], System.Globalization.CultureInfo.InvariantCulture);
                    UInt16 c = Convert.ToUInt16(splitLines[9]);
                    // ulozeni zaznamu do struktury
                    Logs[logi].records.Add(new Record(dt ,t ,h ,c));       // prida novy zaznam mereni
                }
                LogsBox.Items.Add(Logs[logi].name, true);   // pridat polozku do check boxu
            }
            DrawChartW();
        }

        private void LogLoadButton_Click(object sender, EventArgs e)
        {
            // nacist soubory logu z koncentratoru
            DrawChartW();
            
        }

        private void DTmin_ValueChanged(object sender, EventArgs e)
        {
            DateTime dt = DminPicker.Value.Date + TminPicker.Value.TimeOfDay; 
            if (dt.ToOADate() < chart1.ChartAreas[0].AxisX.Maximum)
            chart1.ChartAreas[0].AxisX.Minimum = dt.ToOADate();
        }

        private void DTmaxPicker_ValueChanged(object sender, EventArgs e)
        {
            DateTime dt = DmaxPicker.Value.Date + TmaxPicker.Value.TimeOfDay;
            if (dt.ToOADate() > chart1.ChartAreas[0].AxisX.Minimum)
                chart1.ChartAreas[0].AxisX.Maximum = dt.ToOADate();
        }

        private void LogsBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            DrawChartW();
        }

       
    }
}
