using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
//using Resources = AppLauncher.Properties.Resources;

namespace RyzStudio.Windows.Forms
{
    public class TForm : Form
    {
        //protected readonly Color borderColour = Color.FromArgb(200, 200, 200);
        //protected readonly int borderWidth = 1;

        //protected readonly int resizeBorderWidth = 4;

        protected readonly Color backColour = Color.FromArgb(250, 250, 250);

        //protected readonly Color titleBackColour = Color.FromArgb(235, 234, 233);
        //protected readonly Color titleBorderColour = Color.FromArgb(200, 198, 196);
        //protected readonly Color titleColour = Color.FromArgb(102, 102, 102);
        //protected readonly int titleBarHeight = 33;
        //protected readonly bool showTitleBarLine = true;
        //protected Font titleFont = null;
        //protected int titleFontTop = 0;

        //protected readonly Size titleBarIconSize = new Size(48, 32);
        //protected readonly int titleBarIconMargin = 0;
        //protected readonly int titleBarIconMarginRight = 0;

        //protected Image appIcon = null;
        //protected const int appIconLeft = 12;
        //protected const int appIconRight = 6;
        //protected int appIconTop = 0;

        protected bool isDragging = false;
        protected Point startPosition = new Point();

        //protected Point startWindowSize = new Point();

        //protected bool enableMinimise { get; set; } = true;
        //protected bool enableMaximise { get; set; } = true;
        //protected bool enableClose { get; set; } = true;
        //protected bool closeOnMinimise { get; set; } = false;

        protected bool isBusy = false;

        private IContainer components;

        public TForm() : base()
        {
            InitializeComponent();

            //if (!this.DesignMode)
            //{
            //    this.FormBorderStyle = FormBorderStyle.None;
            //    this.StartPosition = FormStartPosition.Manual;
            //}

            this.AutoScaleMode = AutoScaleMode.Font;
            this.BackColor = backColour;
            this.FormBorderStyle = FormBorderStyle.Sizable;
            this.Padding = new Padding(0);
            this.DoubleBuffered = true;

            this.MouseDown += new MouseEventHandler(form_MouseDown);
            this.MouseMove += new MouseEventHandler(form_MouseMove);
            this.MouseUp += new MouseEventHandler(form_MouseUp);
            this.PreviewKeyDown += new PreviewKeyDownEventHandler(form_PreviewKeyDown);
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            this.Text = Application.ProductName;

            //initialiseLoadComponent();

            //if (appIcon != null) appIconTop = (int)Math.Floor((decimal)(titleBarHeight - appIcon.Height) / 2) + borderWidth;
            //titleFont = new Font(this.Font.FontFamily, 10F);
            //titleFontTop = (int)Math.Floor((decimal)(titleBarHeight - TextRenderer.MeasureText("#", titleFont).Height) / 2) + borderWidth;

        }

        //protected override void OnMouseClick(MouseEventArgs e)
        //{
        //    base.OnMouseClick(e);

        //    bool isLabel = ((e.Location.X >= 0) && (e.Location.X <= this.Width) && (e.Location.Y >= 0) && (e.Location.Y <= titleBarHeight));

        //    if (e.Button == MouseButtons.Left)
        //    {
        //        if (isLabel)
        //        {
        //            // do nothing
        //        }
        //        else
        //        {
        //            // do nothing
        //        }
        //    }
        //    else if (e.Button == MouseButtons.Right)
        //    {
        //        if (isLabel)
        //        {
        //            if (this.TitleContextMenuStrip != null)
        //            {
        //                this.TitleContextMenuStrip.Show(this, e.Location);
        //            }
        //        }
        //        else
        //        {
        //            // do nothing
        //        }
        //    }
        //}

        //protected override void OnPaint(PaintEventArgs e)
        //{
        //    base.OnPaint(e);

        //    Graphics g = e.Graphics;
        //    Rectangle area = new Rectangle(this.DisplayRectangle.X, this.DisplayRectangle.Y, (this.DisplayRectangle.Width - borderWidth), (this.DisplayRectangle.Height - borderWidth));

        //    // border
        //    g.DrawRectangle(new Pen(borderColour, borderWidth), area);

        //    area.Inflate((-1 * borderWidth), (-1 * borderWidth));

            //g.FillRectangle(new SolidBrush(titleBackColour), area.X, area.Y, (area.Width + area.X), titleBarHeight);

            //if (showTitleBarLine) g.DrawLine(new Pen(titleBorderColour, 1), area.X, titleBarHeight, (area.Width + area.X), titleBarHeight);

            //if (!DesignMode)
            //{
            //    if (appIcon != null) g.DrawImageUnscaled(appIcon, appIconLeft, appIconTop);

            //    int iconPosX = borderWidth + appIconLeft + appIconRight + ((appIcon == null) ? 0 : appIcon.Width);
            //    TextRenderer.DrawText(g, this.Text, titleFont, new Point(iconPosX, titleFontTop), titleColour);
            //}
        //}

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);

            this.Invalidate();
        }

        [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
        public override Color BackColor { get => base.BackColor; set => base.BackColor = backColour; }

        [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
        public new FormBorderStyle FormBorderStyle { get => base.FormBorderStyle; set => base.FormBorderStyle = value; }

        [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
        public new Padding Padding { get => base.Padding; set => base.Padding = value; }

        [Category("Appearance")]
        public ContextMenuStrip TitleContextMenuStrip { get; set; } = null;

        //[Browsable(false)]
        //public Image AppIcon { get; set; } = null;

        //[Browsable(false)]
        //public bool IsMaximiseEnabled { get; set; } = false;

        //protected Point DefaultLocation
        //{
        //    get
        //    {
        //        Point newPosition = new Point(Cursor.Position.X, Cursor.Position.Y);
        //        newPosition.X -= (this.Width / 2);
        //        newPosition.Y -= (this.Height / 2);

        //        newPosition.X = Math.Max(newPosition.X, Screen.PrimaryScreen.WorkingArea.Left);
        //        newPosition.Y = Math.Max(newPosition.Y, Screen.PrimaryScreen.WorkingArea.Top);

        //        return newPosition;
        //    }
        //}

        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(TForm));
            this.SuspendLayout();
            //
            // AForm
            //
            this.Name = "AForm";
            this.ResumeLayout(false);
        }

        private void form_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                isDragging = true;
                startPosition = e.Location;
                //startWindowSize = new Point(this.Width, this.Height);
            }
        }

        private void form_MouseMove(object sender, MouseEventArgs e)
        {
            if (isDragging)
            {
                int x = (this.Location.X + (e.Location.X - startPosition.X));
                int y = (this.Location.Y + (e.Location.Y - startPosition.Y));

                //this.Location = validateFormLocation(x, y);
                this.Location = new Point(x, y);
            }
        }

        private void form_MouseUp(object sender, MouseEventArgs e)
        {
            isDragging = false;
        }

        private void form_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                Application.Exit();
            }
        }

        //protected virtual void initialiseLoadComponent()
        //{
        //    if (DesignMode) return;

        //    // close
        //    TImageBox closeBox = generateToolbarImageBox();
        //    closeBox.Image = closeBox.ImageNormal = (enableClose) ? Resources.titlebar_close : Resources.titlebar_blank;
        //    closeBox.ImageHover = (enableClose) ? Resources.titlebar_close3 : Resources.titlebar_blank;
        //    closeBox.ImageSelected = Resources.titlebar_close3;
        //    closeBox.MouseClick += delegate (object sender, MouseEventArgs e)
        //    {
        //        if (e.Button == MouseButtons.Left)
        //        {
        //            if (enableClose)
        //            {
        //                this.Close();
        //            }
        //        }
        //    };
        //    closeBox.Left = this.DisplayRectangle.Width - closeBox.Width - (titleBarIconMarginRight + borderWidth);

        //    this.Controls.Add(closeBox);

        //    // maximise
        //    TImageBox maximiseBox = generateToolbarImageBox();
        //    maximiseBox.Image = maximiseBox.ImageNormal = (enableMaximise) ? Resources.titlebar_maximise : Resources.titlebar_blank;
        //    maximiseBox.ImageHover = (enableMaximise) ? Resources.titlebar_maximise5 : Resources.titlebar_blank;
        //    maximiseBox.ImageSelected = Resources.titlebar_maximise3;
        //    maximiseBox.MouseClick += delegate (object sender, MouseEventArgs e)
        //    {
        //        if (!(sender is TImageBox)) return;

        //        TImageBox imageBox2 = (sender as TImageBox);

        //        if (imageBox2 == null) return;

        //        if (e.Button == MouseButtons.Left)
        //        {
        //            if (enableMaximise)
        //            {
        //                if (this.WindowState == FormWindowState.Maximized)
        //                {
        //                    this.WindowState = FormWindowState.Normal;
        //                    imageBox2.Image = imageBox2.ImageNormal = Resources.titlebar_maximise;
        //                }
        //                else
        //                {
        //                    this.WindowState = FormWindowState.Maximized;
        //                    imageBox2.Image = imageBox2.ImageNormal = Resources.titlebar_maximise5;
        //                }
        //            }
        //        }
        //        else if (e.Button == MouseButtons.Right)
        //        {
        //            this.TopMost = !this.TopMost;

        //            if (this.TopMost)
        //            {
        //                imageBox2.Image = imageBox2.ImageNormal = Resources.titlebar_maximise3;
        //                imageBox2.ImageHover = (enableMaximise) ? Resources.titlebar_maximise5 : Resources.titlebar_maximise3;
        //            }
        //            else
        //            {
        //                imageBox2.Image = imageBox2.ImageNormal = (enableMaximise) ? Resources.titlebar_maximise : Resources.titlebar_blank;
        //                imageBox2.ImageHover = (enableMaximise) ? Resources.titlebar_maximise5 : Resources.titlebar_blank;
        //            }
        //        }
        //    };
        //    maximiseBox.Left = closeBox.Left - maximiseBox.Width - titleBarIconMargin;

        //    this.Controls.Add(maximiseBox);

        //    // minimise
        //    TImageBox minimiseBox = generateToolbarImageBox();
        //    minimiseBox.Image = minimiseBox.ImageNormal = (enableMinimise) ? Resources.titlebar_minimise : Resources.titlebar_blank;
        //    minimiseBox.ImageHover = (enableMinimise) ? Resources.titlebar_minimise5 : Resources.titlebar_blank;
        //    //minimiseBox.ImageSelected = null;
        //    minimiseBox.MouseClick += delegate (object sender, MouseEventArgs e)
        //    {
        //        if (!enableMinimise) return;
        //        if (e.Button != MouseButtons.Left) return;

        //        if (closeOnMinimise)
        //        {
        //            this.Close();
        //        }
        //        else
        //        {
        //            this.WindowState = FormWindowState.Minimized;
        //        }
        //    };
        //    minimiseBox.Left = maximiseBox.Left - minimiseBox.Width - titleBarIconMargin;

        //    this.Controls.Add(minimiseBox);

        //    // resize
        //    UserControl uc1 = new UserControl()
        //    {
        //        Anchor = (AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Right),
        //        Height = resizeBorderWidth,
        //        Width = this.DisplayRectangle.Width - resizeBorderWidth,
        //        Left = 0,
        //        Top = this.DisplayRectangle.Height - resizeBorderWidth,
        //        BackColor = Color.Transparent,
        //        Cursor = Cursors.SizeNS
        //    };
        //    uc1.MouseDown += form_MouseDown;
        //    uc1.MouseUp += form_MouseUp;
        //    uc1.MouseMove += delegate (object sender, MouseEventArgs e)
        //    {
        //        if (isDragging)
        //        {
        //            this.Size = new Size(startWindowSize.X, e.Y - startPosition.Y + this.Height);
        //        }
        //    };
        //    uc1.BringToFront();

        //    this.Controls.Add(uc1);

        //    UserControl uc2 = new UserControl()
        //    {
        //        Anchor = (AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Bottom),
        //        Height = this.DisplayRectangle.Height - resizeBorderWidth,
        //        Width = resizeBorderWidth,
        //        Left = this.DisplayRectangle.Width - resizeBorderWidth,
        //        Top = 0,
        //        BackColor = Color.Transparent,
        //        Cursor = Cursors.SizeWE
        //    };
        //    uc2.MouseDown += form_MouseDown;
        //    uc2.MouseUp += form_MouseUp;
        //    uc2.MouseMove += delegate (object sender, MouseEventArgs e)
        //    {
        //        if (isDragging)
        //        {
        //            this.Size = new Size(e.X - startPosition.X + this.Width, startWindowSize.Y);
        //        }
        //    };
        //    uc2.BringToFront();

        //    this.Controls.Add(uc2);

        //    UserControl uc3 = new UserControl()
        //    {
        //        Anchor = (AnchorStyles.Bottom | AnchorStyles.Right),
        //        Height = resizeBorderWidth,
        //        Width = resizeBorderWidth,
        //        Left = this.DisplayRectangle.Width - resizeBorderWidth,
        //        Top = this.DisplayRectangle.Height - resizeBorderWidth,
        //        BackColor = Color.Transparent,
        //        Cursor = Cursors.SizeNWSE
        //    };
        //    uc3.MouseDown += form_MouseDown;
        //    uc3.MouseUp += form_MouseUp;
        //    uc3.MouseMove += delegate (object sender, MouseEventArgs e)
        //    {
        //        if (isDragging)
        //        {
        //            this.Size = new Size((e.X - startPosition.X + this.Width), (e.Y - startPosition.Y + this.Height));
        //        }
        //    };
        //    uc3.BringToFront();

        //    this.Controls.Add(uc3);
        //}

        //protected TImageBox generateToolbarImageBox()
        //{
        //    TImageBox imageBox = new TImageBox();
        //    //imageBox.BackColor = Color.Transparent;
        //    imageBox.BackColorHover = imageBox.BackColorSelected = Color.FromArgb(220, 220, 220);
        //    imageBox.BackgroundImageLayout  = ImageLayout.Center;
        //    imageBox.ErrorImage = null;
        //    //imageBox.Image = Resources.close;
        //    //imageBox.ImageHover = Resources.close2;
        //    //imageBox.ImageNormal = Resources.close;
        //    imageBox.ImageSelected = null;
        //    imageBox.IsSelected = false;
        //    //closeBox.Location = new System.Drawing.Point(169, 12);
        //    imageBox.Size = titleBarIconSize;
        //    imageBox.SizeMode = PictureBoxSizeMode.CenterImage;
        //    //imageBox.MouseClick += new MouseEventHandler(closeBox_MouseClick);
        //    //imageBox.Left = this.DisplayRectangle.Width - imageBox.Width - 17;
        //    imageBox.Top = (int)Math.Floor((decimal) (titleBarHeight - titleBarIconSize.Height) / 2) + borderWidth;
        //    imageBox.Anchor = (AnchorStyles.Top | AnchorStyles.Right);
        //    imageBox.Padding = new Padding(0);

        //    return imageBox;
        //}

        //private void exitToolStripMenuItem_Click(object sender, EventArgs e) => this.Close();

        //private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
        //{
        //    if (e.Button == MouseButtons.Left)
        //    {
        //        this.Visible = !this.Visible;
        //    }

        //    //notifyIcon1.Visible = !this.Visible;
        //}


    }
}