using System;
using System.Collections.Generic;
using System.Drawing;
using System.Data;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using LabelerCore.Data;

namespace UserControls
{
    public partial class RecordView : UserControl
    {
        public RecordView()
        {
            InitializeComponent();
        }

        public void updateRecordView(VehicleInfo info, bool plotHoldOn = false) {
            //draw image
            if (info.Img != null) {
                camRecPictureBox.Image = info.Img.getImage();
            }
                
            //plot profiles

            //plot 1 channel
            FRQInfo frqInfo = info.Frq;
            if (frqInfo != null) {
                List<List<int>> signals = frqInfo.getSignals();
                plotSignals(channel1Chart, new List<List<int>> { signals[0] }, clear: !plotHoldOn);
                setupSignalChart(channel1Chart);
                channel1Chart.Series[0].ChartType = SeriesChartType.Line;

                //plot all channels
                plotSignals(allChannelChart, signals, clear: !plotHoldOn);
                //plot boundaries
                if (!plotHoldOn)
                    allChannelChart.Series.Add("Pb");
                allChannelChart.Series[2].ChartType = SeriesChartType.Line;
                allChannelChart.Series[2].Color = Color.Black;
                allChannelChart.Series[2].Points.AddXY(int.Parse(frqInfo.info["Pb"]), 0);
                allChannelChart.Series[2].Points.AddXY(int.Parse(frqInfo.info["Pb"]), 1.0);
                if (!plotHoldOn)
                    allChannelChart.Series.Add("Pe");
                allChannelChart.Series[3].ChartType = SeriesChartType.Line;
                allChannelChart.Series[3].Color = Color.Black;
                allChannelChart.Series[3].Points.AddXY(int.Parse(frqInfo.info["Pe"]), 0);
                allChannelChart.Series[3].Points.AddXY(int.Parse(frqInfo.info["Pe"]), 1.0);

                setupSignalChart(allChannelChart);
                foreach (var serie in allChannelChart.Series)
                    serie.ChartType = SeriesChartType.Line;

                //plot P00 ... P19
                if (!plotHoldOn)
                    sampledProfileChart.Series[0].Points.Clear();
                for (int i = 0; i < 20; i++) {
                    double y = double.Parse(frqInfo.info["P" + (i < 10 ? "0" + i.ToString() : i.ToString())].Replace('.', ','));
                    sampledProfileChart.Series[0].Points.AddXY(i, y);
                }
                sampledProfileChart.Series[0].ChartType = SeriesChartType.StepLine;
                setupSignalChart(sampledProfileChart);


                //details from .FRQ 
                frqDGV.Rows.Clear();
                frqDGV.Rows.Add(frqInfo.info.Count);
                foreach (var it in frqInfo.info.Select((Value, Index) => new { Value, Index })) {
                    frqDGV.Rows[it.Index].Cells[0].Value = it.Value.Key;
                    frqDGV.Rows[it.Index].Cells[1].Value = it.Value.Value;
                }
            }

            //details from .CSV
            WIMInfo wim = info.WIM;
            if (wim != null) {
                csvDGV.Rows.Clear();
                csvDGV.Rows.Add(wim.info.Count);
                foreach (var it in wim.info.Select((Value, Index) => new { Value, Index })) {
                    csvDGV.Rows[it.Index].Cells[0].Value = it.Value.Key;
                    csvDGV.Rows[it.Index].Cells[1].Value = it.Value.Value;
                }
            }
        }

        private static void plotSignals(Chart chart, List<List<int>> signals, bool clear = true)
        {
            if (clear)
                chart.Series.Clear();
            for (int i = 0; i < signals.Count; i++)
            {
                try
                {
                    chart.Series.Add("channel " + i.ToString());
                }
                catch (Exception) { }

                int min = signals[i].Min();
                double delta = (double)signals[i].Max() - (double)min;
                for (int j = 0; j < signals[i].Count; j++)
                {
                    if (j % 40 == 0)   //make constant
                        chart.Series[i].Points.AddXY(j, (signals[i][j] - min) / delta);
                }
            }
        }

        private static void setupSignalChart(Chart chart)
        {
            chart.ChartAreas[0].AxisX.MajorGrid.LineDashStyle = ChartDashStyle.Dash;
            chart.ChartAreas[0].AxisX.MajorGrid.LineColor = Color.Gray;
            chart.ChartAreas[0].AxisY.MajorGrid.LineDashStyle = ChartDashStyle.Dash;
            chart.ChartAreas[0].AxisY.MajorGrid.LineColor = Color.Gray;
            chart.ChartAreas[0].AxisY.Minimum = 0.0;
            chart.ChartAreas[0].AxisY.Maximum = 1.2;
            foreach (var serie in chart.Series)
            {
                //serie.ChartType = SeriesChartType.Line;
                serie.IsVisibleInLegend = false;
                serie.BorderWidth = 2;
            }
        }   
    }
}
