﻿// MIT License. Copyright (c) 2016 Maxim Kuzmin. Contacts: https://github.com/maxim-kuzmin

// Author: Maxim Kuzmin

using Makc2017.Core.Caching;
using Makc2017.Core.Caching.Clients;
using Makc2017.Core.Caching.Resources.Errors;
using Makc2017.Core.Execution.Operations.Async;
using Makc2017.Core.Resources.Errors;
using Makc2017.Data.Objects;
using Makc2017.Modules.UserRole.Filters;
using Makc2017.Modules.UserRole.Models;
using Makc2017.Modules.UserRole.Operations.Item;
using System;
using System.Threading.Tasks;

namespace Makc2017.Modules.UserRole.Caching.Operations.Item
{
    /// <summary>
    /// Модули. Модуль "UserRole". Кэширование. Операции. Операция, выполняющая функцию получения элемента.
    /// </summary>
    public class ModuleUserRoleCachingOperationItemGet : CoreExecutionOperationAsyncWithInputAndOutputData
        <
            ModuleUserRoleFilterItem,
            ModuleUserRoleModelItem
        >
    {
        #region Constructors

        /// <summary>
        /// Конструктор.
        /// </summary>
        /// <param name="operation">Операция, содержащая выполняемую функцию.</param>   
        /// <param name="config">Конфигурация.</param>        
        /// <param name="cache">Кэш.</param>  
        /// <param name="coreResourceErrors">Ресурсы ошибок ядра.</param>
        /// <param name="coreCachingResourceErrors">Ресурсы ошибок ядра кэширования.</param>
        public ModuleUserRoleCachingOperationItemGet(
            ModuleUserRoleOperationItemGet operation,
            ICoreCachingClientConfig config,
            ICoreCachingCache cache,
            CoreResourceErrors coreResourceErrors,
            CoreCachingResourceErrors coreCachingResourceErrors
            ) : base(
                CreateExecutableFunction(operation, config, cache, coreCachingResourceErrors),
                operation.FuncErrorMessagesGetting,
                operation.FuncInputDataTransform,
                operation.FuncSuccessMessagesGetting,
                operation.FuncWarningMessagesGetting,
                coreResourceErrors
                )
        {
        }

        #endregion Constructors

        #region Private methods

        /// <summary>
        /// Создать выполняемую функцию.
        /// </summary>
        /// <returns>Выполняемая функция.</returns>
        private static Func<ModuleUserRoleFilterItem, Task<ModuleUserRoleModelItem>> CreateExecutableFunction(
            ModuleUserRoleOperationItemGet operation,
            ICoreCachingClientConfig config,
            ICoreCachingCache cache,
            CoreCachingResourceErrors coreCachingResourceErrors
            )
        {
            if (config.IsCachingEnabled)
            {
                var client = new CoreCachingClientWithChangeAndRead<ModuleUserRoleModelItem>(
                    "Item.Get",
                    config,
                    cache,
                    coreCachingResourceErrors
                    );

                return inputData =>
                {
                    var keys = new object[]
                    {
                        inputData.Id
                    };

                    var tags = new[]
                    {
                        DataObjectUserRole.DB_TABLE_NAME_WITH_SCHEMA
                    };

                    return client.Read(() => operation.ExecutableFunction(inputData), keys, tags);
                };
            }
            else
            {
                return operation.ExecutableFunction;
            }
        }

        #endregion Private methods
    }
}
