using System;
using System.IO;
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.Diagnostics;
using System.Runtime.Serialization.Formatters.Binary;

namespace Windows_7_Gaming_Compatibility
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();

            // *** Deserialize ***
            if (File.Exists("Windows 7 Gaming Compatibility.txt"))
            {
                FileStream listFile = File.Open("Windows 7 Gaming Compatibility.txt", FileMode.Open, FileAccess.Read);
                BinaryFormatter formatter = new BinaryFormatter();

                // Retrieve the item count
                int listLength = (int)formatter.Deserialize(listFile);

                for (int i = 0; i < listLength; i++)
                {
                    object obj = formatter.Deserialize(listFile);
                    ListViewItem item = (ListViewItem)obj;
                    AppList.Items.Add(item);
                }

                listFile.Close();
            }
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            // *** Serialize ***
            FileStream listFile = File.Open("Windows 7 Gaming Compatibility.txt", FileMode.Create, FileAccess.Write);
            BinaryFormatter formatter = new BinaryFormatter();

            // Keep count to make Deserializing simple.
            formatter.Serialize(listFile, AppList.Items.Count);

            for (int i = 0; i < AppList.Items.Count; i++)
            {
                ListViewItem item = AppList.Items[i];
                formatter.Serialize(listFile, item);
            }

            listFile.Close();
        } 

        private void KillExplorer_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("taskkill", "/f /im explorer.exe");
        }

        private void RunExplorer_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("explorer");
        }

        private void VolumeControl_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("sndvol");
        }

        private void AppRun_Click(object sender, EventArgs e)
        {
            if(AppList.SelectedItems.Count > 0)
                System.Diagnostics.Process.Start(AppList.SelectedItems[0].Text);
        }

        private void AppAdd_Click(object sender, EventArgs e)
        {            
            if (AppBrowse.ShowDialog() == DialogResult.OK)
            {
                String path;
                path = AppBrowse.FileName;
                ListViewItem newItem = new ListViewItem();
                newItem.Text = path;
                AppList.Items.Add(newItem);
            }
        }

        private void AppRemove_Click(object sender, EventArgs e)
        {
            if (AppList.SelectedItems.Count > 0)
                AppList.Items.Remove(AppList.SelectedItems[0]);
        }       
    }
}
