using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Reflection;

namespace Launcheroid
{
    static class Program
    {
        public static string version = "2.0";
        public static string path = Assembly.GetExecutingAssembly().Location;
        public static bool NavMode = false;
        /// <summary>
        /// Punto de entrada principal para la aplicación.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            try
            {
                xwolf.FileAssociation.AssociateMe(".lrd", "Launcheroid", "Enlaces Launcheroid");
            }
            catch
            {

            }
                //Arg[] aargs=Arg.ComputeArguments(args);
            //args = null;

            if (args.Length == 1)
                if (File.Exists(args[0]))
                    Launcheroid.Launcher.FILE_NAME = args[0];
            if (args.Length == 2)
            {
                string cmd = args[0].ToLower();
                switch (cmd)
                {
                    case "nav":
                        NavMode = true;
                        Launcher.title = args[1];
                        Launcher.Navigate(args[1]);
                        break;
                }
            }
            else
                Launcher.Load();
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }

    //public class Arg
    //{
    //    string cmd;
    //    string value;

    //    public static Arg[] ComputeArguments(string[] args)
    //    {
    //        List<Arg> result = new List<Arg>();
    //        int i = 0;
    //        while (i < args.Length)
    //        {
    //            string command = args[i];
    //            if ((command.Length == 3) && (i + 1 < args.Length))
    //            {
    //                Arg a = new Arg();
    //                a.cmd = command.ToUpper();
    //                a.value = args[i + 1];
    //                result.Add(a);
    //            }
    //            else
    //            {
    //                Arg a = new Arg();
    //                a.cmd = "";
    //                a.value = command;
    //                result.Add(a);
    //            }
    //        }
    //        return result.ToArray();
    //    }
    //}

    public static class GfxTools
    {
        #region Selected Bitmaps
        private static Bitmap selectedBG;
        private static Bitmap selectedFG;
        private static Bitmap getSelectedBG()
        {
            if (selectedBG == null)
            {
                selectedBG = new Bitmap(128, 128);
                Brush selBrush = new SolidBrush(Color.LightBlue);
                Graphics g = Graphics.FromImage(selectedBG);
                g.SmoothingMode = SmoothingMode.HighQuality;
                g.CompositingQuality = CompositingQuality.HighQuality;
                FillRoundRect(g, selBrush, 0, 0, 128, 128, 15);
                g.Dispose();
                selBrush.Dispose();
            }
            return selectedBG;
        }
        private static Bitmap getSelectedFG()
        {
            if (selectedFG == null)
            {
                selectedFG = new Bitmap(128, 128);
                Pen selPen = new Pen(Color.Gray);
                selPen.Width = 3;
                selPen.DashStyle = DashStyle.Dash;
                Graphics g = Graphics.FromImage(selectedFG);
                g.SmoothingMode = SmoothingMode.HighQuality;
                g.CompositingQuality = CompositingQuality.HighQuality;
                DrawRoundRect(g, selPen, 5, 5, 118, 118, 10);
                g.Dispose();
                selPen.Dispose();
            }
            return selectedFG;
        }

        public static void DrawRoundRect(Graphics g, Pen p, float x, float y, float width, float height, float radius)
        {
            GraphicsPath gp = new GraphicsPath();
            gp.AddLine(x + radius, y, x + width - (radius * 2), y); // Line
            gp.AddArc(x + width - (radius * 2), y, radius * 2, radius * 2, 270, 90); // Corner
            gp.AddLine(x + width, y + radius, x + width, y + height - (radius * 2)); // Line
            gp.AddArc(x + width - (radius * 2), y + height - (radius * 2), radius * 2, radius * 2, 0, 90); // Corner
            gp.AddLine(x + width - (radius * 2), y + height, x + radius, y + height); // Line
            gp.AddArc(x, y + height - (radius * 2), radius * 2, radius * 2, 90, 90); // Corner
            gp.AddLine(x, y + height - (radius * 2), x, y + radius); // Line
            gp.AddArc(x, y, radius * 2, radius * 2, 180, 90); // Corner
            gp.CloseFigure();
            g.DrawPath(p, gp);
            gp.Dispose();
        }

        public static void FillRoundRect(Graphics g, Brush b, float x, float y, float width, float height, float radius)
        {
            GraphicsPath gp = new GraphicsPath();
            gp.AddLine(x + radius, y, x + width - (radius * 2), y); // Line
            gp.AddArc(x + width - (radius * 2), y, radius * 2, radius * 2, 270, 90); // Corner
            gp.AddLine(x + width, y + radius, x + width, y + height - (radius * 2)); // Line
            gp.AddArc(x + width - (radius * 2), y + height - (radius * 2), radius * 2, radius * 2, 0, 90); // Corner
            gp.AddLine(x + width - (radius * 2), y + height, x + radius, y + height); // Line
            gp.AddArc(x, y + height - (radius * 2), radius * 2, radius * 2, 90, 90); // Corner
            gp.AddLine(x, y + height - (radius * 2), x, y + radius); // Line
            gp.AddArc(x, y, radius * 2, radius * 2, 180, 90); // Corner
            gp.CloseFigure();
            g.FillPath(b, gp);
            gp.Dispose();
        }

        public static Bitmap SelectionBG { get { return getSelectedBG(); } }
        public static Bitmap SelectionFG { get { return getSelectedFG(); } }
        #endregion

        private static Font textFont = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Bold);
        private static Brush textBrush = new SolidBrush(Color.White);
        private static Brush textbgBrush = new SolidBrush(Color.FromArgb(220, 0, 0, 20));

        public static void DrawString(Graphics g, string text, int x, int y)
        {
            int w = (int)g.MeasureString(text, textFont).Width / 2;
            int w2 = w + 5;
            FillRoundRect(g, textbgBrush, x - w2, y, w2 * 2, 16, 5);
            g.DrawString(text, textFont, textBrush, x - w, y);
        }
    }
}
