using System.IO;
using System.Diagnostics;
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.Text.RegularExpressions;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace MioStar_GUI
{
    public partial class MioStarGUI
    {


        BindingList<EventEntry> mEventEntries = new BindingList<EventEntry>();


        /*
         * 
         * 
         */

        public string[] GetLogSource()
        {
            string[] lRetVal;
            string lOutput = "";
            EventLog[] lEventLogs = EventLog.GetEventLogs();

            foreach (EventLog lEventLog in lEventLogs)
                lOutput += lEventLog.Log + ",";

            lOutput = lOutput.TrimEnd(',');
            lRetVal = lOutput.Split(',');

            return (lRetVal);
        }



        /*
         * 
         * 
         */

        public void ListEntries()
        {
            string lLogTime;
            string lLogMessage;
            string lInstanceID;
            string lShortMsg;
            EventLog mEventLogObj = new EventLog();
            mEventLogObj.Log = "Application";
            mEventLogObj.MachineName = ".";
            int lEventCounter = 0;


            foreach (EventLogEntry lEntry in mEventLogObj.Entries)
            {
                if (lEntry.InstanceId >= 1000 && lEntry.InstanceId <= 1010)
                {
                    lLogTime = lEntry.TimeGenerated.ToString();
                    lLogMessage = lEntry.Message;
                    lInstanceID = lEntry.InstanceId.ToString();
                    lShortMsg = lEntry.ReplacementStrings[0].ToString();



                    /*
                     * Give the Instance ID a name. 
                     * 
                     */

                    if (lEntry.InstanceId >= 1000 && lEntry.InstanceId <= 1009)
                        lInstanceID = mSrcCode[lEntry.InstanceId.ToString()].ToString();
                    else
                        lInstanceID = "Undefined";
                    



                    /*
                     * Append item to the data grid view. 
                     * 
                     */

                    if (TextBoxKeyWord.Text.Length > 0)
                    {
                        if (Regex.Match(lLogMessage, TextBoxKeyWord.Text, RegexOptions.IgnoreCase).Success)
                        {
                            mEventEntries.Add(new EventEntry(lLogTime, lInstanceID, lShortMsg, lLogMessage));
                            DataGridViewMain.DataSource = mEventEntries;
                            DataGridViewMain.Refresh();
                        }
                    }
                    else if (TextBoxKeyWord.Text.Length == 0)
                    {
                        mEventEntries.Add(new EventEntry(lLogTime, lInstanceID, lShortMsg, lLogMessage));
                        DataGridViewMain.DataSource = mEventEntries;
                        DataGridViewMain.Refresh();
                    }

                    lEventCounter++;
                }
            }


            LabelNewEvents.Text = "";
            EventCounter = lEventCounter;

            mEventLogObj.Close();

            for (int lCounter = 0; lCounter < 20; lCounter++)
                mEventEntries.Add(new EventEntry("", "", "", ""));


        }



        /*
         * 
         * 
         */

        public void CountEntries()
        {
            EventLog mEventLogObj = new EventLog();
            mEventLogObj.Log = "Application";
            mEventLogObj.MachineName = ".";
            int lEventCounter = 0;


            foreach (EventLogEntry lEntry in mEventLogObj.Entries)
                if (lEntry.InstanceId >= 1000 && lEntry.InstanceId <= 1010)
                    lEventCounter++;


            /*
             * Set NewEvents label.
             * 
             */

            if (EventCounter != lEventCounter)
                LabelNewEvents.Text = "(New events!)";


            mEventLogObj.Close();
        }





        /*
         * 
         * 
         */

        public void ResetFile()
        {
            EventLog mEventLogObj = new EventLog("Application");
            EventCounter = 0;
            TextBoxMain.Text = "";

            mEventLogObj.Clear();
            mEventLogObj.Close();


            for (int lCounter = 0; lCounter < 20; lCounter++)
                mEventEntries.Add(new EventEntry("", "", "", ""));

            DataGridViewMain.FirstDisplayedScrollingRowIndex = 0;
            DataGridViewMain.CurrentCell = DataGridViewMain.Rows[0].Cells[0];
            DataGridViewMain.Rows[0].Selected = true;
            DataGridViewMain.Refresh();

            MessageBox.Show("Log file \"" + mEventLogObj.Log + "\" resetted!", "File resetted", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }



        public int EventCounter
        {
            get { return mEventCounter; }
            set
            {
                mEventCounter = value;
            }
        }

    }
}
