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;

namespace Grovewood
{
    public partial class Log_Reader : Form
    {
        public Log_Reader()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //flush the log
            errorLog.Items.Clear();
            LoadError();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Log_Reader_Load(object sender, EventArgs e)
        {
            logDate.Value = DateTime.Now;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //this is a dummy error.
            Form1.ErrorLog("Dummy Error - 1,2,3,4,5 - 5,4,3,2,1");
        }

        private void button4_Click(object sender, EventArgs e)
        {
            //flush the log
            errorLog.Items.Clear();
            LoadError();
        }
        
        private void LoadError()
        {
            string mylogDate = logDate.Text;
            mylogDate = mylogDate.Replace('/', '-');
            mylogDate = "./logs/" + mylogDate + ".log";
            //MessageBox.Show("Selected Date: " + mylogDate);
            //see if the file exists

            if (File.Exists(mylogDate) == true)
            {
                //load it
                //LoadError("./logs/" + mylogDate + ".log");

                using (StreamReader sr = File.OpenText(mylogDate))
                {
                    string s = "";
                    while ((s = sr.ReadLine()) != null)
                    {
                        errorLog.Items.Add(s);
                    }
                }
            }
            else
            {
                errorLog.Items.Add("No Errors to load for: " + mylogDate);
            }

        }
    }
}
