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

namespace GFATask
{
    [Browsable(false), ToolboxItem(false)]
    public partial class ucAdminGeneral : UserControl
    {

        private void CreateGeneralBindings()
        {
            switch_use_ad_groups.Checked = Config.Main.UseActiveDirectoryGroups;
            switch_frontendupdate.Checked = Config.Main.Administration.RemoteFrontEndUpdate;
            rtbDomains.Lines = Config.Main.WebDomains;
        }

        /// <summary>
        /// Erstellt die ToolTips für die Steuerelemente
        /// </summary>
        private void SetToolTips()
        {
            switch_use_ad_groups.MouseEnter += (sender, e) => tooltip.Show("Nutzt die Active Directory Gruppen, um die Apps aus den Listen zu filtern. Wenn nicht aktiv, werden alle Apps (außer die Admin-Liste) angezeigt", sender as Control);
            switch_frontendupdate.MouseEnter += (sender, e) => tooltip.Show("Aktiviert, oder deaktiviert das Aktualisieren aller GFATask Remote-FrontEnds", sender as Control);
            rtbDomains.MouseEnter += (sender, e) => tooltip.Show("Alle aufgeführten Top-Level-Domains in der Liste werden für die Favoriten-Liste als Webseite erkannt", sender as Control);
            mtbPwd.MouseEnter += (sender, e) => tooltip.Show("Ändert das Passwort für den Adminbereich für Nicht-Administratoren", sender as Control);
        }


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

            this.CreateGeneralBindings();
            tbAdGroupAdmins.Text = Config.Main.Administration.ActiveDirectoryGroup;
            tbUserDomain.Text = Config.Main.DomainName;

            tbMailAccountPwd.Text = new string(Config.Main.Administration.Accounts.Mail.SecurePassword.Select(chr => (char)chr).ToArray());
            tbMailAccountUser.Text = Config.Main.Administration.Accounts.Mail.UserName;
            tbMailAccountHost.Text = Config.Main.Administration.Accounts.Mail.Host;
            numMailAccountPort.Value = Config.Main.Administration.Accounts.Mail.SSLPort;

            tbMailMessageFrom.Text = Config.Main.Administration.Accounts.Mail.MailMessage.From.Address;
            tbMailMessageDisplayName.Text = Config.Main.Administration.Accounts.Mail.MailMessage.From.DisplayName;
            tbMailMessageTo.Text = Config.Main.Administration.Accounts.Mail.MailMessage.To;

            tbCTXxmlNode.Text = Config.Main.Citrix.XMLNodes;
            tbCTXicaSelfService.Text = Config.Main.Citrix.ICASelfService;
            tbCTXIconPath.Text = Config.Main.Citrix.Icons;
            tbCTXappsLocation.Text = Config.Main.Citrix.AppsLocation;

            label9.Paint += Designer.DrawHeaderLabel;
            label15.Paint += Designer.DrawHeaderLabel;
            label11.Paint += Designer.DrawHeaderLabel;
            label4.Paint += Designer.DrawHeaderLabel;
            label3.Paint += Designer.DrawHeaderLabel;

            this.SetToolTips();
        }


        private void abtnChangePwd_Click(object sender, EventArgs e)
        {
            string oldpw = new string(Config.Main.Administration.Accounts.Login.SecurePassword.Select(chr => (char)chr).ToArray());
            Config.Main.Administration.Accounts.Login.SecurePassword = mtbPwd.Text.ToArray().Select(chr => (int)chr).ToArray();
            JSON.Save(JSONConfig.Main);

            XObjects.MailService.Send(
                new MailAddress(
                    Config.Main.Administration.Accounts.Mail.MailMessage.From.Address,
                    Config.Main.Administration.Accounts.Mail.MailMessage.From.DisplayName, Encoding.UTF8),
                new MailAddress(Config.Main.Administration.Accounts.Mail.MailMessage.To),
                "GFATask Kennwortänderung des Adminbereichs",
                $"<body style=\"font-family:Segoe UI\">" +
                $"Das Administartionskennwort wurde\n" +
                $"durch den Benutzer <b>{Environment.UserDomainName + @"\" + Environment.UserName}</b>" +
                $"<br> von \"{"<b>" + oldpw + "</b>"}\"" +
                $"<br> in \"{"<b>" + mtbPwd.Text + "</b>"}\" geändert" +
                $"<br><br>" +
                $"Ihr GFATask Administrator" +
                $"</body>"
                );
        }

        private void abtnConfirmCitrix_Click(object sender, EventArgs e)
        {
            Config.Main.Citrix = new Models.Config.Main.CitrixProperties()
            {
                AppsLocation = tbCTXappsLocation.Text,
                ICASelfService = tbCTXicaSelfService.Text,
                Icons = tbCTXIconPath.Text,
                XMLNodes = tbCTXxmlNode.Text
            };
            JSON.Save(JSONConfig.Main);
            XObjects.FrontEndRefresh.Update(new GFATaskStream() { PipeType = NetPipeType.Citrix });
        }

        private void abtnConfirmAD_Click(object sender, EventArgs e)
        {
            Config.Main.UseActiveDirectoryGroups = switch_use_ad_groups.Checked;
            JSON.Save(JSONConfig.Main);
            this.UseActiveDirectory();
            XObjects.FrontEndRefresh.Update(new GFATaskStream() { PipeType = NetPipeType.ActiveDirectory });
        }


        public void UseActiveDirectory()
        {
            if (this.Created)
            {
                Forms.Main.CreateAppListPanels();
                Accessor.SetLastAppListPanel();
            }
        }

        public void ChangeCitrix()
        {
            if (this.Created)
            {
                Creator.AppListPanel(XObjects.AllAppListPanels.Find(panel => panel.AppType == AppType.Citrix), AppLists.Citrix);
            }
        }

        private void abtnConfirmMailAccount_Click(object sender, EventArgs e)
        {
            if (this.Created)
            {
                Config.Main.Administration.Accounts.Mail = new Models.Config.Main.AdministrationProperties.AccountProperties.MailAccount()
                {
                    Host = tbMailAccountHost.Text,
                    MailMessage = new Models.Config.Main.AdministrationProperties.AccountProperties.MailAccount.MailMessageProperties()
                    {
                        From = new Models.Config.Main.AdministrationProperties.AccountProperties.MailAccount.MailMessageProperties.FromProperties()
                        {
                            Address = tbMailMessageFrom.Text,
                            DisplayName = tbMailMessageDisplayName.Text
                        },
                        To = tbMailMessageTo.Text,
                    },
                    SSLPort = (int)numMailAccountPort.Value,
                    SecurePassword = tbMailAccountPwd.Text.ToArray().Select(chr => (int)chr).ToArray(),
                    UserName = tbMailAccountUser.Text
                };
                JSON.Save(JSONConfig.Main);
                XObjects.FrontEndRefresh.Update(new GFATaskStream() { PipeType = NetPipeType.MailAccount });
            }
        }

        public void SetFrontEndUpdate()
        {
            XObjects.FrontEndRefresh.Active = Config.Main.Administration.RemoteFrontEndUpdate;
        }

        private void switch_frontendupdate_CheckStateChanged(object sender, CheckStateEventArgs e)
        {
            Config.Main.Administration.RemoteFrontEndUpdate = switch_frontendupdate.Checked;
            JSON.Save(JSONConfig.Main);
            this.SetFrontEndUpdate();
            XObjects.FrontEndRefresh.Update(new GFATaskStream() { PipeType = NetPipeType.FrontEndUpdate });
        }
    }
}
