using CCDCount.DLL;
using CCDCount.MODEL.ShuLiClass;
using CsvHelper;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CCDCount.Forms
{
    public partial class HistoryImageDataForm : Form
    {
        public List<ActiveObjectClass> historyActive = null;
        Stopwatch ShowImageStopwatch = new Stopwatch();
        int Frame = 24;

        string csvFilePath = "";
        StreamWriter writer = null;
        CsvWriter csv = null;
        string csvRowsFilePath = "";
        StreamWriter Rowswriter = null;
        CsvWriter Rowscsv = null;

        public HistoryImageDataForm()
        {
            InitializeComponent();
        }

        private void HistoryDataForm_Load(object sender, EventArgs e)
        {
            trackBar1.Maximum = historyActive.Count-1;
            ShowImageStopwatch.Start();
            PrintHistoryImage(0);
        }
        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            if (ShowImageStopwatch.ElapsedMilliseconds < (1000 / Frame))
            {
                return;
            }
            else
            {
                ShowImageStopwatch.Restart();
            }
            PrintHistoryImage(trackBar1.Value);
        }
        private void PrintHistoryImage(int index)
        {
            GC.Collect();
            ActiveObjectClass activeObjectClass = historyActive[index];
            Bitmap bitmap = new Bitmap(activeObjectClass.ImageWidth, 2048);
            Graphics g = Graphics.FromImage(bitmap);
            Pen redPen = new Pen(Color.Red, 1);
            List<ActiveObjectClass> ShowList = historyActive.Where(o => o.LastSeenLine <= activeObjectClass.StartLine + 2048).ToList();
            for (int i = 0; i < ShowList.Count; i++)
            {
                ShowList[i].RowsData.ForEach(o => g.DrawLine(redPen, new Point(o.StartCol,bitmap.Height-(int)(o.RowsCol - activeObjectClass.StartLine)), new Point(o.EndCol, bitmap.Height - (int)(o.RowsCol - activeObjectClass.StartLine))));
            };
            UpdatepictureBox1(bitmap.Clone() as Bitmap);
        }

        private void UpdatepictureBox1(Bitmap image)
        {
            if (pictureBox1.InvokeRequired)
            {
                pictureBox1.Invoke(new Action<Bitmap>(UpdatepictureBox1), image);
            }
            else
            {
                pictureBox1.Image = image;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            csvFilePath = ".\\HistoryData\\" + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + ".csv";
            csvRowsFilePath = ".\\HistoryData\\Rows" + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + ".csv";
            if (!Directory.Exists(".\\HistoryData\\")) Directory.CreateDirectory(".\\HistoryData\\");
            writer = new StreamWriter(csvFilePath);
            csv = new CsvWriter(writer, CultureInfo.InvariantCulture);
            csv.WriteHeader<ActiveObjectClass>();
            csv.NextRecord();
            Rowswriter = new StreamWriter(csvRowsFilePath);
            Rowscsv = new CsvWriter(Rowswriter, CultureInfo.InvariantCulture);
            Rowscsv.WriteHeader<RowStartEndCol>();
            Rowscsv.NextRecord();
            var batchRecords = historyActive.GetRange(0, historyActive.Count);
            csv.WriteRecords(batchRecords);
            csv.Flush(); // 确保数据写入文件
            for (int i = 0; i < historyActive.Count; i++)
            {
                List<RowStartEndCol> batchRecords2 = historyActive[i].RowsData.GetRange(0, historyActive[i].RowsData.Count);
                batchRecords2.Add(new RowStartEndCol());
                Rowscsv.WriteRecords(batchRecords2);
                Rowscsv.Flush(); // 确保数据写入文件
            }
        }
    }
}
