using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GFATask
{
    public partial class FormAdmin : FormEx
    {
        public ucAdminApps UcAdminApps { get; } = new ucAdminApps() { Dock = DockStyle.Fill };
        public ucAdminGeneral UcAdminGeneral { get; } = new ucAdminGeneral() { Dock = DockStyle.Fill };
        public ucAdminNewsFeeds UcAdminNewsFeeds { get; } = new ucAdminNewsFeeds() { Dock = DockStyle.Fill };
        public ucAdminDesigns UcAdminDesigns { get; } = new ucAdminDesigns() { Dock = DockStyle.Fill };
        public ucAdminHelp UcAdminHelp { get; } = new ucAdminHelp() { Dock = DockStyle.Fill };
        public ucAdminAppClickCounter UcAdminAppClickCounter { get; } = new ucAdminAppClickCounter() { Dock = DockStyle.Fill };

        private AnimationButton clickedbutton = null;
        
        int loginfails = 0;

        public FormAdmin()
        {
            InitializeComponent();
            this.SetDefaultDesign();


            panelUC.Controls.AddRange(new Control[] { UcAdminApps, UcAdminGeneral, UcAdminNewsFeeds, UcAdminDesigns, UcAdminHelp, UcAdminAppClickCounter });
            this.Icon = Icon.FromHandle(new Bitmap(File.OpenRead(Paths.Icons.Directory + @"System\administration.png")).GetHicon());
            Accumulations.Controls(flpAdminMenu).OfType<AnimationButton>().ToList().ForEach(button =>
            {
                button.MouseClick += OpenUserControl;
                button.Paint += Button_Paint;
            });
            OpenUserControl(abtnApps, new MouseEventArgs(MouseButtons.Left, 0, 0, 0, 0));
            

        }

        private void Button_Paint(object sender, PaintEventArgs e)
        {
            if((clickedbutton != null && (sender as AnimationButton).Equals(clickedbutton) && (sender as AnimationButton).BorderSize == 0))
            {
                e.Graphics.DrawLine(new Pen(Config.Main.Colors.Apps.Hover.Border, 3), new Point(0, e.ClipRectangle.Bottom - 3 ), new Point(e.ClipRectangle.Right, e.ClipRectangle.Bottom - 3));
            }
        }

        private void OpenUserControl(object sender, MouseEventArgs e)
        {
            if(e.Button == MouseButtons.Left)
            {
                var button = sender as AnimationButton;
                clickedbutton = button;
                Accumulations.Controls(this).OfType<AnimationButton>().ToList().ForEach(btn => btn.Invalidate());
                if (button.BorderSize > 0 || button.BorderColor == button.BackColor)
                {
                    button.BorderColor = Color.Green;
                    Accumulations.Controls(this).OfType<AnimationButton>().Where(btn => btn != button).ToList().ForEach(btn => btn.BorderColor = Config.Main.Colors.Apps.Default.Border);
                }
                panelUC.Controls[button.Tag.ToString()].Dock = DockStyle.Fill;
                panelUC.Controls[button.Tag.ToString()].BringToFront();
                panelUC.Controls[button.Tag.ToString()].Invalidate();
            }
        }

        


        public void Login()
        {
            Task.Run(delegate 
            {
                Forms.Main.Invoke(new MethodInvoker(async delegate
                {
                    if (Forms.AppButtonGroup != null && Forms.AppButtonGroup.Visible)
                        Forms.AppButtonGroup.Close();

                    if (await XObjects.IsAdmin)
                    {
                        this.Visible = true;
                        return;
                    }
                   
                    var msgbox = XObjects.MessageBox(
                        loginfails == 0 ? "Administratorkennwort" :
                        $"Das Kennwort war falsch. Nochmal!", NotifyButtons.OkCancel, NotifyType.Login, new Size(250, 200), "Administrationsbereich", null);

                    if (msgbox.Result == DialogResult.OK)
                    {
                        if (msgbox.Value.ToString() == new string(Config.Main.Administration.Accounts.Login.SecurePassword.Select(chr => (char)chr).ToArray()))
                        {
                            this.Visible = true;
                        }
                        else
                        {
                            loginfails++;
                            this.Login();
                        }
                    }
                    else if (msgbox.Result == DialogResult.Cancel)
                    {
                        return;
                    }
                }));
            });
        }

        protected override void OnVisibleChanged(EventArgs e)
        {
            base.OnVisibleChanged(e);
            if (this.Visible)
            {
                XObjects.Delay.Increase();
                this.TopMost = true;
            }
        }


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

            flpAdminMenu.SetDefaultDesign();
            flpAdminMenu.Controls.OfType<AnimationButton>().ToList().ForEach(abtn => abtn.SetDefaultDesign());
        }


        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            if(e.CloseReason == CloseReason.UserClosing)
                this.Visible = false;
            e.Cancel = e.CloseReason == CloseReason.UserClosing;
            base.OnFormClosing(e);
        }
    }
}
