using System;
using System.Collections.Generic;
using System.Reflection;
using System.Xml;

using Newtonsoft.Json;

using C4IT.Configuration;
using C4IT.FASD.Base;
using C4IT.Logging;
using C4IT.XML;

using static C4IT.Logging.cLogManager;
using static C4IT.Configuration.cConfigRegistryHelper;

namespace C4IT.DataHistoryProvider
{
    public class cDataHistoryConfigGolabalParameters : cFasdBaseConfig
    {
        public const string constFileNameF4sdConfig = "F4SD-Global-Configuration.xml";
        private const string constFileNameF4sdSchema = "F4SD-Global-Configuration.xsd";
        private const string constConfigRootElement = "F4SD-Global-Configuration";

        public cConfigHelperParameterList Parameters { get; private set; } = null;

        private cDataHistoryConfigInfrastructure InfrastructureConfig = null;

        internal cDataHistoryConfigGolabalParameters(cDataHistoryConfigInfrastructure configInfrastructure) :
            base(constFileNameF4sdConfig, constFileNameF4sdSchema, constConfigRootElement, true)
        {
            InfrastructureConfig = configInfrastructure;
        }

        public override bool InstantiateProperties(XmlElement RootElement, cXmlParser Parser)
        {
            MethodBase CM = null; if (cLogManager.DefaultLogger.IsDebug) { CM = MethodBase.GetCurrentMethod(); LogMethodBegin(CM); }

            try
            {
                Parameters = LoadParameters(RootElement, Parser );

                var _s = JsonConvert.SerializeObject(Parameters, Newtonsoft.Json.Formatting.Indented);

                this.IsValid = true;

                return true;
            }
            catch (Exception E)
            {
                LogException(E);
            }
            finally
            {
                if (CM != null) LogMethodEnd(CM);
            }

            return false;

        }

        private cConfigHelperParameterList LoadParameters (XmlElement XRoot, cXmlParser Parser)
        {
            MethodBase CM = null; if (cLogManager.DefaultLogger.IsDebug) { CM = MethodBase.GetCurrentMethod(); LogMethodBegin(CM); }
            try
            {
                return cF4sdGlobalConfigParameterParser.Parse(XRoot, Parser);
            }
            catch (Exception E)
            {
                LogException(E);
            }
            finally
            {
                if (CM != null) LogMethodEnd(CM);
            }

            return new cConfigHelperParameterList();
        }

        public override bool DoXmlUpdates(XmlElement XmlRoot, bool withM42Config = false, bool withIntuneConfig = false, bool withMobileDeviceConfig = false, bool withCitrixConfig = false)
        {
            MethodBase CM = null; if (cLogManager.DefaultLogger.IsDebug) { CM = MethodBase.GetCurrentMethod(); LogMethodBegin(CM); }

            var RetVal = false;
            try
            {
                RetVal |= DoXmlInsertElement(XmlRoot
                     , ""
                     , "ShouldSkipSlimView"
                     , "<ShouldSkipSlimView Policy=\"Default\" Value=\"false\" />"
                     );

                RetVal |= DoXmlInsertElement(XmlRoot
                     , ""
                     , "SmallViewAlignment"
                     , "<SmallViewAlignment Policy=\"Default\" Value=\"Right\" />"
                     );

                RetVal |= DoXmlInsertElement(XmlRoot
                     , ""
                     , "FavouriteBarAlignment"
                     , "<FavouriteBarAlignment Policy=\"Default\" Value=\"Right\" />"
                     );

                RetVal |= DoXmlInsertElement(XmlRoot
                     , ""
                     , "InformationClassSearchPriority"
                     , "<InformationClassSearchPriority Policy=\"Default\" />"
                     );

                RetVal |= DoXmlInsertElement(XmlRoot
                     , "InformationClassSearchPriority"
                     , "InformationClass[@Type='User']"
                     , "<InformationClass Type=\"User\" />"
                     );

                RetVal |= DoXmlInsertElement(XmlRoot
                     , "InformationClassSearchPriority"
                     , "InformationClass[@Type='Computer']"
                     , "<InformationClass Type=\"Computer\" />"
                     );

                RetVal |= DoXmlInsertElement(XmlRoot
                     , "InformationClassSearchPriority"
                     , "InformationClass[@Type='VirtualSession']"
                     , "<InformationClass Type=\"VirtualSession\" />"
                     );

                RetVal |= DoXmlInsertElement(XmlRoot
                     , "InformationClassSearchPriority"
                     , "InformationClass[@Type='Ticket']"
                     , "<InformationClass Type=\"Ticket\" />"
                     );
                RetVal |= DoXmlInsertElement(XmlRoot
                     , ""
                     , "TicketConfiguration"
                     , "<TicketConfiguration />"
                     );
                RetVal |= DoXmlInsertElement(XmlRoot
                     , "TicketConfiguration"
                     , "NotesMandatory"
                     , "<NotesMandatory Policy=\"Hidden\" Value=\"false\" />"
                     );
                RetVal |= DoXmlInsertElement(XmlRoot
                     , "TicketConfiguration"
                     , "ShowOverview"
                     , "<ShowOverview Policy=\"Hidden\" Value=\"true\" />"
                     );
                RetVal |= DoXmlInsertElement(XmlRoot
                     , "TicketConfiguration"
                     , "OpenActivitiesExternally"
                     , "<OpenActivitiesExternally Policy=\"Hidden\" Value=\"false\" />"
                     );
                RetVal |= DoXmlInsertElement(XmlRoot
                     , "TicketConfiguration/OpenActivitiesExternally"
                     , "OpenActivityOverride[@ActivityType='SPSActivityTypeTicket']"
                     , "<OpenActivityOverride ActivityType=\"SPSActivityTypeTicket\" Value=\"false\" />"
                     );
                RetVal |= DoXmlInsertElement(XmlRoot
                     , "TicketConfiguration/OpenActivitiesExternally"
                     , "OpenActivityOverride[@ActivityType='SPSActivityTypeServiceRequest']"
                     , "<OpenActivityOverride ActivityType=\"SPSActivityTypeServiceRequest\" Value=\"true\" />"
                     );
                RetVal |= DoXmlInsertElement(XmlRoot
                     , "TicketConfiguration"
                     , "OverviewPollingPersonal"
                     , $"<OverviewPollingPersonal Policy=\"Hidden\" Value=\"{cF4sdTicketConfig.DefaultOverviewPollingPersonal}\" />"
                     );
                RetVal |= DoXmlInsertElement(XmlRoot
                     , "TicketConfiguration"
                     , "OverviewPollingRole"
                     , $"<OverviewPollingRole Policy=\"Hidden\" Value=\"{cF4sdTicketConfig.DefaultOverviewPollingRole}\" />"
                     );
#pragma warning disable CS0618 // Type or member is obsolete
                var oldShowDocumentCaseDialog = InfrastructureConfig?.M42Wpm?.ShowDocumentCaseDialog ?? enumShowDocumentCaseDialog.ifRequired;
                var oldDisableAutomaticTimeTracking = InfrastructureConfig?.M42Wpm?.DisableAutomaticTimeTracking ?? false;
#pragma warning restore CS0618 // Type or member is obsolete
                RetVal |= DoXmlInsertElement(XmlRoot
                     , "TicketConfiguration"
                     , "CompletitionPolicy"
                     , $"<CompletitionPolicy Policy=\"Hidden\" Value=\"{oldShowDocumentCaseDialog.ToString()}\" />"
                     );
                RetVal |= DoXmlInsertElement(XmlRoot
                     , "TicketConfiguration"
                     , "DisableAutomaticTimeTracking"
                     , $"<DisableAutomaticTimeTracking Policy=\"Mandatory\" Value=\"{oldDisableAutomaticTimeTracking.ToString()}\" />"
                     );
            }

            catch (Exception E)
            {
                LogException(E);
            }
            finally
            {
                if (CM != null) LogMethodEnd(CM);
            }

            return RetVal;
        }

    }


}
