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

// Author: Maxim Kuzmin

using Autofac;
using Autofac.Core;
using Makc2017.Core.App;
using Makc2017.Core.App.Plugins;
using Makc2017.Core.Configuration.Providers;
using System.Collections.Generic;

namespace Makc2017.Modules.UserRole
{
    /// <summary>
    /// Модули. Модуль "UserRole". Плагин.
    /// </summary>
    public class ModuleUserRolePlugin :
        ICoreAppPlugin,
        ICoreAppPluginWithInit,
        ICoreAppPluginWithResolver,
        ICoreAppPluginWithResolving
    {
        #region Properties

        /// <summary>
        /// Конфигурация.
        /// </summary>
        private ModuleUserRoleConfig Config { get; set; }

        /// <summary>
        /// Поставщик конфигурации.
        /// </summary>
        protected CoreConfigurationProviderJson ConfigurationProvider { get; private set; }

        /// <summary>
        /// Распознаватель зависимостей.
        /// </summary>
        public IModule Resolver { get; private set; }

        #endregion Properties

        #region Constructors

        /// <summary>
        /// Конструктор.
        /// </summary>
        public ModuleUserRolePlugin()
        {
            Resolver = CreateResolver();
        }

        #endregion Constructors

        #region Public methods

        /// <summary>
        /// Инициализировать.
        /// </summary>
        /// <param name="pluginEnvironment">Окружение плагинов.</param>
        public virtual void Init(CoreAppPluginEnvironment pluginEnvironment)
        {
            ConfigurationProvider.Init(pluginEnvironment.BasePath, CreateConfigFilePaths());

            Config.Init(ConfigurationProvider);
        }

        /// <summary>
        /// Распознать зависимости.
        /// </summary>
        /// <param name="componentContext">Контекст компонентов.</param>
        public virtual void Resolve(IComponentContext componentContext)
        {
            ConfigurationProvider = componentContext.Resolve<ModuleUserRoleConfigurationProvider>();

            Config = componentContext.Resolve<ModuleUserRoleConfig>();
        }

        #endregion Public methods

        #region Protected methods

        /// <summary>
        /// Создать распознаватель зависимостей.
        /// </summary>
        /// <returns>Распознаватель зависимостей.</returns>
        protected virtual IModule CreateResolver()
        {
            return new ModuleUserRoleResolver(); 
        }

        /// <summary>
        /// Создать пути к файлам конфигурации.
        /// </summary>
        /// <returns>Пути к файлам конфигурации.</returns>
        protected virtual IEnumerable<string> CreateConfigFilePaths()
        {
            return new[] { @"Configs\Module.UserRole.config.json" };
        }

        #endregion Protected methods
    }
}