using System;
using System.Collections.Generic;
using System.Windows;

using C4IT.F4SD.DisplayFormatting;
using C4IT.F4SD.SupportCaseProtocoll.Models;
using C4IT.FASD.Base;

using FasdCockpitBase;
using FasdDesktopUi.Basics.Services.ProtocollService;
using static C4IT.Logging.cLogManager;

namespace FasdDesktopUi.Basics.Models
{
    public abstract class cNamedParameterEntryBase : FasdCockpitBase.cExternalToolExecutor.iNamedParameter
    {
        internal readonly cSupportCaseDataProvider dataProvider;

        public string Title { get; set; }

        public cNamedParameterEntryBase(cSupportCaseDataProvider dataProvider)
        {
            this.dataProvider = dataProvider;
        }

        public abstract string GetValue();
        public abstract string GetHtmlValue();
        public virtual (string Title, string Value) GetTitleValuePair()
        {
            return (Title, GetValue());
        }
    }

    public class cNamedParameterEntryPointer : cNamedParameterEntryBase
    {
        private readonly cValueAddress valueAdress;
        private readonly RawValueType display;

        public cNamedParameterEntryPointer(cSupportCaseDataProvider dataProvider, cValueAddress valueAdress, RawValueType display = RawValueType.STRING) : base(dataProvider)
        {
            this.valueAdress = valueAdress;
            this.valueAdress = valueAdress;
            this.display = display;
        }

        public override string GetValue()
        {
            var output = string.Empty;

            try
            {
                cUtility.RawValueFormatter.SetDefaultCulture(new System.Globalization.CultureInfo(cFasdCockpitConfig.Instance.SelectedLanguage));
                cUtility.RawValueFormatter.SetDefaultTimeZone(TimeZoneInfo.Local);

                var outputTable = dataProvider.HealthCardDataHelper.HealthCardRawData.GetTableByName(valueAdress.ValueTable, true);
                if (outputTable != null)
                    if (outputTable.Columns.TryGetValue(valueAdress.ValueColumn, out var outpuColumn))
                        output = cUtility.RawValueFormatter.GetDisplayValue(outpuColumn.Values[valueAdress.DayIndex], display);
            }
            catch (Exception E)
            {
                LogException(E);
            }

            return output;
        }

        public override string GetHtmlValue()
        {
            return GetValue();
        }
    }

    public class cNamedParameterEntryCopyTemplate : cNamedParameterEntryBase
    {
        private readonly string copyTemplateName;

        public cNamedParameterEntryCopyTemplate(cSupportCaseDataProvider dataProvider, string copyTemplateName) : base(dataProvider)
        {
            this.copyTemplateName = copyTemplateName;
        }

        public override string GetValue()
        {
            var output = string.Empty;
            try
            {
                if (cF4SDCockpitXmlConfig.Instance?.CopyTemplateConfig?.CopyTemplates.CopyTemplates.TryGetValue(copyTemplateName, out var selectedCopyTemplate) ?? false)
                    if (selectedCopyTemplate.CopyContentList.TryGetValue(enumCopyContentFormat.UNICODE, out var selectedCopyContent))
                        output = dataProvider.HealthCardDataHelper.ReplaceNamedParameters(selectedCopyContent.Content, false);
            }
            catch (Exception E)
            {
                LogException(E);
            }
            return output;
        }

        public override string GetHtmlValue()
        {
            var output = string.Empty;
            try
            {
                if (cF4SDCockpitXmlConfig.Instance?.CopyTemplateConfig?.CopyTemplates.CopyTemplates.TryGetValue(copyTemplateName, out var selectedCopyTemplate) ?? false)
                {
                    if (selectedCopyTemplate.CopyContentList.TryGetValue(enumCopyContentFormat.HTML, out var selectedCopyContent))
                        output = dataProvider.HealthCardDataHelper.ReplaceNamedParameters(selectedCopyContent.Content, true);
                    else if (selectedCopyTemplate.CopyContentList.TryGetValue(enumCopyContentFormat.UNICODE, out var selectedCopyContentUnicode))
                        output = dataProvider.HealthCardDataHelper.ReplaceNamedParameters(selectedCopyContentUnicode.Content, false);
                }
            }
            catch (Exception E)
            {
                LogException(E);
            }
            return output;
        }
    }

    public class cNamedParameterEntryQuickActionResult : cNamedParameterEntryBase
    {
        public cNamedParameterEntryQuickActionResult(cSupportCaseDataProvider dataProvider) : base(dataProvider)
        {

        }

        public override string GetValue()
        {
            var output = string.Empty;
            try
            {
                var quickActionResult = F4SDProtocoll.Instance.GetLatestOfTypeAsDataObject<QuickActionProtocollEntry>(false);
                output = quickActionResult.GetText();
            }
            catch (Exception E)
            {
                LogException(E);
            }
            return output;
        }

        public override string GetHtmlValue()
        {
            var output = string.Empty;
            try
            {
                var quickActionResult = F4SDProtocoll.Instance.GetLatestOfTypeAsDataObject<QuickActionProtocollEntry>(true);
                output = quickActionResult.GetText(TextDataFormat.Html);
            }
            catch (Exception E)
            {
                LogException(E);
            }
            return output;
        }
    }

    public class cNamedParameterEntryQuickActionResultProtocol : cNamedParameterEntryBase
    {
        public cNamedParameterEntryQuickActionResultProtocol(cSupportCaseDataProvider dataProvider) : base(dataProvider)
        {

        }

        public override string GetValue()
        {
            var output = string.Empty;
            try
            {
                var quickActionResult = F4SDProtocoll.Instance.GetAllAsDataObject(false); // dataProvider.QuickActionProtocollHelper.GetFullProtocollQuickActionResult();
                output = quickActionResult.GetText();
            }
            catch (Exception E)
            {
                LogException(E);
            }
            return output;
        }

        public override string GetHtmlValue()
        {
            var output = string.Empty;
            try
            {
                var quickActionResult = F4SDProtocoll.Instance.GetAllAsDataObject(false); // dataProvider.QuickActionProtocollHelper.GetFullProtocollQuickActionResult();
                output = quickActionResult.GetText(TextDataFormat.Html);
            }
            catch (Exception E)
            {
                LogException(E);
            }
            return output;
        }
    }

    public class cNamedParameterList : Dictionary<string, cNamedParameterEntryBase>
    {
        public cNamedParameterList()
        {

        }

        public cNamedParameterList(cSupportCaseDataProvider dataProvider)
        {
            try
            {
                Add("F4SD_QuickActionProtocolLast", new cNamedParameterEntryQuickActionResult(dataProvider));
                Add("F4SD_QuickActionProtocol", new cNamedParameterEntryQuickActionResultProtocol(dataProvider));
            }
            catch (Exception E)
            {
                LogException(E);
            }
        }

        public Dictionary<string, FasdCockpitBase.cExternalToolExecutor.iNamedParameter> ToInterfaceDictionary()
        {
            var output = new Dictionary<string, FasdCockpitBase.cExternalToolExecutor.iNamedParameter>();
            foreach (var entry in this)
                output.Add(entry.Key, entry.Value);
            return output;
        }
    }
}
