using Abp.AutoMapper;
using Abp.Dependency;
using Abp.Modules;
using Abp.Reflection.Extensions;
using Abp.Timing;
using Abp.Zero;
using Abp.Zero.Configuration;
using MineTec.ProManager.Authorization;
using MineTec.ProManager.Authorization.Roles;
using MineTec.ProManager.Authorization.Users;
using MineTec.ProManager.Social.Chat;
using MineTec.ProManager.Configuration;
using MineTec.ProManager.Features;
using MineTec.ProManager.Social.Friendships;
using MineTec.ProManager.Localization;
using MineTec.ProManager.MultiTenancy;
using MineTec.ProManager.Notifications;
using MineTec.ProManager.Timing;

namespace MineTec.ProManager
{
    [DependsOn(
        typeof(AbpZeroCoreModule),
        typeof(AbpAutoMapperModule)
        )]
    public class ProManagerCoreModule : AbpModule
    {
        public override void PreInitialize()
        {
            Configuration.Auditing.IsEnabledForAnonymousUsers = true;

            // Declare entity types
            Configuration.Modules.Zero().EntityTypes.Tenant = typeof(Tenant);
            Configuration.Modules.Zero().EntityTypes.Role = typeof(Role);
            Configuration.Modules.Zero().EntityTypes.User = typeof(User);

            ProManagerLocalizationConfigurer.Configure(Configuration.Localization);

            // Enable this line to create a multi-tenant application.
            Configuration.MultiTenancy.IsEnabled = ProManagerCoreConsts.MultiTenancyEnabled;

            // Configure roles
            AppRoleConfig.Configure(Configuration.Modules.Zero().RoleManagement);

            // Add setting providers
            Configuration.Settings.Providers.Add<AppSettingProvider>();

            // Add feature providers
            Configuration.Features.Providers.Add<AppFeatureProvider>();

            // Add notification providers
            Configuration.Notifications.Providers.Add<AppNotificationProvider>();

            // Add permission providers
            Configuration.Authorization.Providers.Add<AppPermissionProvider>();
        }

        public override void Initialize()
        {
            IocManager.RegisterAssemblyByConvention(typeof(ProManagerCoreModule).GetAssembly());
        }

        public override void PostInitialize()
        {
            IocManager.RegisterIfNot<IChatCommunicator, NullChatCommunicator>();
            IocManager.Resolve<ChatUserStateWatcher>().Initialize();
            IocManager.Resolve<AppTimes>().StartupTime = Clock.Now;
        }

        public override void Shutdown()
        {
            base.Shutdown();
        }
    }
}
