using System.Collections;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Serialization;

namespace GFATask
{
    public class FrontEndRefreshExecution
    {
        public static void Updated(object sender, FrontEndRefreshEventArgs<GFATaskStream> e)
        {
            if(e.Data.PipeType == NetPipeType.ErrorHandle)
            {
                return;
            }
            else if (e.Data.PipeType == NetPipeType.AppCounter_CountUp)
            {
                Forms.Administration.UcAdminAppClickCounter.RefreshList(e.Data.ClickCounter);
            }
            else if (e.Data.PipeType == NetPipeType.AppCounter_Reset)
            {
                Forms.Administration.UcAdminAppClickCounter.ResetAppCounter(e.Data.ClickCounter);
            }
            else if (e.Data.PipeType == NetPipeType.ClickCounter_Clear)
            {
                Config.AppClickCounter = JSON.Read<Models.ClickCounter>(Paths.AppClickCounter.FullName);
                Forms.Administration.UcAdminAppClickCounter.ClearAppClickCounterList();
            }
            else if (e.Data.PipeType == NetPipeType.MainConfig)
            {
                Config.Main = JSON.Read<Models.Config.Main>(Paths.MainConfig.FullName);
                Forms.Administration.UcAdminDesigns.SetWindowBorder();
                Forms.Administration.UcAdminDesigns.SetNewDesign();
                Application.OpenForms.OfType<FormEx>().ToList().ForEach(f => f.SetDefaultDesign());
            }
            else if(e.Data.PipeType == NetPipeType.Citrix)
            {
                Config.Main = JSON.Read<Models.Config.Main>(Paths.MainConfig.FullName);
                Forms.Administration.UcAdminGeneral.ChangeCitrix();
            }
            else if(e.Data.PipeType == NetPipeType.FrontEndUpdate)
            {
                Config.Main = JSON.Read<Models.Config.Main>(Paths.MainConfig.FullName);
                Forms.Administration.UcAdminGeneral.SetFrontEndUpdate();
            }
            else if (e.Data.PipeType == NetPipeType.ActiveDirectory)
            {
                Config.Main = JSON.Read<Models.Config.Main>(Paths.MainConfig.FullName);
                Forms.Administration.UcAdminGeneral.UseActiveDirectory();
            }
            else if (e.Data.PipeType == NetPipeType.MailAccount)
            {
                Config.Main = JSON.Read<Models.Config.Main>(Paths.MainConfig.FullName);
            }
            else if (e.Data.PipeType == NetPipeType.Edit && e.Data.AppType != System.AppType.Favorites)
            {
                var applist = Accessor.GetAppList(e.Data.AppType);
                Accessor.GetAppList(e.Data.AppType).Apps[Accessor.GetAppList(e.Data.AppType).Apps.FindIndex(app => app.ID == e.Data.App.ID)] = e.Data.App;

                // BUGFIX - Wenn der Sender einen Button aus, oder in eine Gruppe verschiebt, wird dies nicht richtig übertragen...
                // Jetzt wird das gesamte AppListPanel erneuert - UNSCHÖN!
                //Accessor.GetAppListPanel(e.Data.AppType).ReplaceButton(e.Data.App);
                Creator.AppListPanel(Accessor.GetAppListPanel(e.Data.AppType), applist);
            }
            else if (e.Data.PipeType == NetPipeType.Delete && e.Data.AppType != System.AppType.Favorites)
            {
                var applist = Accessor.GetAppList(e.Data.AppType);
                applist.Apps.Remove(e.Data.App);
                Accessor.GetAppListPanel(e.Data.AppType).RemoveButton(e.Data.App);
            }
            else if (e.Data.PipeType == NetPipeType.Create && e.Data.AppType != System.AppType.Favorites)
            {
                var applist = Accessor.GetAppList(e.Data.AppType);
                applist.Apps.Add(e.Data.App);
                Accessor.GetAppListPanel(e.Data.AppType).AddAppButton(e.Data.App);
            }
        }


        public static void OnError(object sender, FrontEndErrorEventArgs e)
        {
            //Log.EventLog.Write("Fehler in FrontEndRefresh", System.LogClassification.MediumError, e.Ex);
        }
    }
}
