﻿namespace MetroFramework.Forms
{
    using MetroFramework.Animation;
    using MetroFramework.Components;
    using MetroFramework.Controls;
    using MetroFramework.Drawing;
    using MetroFramework.Interfaces;
    using MetroFramework.Native;
    using System;
    using System.Drawing;
    using System.Windows.Forms;

    public sealed class MetroTaskWindow : MetroForm
    {
        private bool cancelTimer;
        private readonly int closeTime;
        private readonly MetroPanel controlContainer;
        private int elapsedTime;
        private bool isInitialized;
        private int progressWidth;
        private static MetroTaskWindow singletonWindow;
        private DelayedCall timer;

        public MetroTaskWindow()
        {
            this.controlContainer = new MetroPanel();
            base.Controls.Add(this.controlContainer);
        }

        public MetroTaskWindow(int duration, Control userControl) : this()
        {
            this.controlContainer.Controls.Add(userControl);
            userControl.Dock = DockStyle.Fill;
            this.closeTime = duration * 500;
            if (this.closeTime > 0)
            {
                this.timer = DelayedCall.Start(new DelayedCall.Callback(this.UpdateProgress), 5);
            }
        }

        public static void CancelAutoClose()
        {
            if (singletonWindow != null)
            {
                singletonWindow.CancelTimer = true;
            }
        }

        public static void ForceClose()
        {
            if (singletonWindow != null)
            {
                CancelAutoClose();
                singletonWindow.Close();
                singletonWindow.Dispose();
                singletonWindow = null;
            }
        }

        public static bool IsVisible()
        {
            return ((singletonWindow != null) && singletonWindow.Visible);
        }

        protected override void OnActivated(EventArgs e)
        {
            if (!this.isInitialized)
            {
                this.controlContainer.Theme = base.Theme;
                this.controlContainer.Style = base.Style;
                this.controlContainer.StyleManager = base.StyleManager;
                base.MaximizeBox = false;
                base.MinimizeBox = false;
                base.Movable = false;
                base.TopMost = true;
                base.FormBorderStyle = FormBorderStyle.FixedDialog;
                base.Size = new System.Drawing.Size(400, 200);
                Taskbar taskbar = new Taskbar();
                switch (taskbar.Position)
                {
                    case TaskbarPosition.Left:
                        base.Location = new System.Drawing.Point(taskbar.Bounds.Width + 5, (taskbar.Bounds.Height - base.Height) - 5);
                        break;

                    case TaskbarPosition.Top:
                        base.Location = new System.Drawing.Point((taskbar.Bounds.Width - base.Width) - 5, taskbar.Bounds.Height + 5);
                        break;

                    case TaskbarPosition.Right:
                        base.Location = new System.Drawing.Point((taskbar.Bounds.X - base.Width) - 5, (taskbar.Bounds.Height - base.Height) - 5);
                        break;

                    case TaskbarPosition.Bottom:
                        base.Location = new System.Drawing.Point((taskbar.Bounds.Width - base.Width) - 5, (taskbar.Bounds.Y - base.Height) - 5);
                        break;

                    default:
                        base.Location = new System.Drawing.Point((Screen.PrimaryScreen.Bounds.Width - base.Width) - 5, (Screen.PrimaryScreen.Bounds.Height - base.Height) - 5);
                        break;
                }
                this.controlContainer.Location = new System.Drawing.Point(0, 60);
                this.controlContainer.Size = new System.Drawing.Size(base.Width - 40, base.Height - 80);
                this.controlContainer.Anchor = AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Top;
                base.StyleManager.UpdateOwnerForm();
                this.isInitialized = true;
                new MoveAnimation().Start(this.controlContainer, new System.Drawing.Point(20, 60), TransitionType.EaseInOutCubic, 15);
            }
            base.OnActivated(e);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            using (SolidBrush brush = new SolidBrush(MetroPaint.BackColor.Form(base.Theme)))
            {
                e.Graphics.FillRectangle(brush, new Rectangle(base.Width - this.progressWidth, 0, this.progressWidth, 5));
            }
        }

        public static void ShowTaskWindow(string text, Control userControl)
        {
            ShowTaskWindow(null, text, userControl);
        }

        public static void ShowTaskWindow(string text, Control userControl, int secToClose)
        {
            ShowTaskWindow(null, text, userControl, secToClose);
        }

        public static void ShowTaskWindow(IWin32Window parent, string text, Control userControl)
        {
            ShowTaskWindow(parent, text, userControl, 0);
        }

        public static void ShowTaskWindow(IWin32Window parent, string title, Control userControl, int secToClose)
        {
            if (singletonWindow != null)
            {
                singletonWindow.Close();
                singletonWindow.Dispose();
                singletonWindow = null;
            }
            singletonWindow = new MetroTaskWindow(secToClose, userControl);
            singletonWindow.Text = title;
            singletonWindow.Resizable = false;
            singletonWindow.StartPosition = FormStartPosition.Manual;
            if ((parent != null) && (parent is IMetroForm))
            {
                singletonWindow.Theme = ((IMetroForm) parent).Theme;
                singletonWindow.Style = ((IMetroForm) parent).Style;
                singletonWindow.StyleManager = ((IMetroForm) parent).StyleManager.Clone() as MetroStyleManager;
                if (singletonWindow.StyleManager != null)
                {
                    singletonWindow.StyleManager.OwnerForm = singletonWindow;
                }
            }
            singletonWindow.Show(parent);
        }

        private void UpdateProgress()
        {
            if (this.elapsedTime == this.closeTime)
            {
                this.timer.Dispose();
                this.timer = null;
                base.Close();
            }
            else
            {
                this.elapsedTime += 5;
                if (this.cancelTimer)
                {
                    this.elapsedTime = 0;
                }
                double num = ((double) this.elapsedTime) / (((double) this.closeTime) / 100.0);
                this.progressWidth = (int) (base.Width * (num / 100.0));
                base.Invalidate(new Rectangle(0, 0, base.Width, 5));
                if (!this.cancelTimer)
                {
                    this.timer.Reset();
                }
            }
        }

        public bool CancelTimer
        {
            get
            {
                return this.cancelTimer;
            }
            set
            {
                this.cancelTimer = value;
            }
        }
    }
}

