using System;
using System.IO;
using System.Diagnostics;
using System.Collections; 
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MioStar_GUI
{
    public partial class MioStarGUI : Form
    {
        private int mEventCounter;
        private string mRulesPath;
        private Hashtable mSrcCode;
        private Process mMioStarProc;
        private bool mMioStarRunning;
        private string mMioStarDirPath;


        public MioStarGUI()
        {
            string lDonateReq = "";
            string lLine = "";
            FileInfo lFileInfo;
            StreamReader lReader;


            


            /*
             * Create Codes Hash table. 
             * 
             */

            mSrcCode = new Hashtable();
            mSrcCode.Add("1000", "Default");
            mSrcCode.Add("1001", "File");
            mSrcCode.Add("1002", "Registry");
            mSrcCode.Add("1003", "Socket");
            mSrcCode.Add("1004", "HTTP");
            mSrcCode.Add("1005", "Crypt");
            mSrcCode.Add("1006", "DSound");
            mSrcCode.Add("1007", "WinMM");
            mSrcCode.Add("1008", "MP3");
            mSrcCode.Add("1009", "System");


            InitializeComponent();
            EventCounter = 0;
            ButtonRefresh_Click(null, null);


            /*
             * Determine MioStar directory or create it if necessary.
             * 
             */

            mMioStarDirPath = Directory.GetCurrentDirectory();
            RegFUnctions.SetValue("MioStarPath", mMioStarDirPath);
            mRulesPath = mMioStarDirPath + "\\RulesFile.txt";


            /*
             * Read in Rules file
             * 
             */

            LabelRulesPath.Text = mRulesPath;
            if (File.Exists(mRulesPath))
            {
                lFileInfo = new FileInfo(mRulesPath);

                if (mRulesPath.Length > 0)
                {
                    try
                    {
                        lReader = new StreamReader(mRulesPath);

                        while ((lLine = lReader.ReadLine()) != null)
                            TextBoxRules.AppendText(lLine + Environment.NewLine);

                        lReader.Close();
                    }
                    catch (Exception exept)
                    {
                        MessageBox.Show("Can't read " + mRulesPath + " : " + exept.ToString() + "!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }



            /*
             * If there are events in the list, write the first entry
             * into the Message text box.
             * 
             */

            try
            {
                if (DataGridViewMain.Rows.Count > 0)
                    TextBoxMain.Text = DataGridViewMain.Rows[1].Cells[2].Value.ToString();
            }
            catch 
            { 
            }


            /*
             * Donate a request.
             * 
             */

            lDonateReq = RegFUnctions.GetValue("DonateRequest");
            if (lDonateReq == "1")
                DonateRequest.SendRequest("http://www.megapanzer.com/DonateRequest/miostar.php");
            else if (lDonateReq == "0")
               lDonateReq = ""; // do nothing.
            else
                Application.Run(new DonateRequest());
        }



        private void ButtonEmpty_Click(object sender, EventArgs e)
        {
            ResetFile();
            ButtonRefresh_Click(sender, e);
        }



        private void ButtonClose_Click(object sender, EventArgs e)
        {
            Dispose();
        }



        private void ButtonRefresh_Click(object sender, EventArgs e)
        {
            int lFirstRow = -1;
            int lSelectedRow = -1;

            /*
             * Save position and last selected row.
             * 
             */

            if (DataGridViewMain.CurrentRow != null)
            {
                lSelectedRow = DataGridViewMain.CurrentRow.Index;
                lFirstRow = DataGridViewMain.FirstDisplayedScrollingRowIndex;
            }



            /*
             * Clear event log entry table.
             * 
             */

            mEventEntries.Clear();
            DataGridViewMain.DataSource = mEventEntries;
            DataGridViewMain.Refresh();


            /*
             * List event log entries
             * 
             */

            ButtonRefresh.Enabled = false;
            ButtonClose.Enabled = false;
            ButtonRefresh.Enabled = false;
            ButtonEmpty.Enabled = false;
            Cursor = System.Windows.Forms.Cursors.WaitCursor;

            ListEntries();

            ButtonRefresh.Enabled = true;
            ButtonClose.Enabled = true;
            ButtonRefresh.Enabled = true;
            ButtonEmpty.Enabled = true;
            Cursor = System.Windows.Forms.Cursors.Default;

            if (lSelectedRow >= 0)
            {
                DataGridViewMain.FirstDisplayedScrollingRowIndex = lFirstRow;
                DataGridViewMain.CurrentCell = DataGridViewMain.Rows[lSelectedRow].Cells[0];
                DataGridViewMain.Rows[lSelectedRow].Selected = true;
                DataGridViewMain.Refresh();
            }
            		

        }



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



        private void ButtonSave_Click(object sender, EventArgs e)
        {
            StreamWriter lWriter;

            try
            {
                using (lWriter = new StreamWriter(mRulesPath))
                {
                lWriter.WriteLine(TextBoxRules.Text);
                lWriter.Close();
                    }
            }
            catch (Exception exept)
            {
                MessageBox.Show("Can't write to " + mRulesPath + " : " + exept.ToString() + "!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }



        private void DataGridViewMain_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            int lRow = e.RowIndex;

            try
            {
                TextBoxMain.Text = this.DataGridViewMain.Rows[lRow].Cells[3].Value.ToString();
            }
            catch
            {
            }

        }

        private void ButtonStartStop_Click(object sender, EventArgs e)
        {
            if (ButtonStartStop.Text.ToString() == "Start hooking")
            {
                mMioStarProc = new Process();
                // Configure the process using the StartInfo properties.
                mMioStarProc.StartInfo.FileName = "DLLInjector.exe";
                mMioStarProc.StartInfo.Arguments = "MioStar.dll 1";
                mMioStarProc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                mMioStarProc.Start();
                ButtonStartStop.Text = "Stop hooking";
                mMioStarRunning = true;
            }
            else
            {
                mMioStarProc.Kill();
                ButtonStartStop.Text = "Start hooking";
                mMioStarRunning = false;
            }
        }

    }

}





