using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Diagnostics;
using XWolf.Aero;
using System.Drawing.Imaging;

namespace Launcheroid
{
    public partial class Form1 : XGlassForm
    {
        private int cols;
        private int rows;
        private lLink[] links;
        private Point mPoint;
        private lLink selected;
        private bool inside = false;
        private int scrollindex = 0, scrollheight = -1, scrollbarh = 0;
        private Pen scrollPen = new Pen(Color.BurlyWood);

        public Form1()
        {
            InitializeComponent();
            if (Program.NavMode)
            {
                AllowDrop = false;
                ContextMenuStrip = navMenuStrip;
            }
            MouseWheel += new MouseEventHandler(Form1_MouseWheel);
            if (Glass.Composite)
            {
                FullGlass = true;
            }
            else
            {
                FormBorderStyle = FormBorderStyle.None;
            }
        }

        #region NO AERO

        private Bitmap GetNoaeroBG()
        {
            Point zeropoint = new Point(0, 0);
            Point pos = PointToScreen(zeropoint);
            Size sz = ClientSize;
            Bitmap capture = new Bitmap(sz.Width, sz.Height);
            using (Graphics g = Graphics.FromImage(capture))
            {
                g.CopyFromScreen(pos, zeropoint, sz);
            }
            Bitmap capture2 = new Bitmap(sz.Width / 2, sz.Height / 2);
            using (Graphics g = Graphics.FromImage(capture2))
            {
                g.InterpolationMode = InterpolationMode.HighQualityBilinear;
                g.SmoothingMode = SmoothingMode.HighQuality;
                g.DrawImage(capture, 0, 0, sz.Width / 2, sz.Height / 2);
            }
            capture.Dispose();
            Bitmap noaerobg = new Bitmap(sz.Width, sz.Height);
            using (Graphics g = Graphics.FromImage(noaerobg))
            {
                ColorMatrix matrix = new ColorMatrix();
                matrix.Matrix33 = 0.8f; //opacity 0 = completely transparent, 1 = completely opaque
                ImageAttributes attr = new ImageAttributes();
                attr.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);

                g.InterpolationMode = InterpolationMode.HighQualityBilinear;
                g.SmoothingMode = SmoothingMode.HighQuality;
                g.DrawImage(capture2, new Rectangle(0, 0, sz.Width, sz.Height), 0, 0, sz.Width / 2, sz.Height / 2, GraphicsUnit.Pixel, attr);
            }
            capture2.Dispose();
            return noaerobg;
        }

        #endregion

        private void ReAlign()
        {
            links = Launcher.Links;
            if (links.Length <= 1)
            {
                cols = 1;
                rows = 1;
            }
            else
                if (links.Length == 2)
                {
                    cols = 2;
                    rows = 1;
                }
                else
                {
                    cols = (int)Math.Ceiling(Math.Sqrt(links.Length)) + 1;
                    rows = links.Length / cols;
                    if (links.Length % cols > 0)
                        rows++;
                }
            Size cs = new Size(cols * 138, (rows * 150) + 10);
            if (cs.Height > Screen.PrimaryScreen.WorkingArea.Height)
                if (cols < 8)
                {
                    cols = 8;
                    rows = links.Length / cols;
                    cs = new Size(cols * 138, (rows * 150) + 10);

                }
            if (cs.Height > Screen.PrimaryScreen.WorkingArea.Height)
            {
                cols = 8;
                rows = links.Length / cols;
                if (links.Length % cols > 0)
                    rows++;
                scrollheight = (rows * 150) + 10;
                cs = new Size((cols * 138) + 25, Screen.PrimaryScreen.WorkingArea.Height - 75);
                scrollbarh = scrollheight - cs.Height;
                if (scrollindex > scrollbarh)
                    scrollindex = scrollbarh;
            }
            else
                scrollheight = -1;
            ClientSize = cs;
            if (Top + Height + 10 > Screen.PrimaryScreen.WorkingArea.Bottom)
                Top = Screen.PrimaryScreen.WorkingArea.Bottom - (Height + 10);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            ReAlign();
            int left = Cursor.Position.X - Width / 2;
            int top = Cursor.Position.Y - Height / 2;
            if (top < 10)
                top = 10;
            if (top + Height > Screen.PrimaryScreen.WorkingArea.Height - 10)
                top = Screen.PrimaryScreen.WorkingArea.Height - (10 + Height);
            Top = top;
            if (left < 10)
                left = 10;
            if (left + Width > Screen.PrimaryScreen.WorkingArea.Width - 10)
                left = Screen.PrimaryScreen.WorkingArea.Width - (10 + Width);
            Left = left;
            Text = Launcher.title;
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            g.InterpolationMode = InterpolationMode.HighQualityBilinear;
            g.SmoothingMode = SmoothingMode.HighQuality;
            if (Glass.Composite) // VISTA AERO
            {
                g.Clear(Color.Transparent);
            }
            else
            {// XP
                g.Clear(Color.SkyBlue);
                Bitmap bg = GetNoaeroBG();
                if (bg != null)
                    g.DrawImage(bg, ClientRectangle);
            }

            int x = 0, y = 0;
            int select = -1;
            int index = 0;
            foreach (lLink lnk in links)
            {
                Bitmap ico = lnk.Icon;
                int px = (x * 138) + 5;
                int py = (y * 150) + 5;
                py -= scrollindex;
                bool sel = false;
                if (mPoint != null)
                {
                    sel = ((mPoint.X > px) && (mPoint.X - 128 < px)) &&
                        ((mPoint.Y > py) && (mPoint.Y - 128 < py));
                }
                if (sel)
                {
                    g.DrawImage(GfxTools.SelectionBG, px, py);
                    g.DrawImage(ico, px + 5, py + 5, 118, 118);
                    g.DrawImage(GfxTools.SelectionFG, px, py);
                    select = index;
                }
                else
                    g.DrawImage(ico, px + 1, py + 1, 128, 128);
                GfxTools.DrawString(g, lnk.Name, px + 64, py + 128);
                x++;
                if (x >= cols)
                {
                    x = 0;
                    y++;
                }
                index++;
            }
            if (select == -1)
                selected = null;
            else
                selected = links[select];
            if (scrollheight > -1)
            {
                GfxTools.DrawRoundRect(g, scrollPen, ClientSize.Width - 11, 1, 10, ClientSize.Height - 2, 3);
                int sbh = (ClientSize.Height - 2) - scrollbarh;
                int sbi = scrollindex;
                if (sbh < 10)
                {
                    sbh = 10;
                    sbi = (sbi * (ClientSize.Height - 12)) / scrollbarh;
                }
                GfxTools.FillRoundRect(g, scrollPen.Brush, ClientSize.Width - 11, 1 + sbi, 10, sbh, 3);
            }
        }

        private void Form1_MouseWheel(object sender, MouseEventArgs e)
        {
            scrollindex -= e.Delta / 3;
            if (scrollindex < 0)
                scrollindex = 0;
            if (scrollindex > scrollbarh)
                scrollindex = scrollbarh;
            Invalidate();
        }

        private void Form1_Deactivate(object sender, EventArgs e)
        {
            if (!inside)
                t1.Enabled = true;
            else
                t1.Enabled = false;
        }

        private void Form1_KeyUp(object sender, KeyEventArgs e)
        {

        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
                Close();
        }

        private void t1_Tick(object sender, EventArgs e)
        {
            Close();
        }

        private void Form1_Activated(object sender, EventArgs e)
        {
            t1.Enabled = false;
        }

        private void Form1_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop)) e.Effect = DragDropEffects.Copy;
        }

        private void Form1_DragDrop(object sender, DragEventArgs e)
        {
            string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
            if (files.Length == 0)
                return;
            foreach (string file in files)
                Launcher.Add(file);
            Launcher.Save();
            ReAlign();
            Update();
        }

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            mPoint = new Point(e.X, e.Y);
            Invalidate();
        }

        private void Form1_MouseClick(object sender, MouseEventArgs e)
        {
            if (selected == null)
                return;
            inside = true;
            if (e.Button == MouseButtons.Left)
                if (selected.Open())
                    Close();
            inside = false;
        }

        private void formMenuStrip_Opening(object sender, CancelEventArgs e)
        {
            bool itemen = selected != null;
            borrarToolStripMenuItem.Enabled = itemen;
            propiedadesToolStripMenuItem.Enabled = itemen;
        }

        private void borrarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Launcher.Del(selected);
            Launcher.Save();
            ReAlign();
            Invalidate();
        }

        private void configuraciónToolStripMenuItem_Click(object sender, EventArgs e)
        {
            inside = true;
            FConf.Execute();
            Text = Launcher.title;
            inside = false;
            Focus();
        }

        private void propiedadesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (selected == null)
                return;
            inside = true;
            FItem.Execute(selected);
            inside = false;
            Focus();
            Invalidate();
        }

        private void abrirCarpetaContenedoraToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Process.Start(Text);
            Close();
        }

        private void Form1_Shown(object sender, EventArgs e)
        {
            if (!Glass.Composite)
            {
                Opacity = 0.99;
                Timer updt = new Timer();
                updt.Tick += new EventHandler(updt_Tick);
                updt.Interval = 500;
                updt.Enabled = true;
            }
        }

        void updt_Tick(object sender, EventArgs e)
        {
            Invalidate();
        }
    }
}
