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

namespace MLauncher
{
    public partial class Form1 : Form
    {
        public static string CONF_FILE_NAME = "MLauncher.xml";
        private List<ConfItem> conf = new List<ConfItem>();

        public Form1()
        {
            InitializeComponent();
        }

        private void loadConf()
        {
            XmlDocument doc = new XmlDocument();

            try
            {
                doc.Load(CONF_FILE_NAME);
                XmlNodeList nodeList = doc.SelectNodes("/conf/paths");
                foreach (XmlNode node in nodeList)
                {
                    XmlAttribute itemList = node.Attributes["itemList"];
                    if (itemList != null && itemList.Value != "")
                    {
                        ConfItem confItem = new ConfItem();
                        confItem.ItemList = itemList.Value;
                        XmlAttribute seekApp = node.Attributes["seekApp"];
                        if (seekApp != null)
                        {
                            confItem.SeekApp = seekApp.Value;
                        }
                        conf.Add(confItem);
                    }
                }
            }
            catch (FileNotFoundException e)
            {
                Trace.WriteLine(e.Message);
            }
            catch (FileLoadException e)
            {
                Trace.WriteLine(e.Message);
            }
            catch (XmlException e)
            {
                Trace.WriteLine(e.Message);
            }
        }

        private void updateItemList()
        {
            foreach (ConfItem cItem in conf)
            {
                try
                {
                    StreamReader sr = new StreamReader(cItem.ItemList);
                    string line = null;
                    while ((line = sr.ReadLine()) != null)
                    {
                        addItemList(line);
                    }
                }
                catch (FileNotFoundException e)
                {
                    Trace.WriteLine(e.Message);
                }
                catch (FileLoadException e)
                {
                    Trace.WriteLine(e.Message);
                }
            }
        }

        private void addItemList(string path)
        {
            LaunchItem lItem = new LaunchItem();
            lItem.Path = path;

            string[] splitted = path.Split('\\');
            string nameexe = splitted[splitted.Length - 1];
            string name = Regex.Replace(nameexe, "\\.exe$", "");
            lItem.Name = name;

            Icon ic = System.Drawing.Icon.ExtractAssociatedIcon(path);
            Bitmap ib = ic.ToBitmap();
            Bitmap iconImage = new Bitmap(ib, 16, 16);
            lItem.Icon = iconImage;

            this.launchItemBindingSource.Add(lItem);
        }

        private void launch(LaunchItem item)
        {
            Process.Start(item.Path);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            loadConf();
            updateItemList();
        }

        private void dataGridView1_DoubleClick(object sender, EventArgs e)
        {
            launch((LaunchItem)launchItemBindingSource.Current);
        }
    }

    public class LaunchItem
    {
        private Image icon;
        private string name;
        private string path;

        public Image Icon
        {
            get { return icon; }
            set { icon = value; }
        }
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        public string Path
        {
            get { return path; }
            set { path = value; }
        }
    }

    public class ConfItem
    {
        private string itemList;
        private string seekApp;

        public string ItemList
        {
            get { return itemList; }
            set { itemList = value; }
        }
        public string SeekApp
        {
            get { return seekApp; }
            set { seekApp = value; }
        }
    }
}
