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

// Author: Maxim Kuzmin

using Autofac;
using Makc2017.Core.Authorize;
using Makc2017.Core.Data;
using Makc2017.Core.Resources.Errors;
using Makc2017.Core.Security;
using Makc2017.Core.Session;
using Makc2017.Data;
using Makc2017.Host.Security;
using Makc2017.Modules.User.Operations.CurrentUser;
using Makc2017.Modules.User.Operations.Item;
using Makc2017.Modules.User.Operations.List;
using Makc2017.Modules.User.Resources.Errors;
using Makc2017.Modules.User.Resources.Successes;
using Makc2017.Modules.User.Services.External;
using Makc2017.Modules.User.Services.Internal;
using Microsoft.Extensions.Localization;

namespace Makc2017.Modules.User
{
    /// <summary>
    /// Модули. Модуль "User". Распознаватель зависимостей модуля.
    /// </summary>
    public class ModuleUserResolver : Module
    {
        #region Protected methods

        /// <summary>
        /// Загрузить.
        /// </summary>
        /// <param name="builder">Построитель контейнера зависимостей.</param>
        protected override void Load(ContainerBuilder builder)
        {
            // Configuration provider

            builder.Register(
                x => new ModuleUserConfigurationProvider()
                ).AsSelf().SingleInstance();

            // Config

            builder.Register(
                x => new ModuleUserConfig()
                ).AsSelf().SingleInstance();

            // Resources

            builder.Register(
                x => new ModuleUserResourceData(
                    x.Resolve<IStringLocalizer<ModuleUserResourceData>>()
                    )
                ).AsSelf().SingleInstance();

            builder.Register(
                x => new ModuleUserResourceErrors(
                    x.Resolve<IStringLocalizer<ModuleUserResourceErrors>>()
                    )
                ).AsSelf().SingleInstance();

            builder.Register(
                x => new ModuleUserResourceSuccesses(
                    x.Resolve<IStringLocalizer<ModuleUserResourceSuccesses>>()
                    )
                ).AsSelf().SingleInstance();

            // Repository

            builder.Register(
                x => new ModuleUserRepository(
                    x.Resolve<ModuleUserConfig>(), 
                    x.Resolve<ICoreDataProvider>(),
                    x.Resolve<DataConfig>(),
                    x.Resolve<ModuleUserResourceData>()
                    )
                ).AsSelf().SingleInstance();

            // Operations

            builder.Register(
                x => new ModuleUserOperationItemDelete(
                    x.Resolve<ModuleUserRepository>().DeleteItem,
                    x.Resolve<CoreResourceErrors>(),
                    x.Resolve<ModuleUserResourceSuccesses>()
                    )
                ).AsSelf().SingleInstance();

            builder.Register(
                x => new ModuleUserOperationItemGet(
                    x.Resolve<ModuleUserRepository>().GetItem,
                    x.Resolve<CoreResourceErrors>()
                    )
                ).AsSelf().SingleInstance();

            builder.Register(
                x => new ModuleUserOperationItemInsert(
                    x.Resolve<ModuleUserRepository>().SaveItem,
                    x.Resolve<CoreResourceErrors>(),
                    x.Resolve<ModuleUserResourceSuccesses>(),
                    x.Resolve<ModuleUserResourceErrors>()
                    )
                ).AsSelf().SingleInstance();

            builder.Register(
                x => new ModuleUserOperationItemUpdate(
                    x.Resolve<ModuleUserRepository>().SaveItem,
                    x.Resolve<CoreResourceErrors>(),
                    x.Resolve<ModuleUserResourceSuccesses>(),
                    x.Resolve<ModuleUserResourceErrors>()
                    )
                ).AsSelf().SingleInstance();

            builder.Register(
                x => new ModuleUserOperationListGet(
                    x.Resolve<ModuleUserRepository>().GetList,
                    x.Resolve<CoreResourceErrors>()
                    )
                ).AsSelf().SingleInstance();

            // Internal services

            builder.Register(
                x => new ModuleUserServiceInternalCurrentUser(
                    x.Resolve<ModuleUserOperationItemGet>(),
                    x.Resolve<ICoreSessionService>(),
                    x.Resolve<ICoreAuthorizeService>()
                    )
                ).AsSelf().SingleInstance();

            // Internal service operations

            builder.Register(
                x => new ModuleUserOperationCurrentUserClear(
                    x.Resolve<ModuleUserServiceInternalCurrentUser>().ClearCurrentUser,
                    x.Resolve<CoreResourceErrors>()
                    )
                ).AsSelf().SingleInstance();

            builder.Register(
                x => new ModuleUserOperationCurrentUserGet(
                    x.Resolve<ModuleUserServiceInternalCurrentUser>().GetCurrentUser,
                    x.Resolve<CoreResourceErrors>()
                    )
                ).AsSelf().SingleInstance();

            builder.Register(
                x => new ModuleUserOperationCurrentUserSet(
                    x.Resolve<ModuleUserServiceInternalCurrentUser>().SetCurrentUser,
                    x.Resolve<CoreResourceErrors>()
                    )
                ).AsSelf().SingleInstance();

            // External services

            builder.Register(
                x => new ModuleUserServiceExternalSecurity(
                    x.Resolve<ModuleUserOperationItemGet>(),
                    x.Resolve<ModuleUserOperationCurrentUserGet>()
                    )
                ).As<IHostSecurityService>().SingleInstance();
        }

        #endregion Protected methods
    }
}
