using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web.Http.Dependencies;
using CncRepository.Base;
using CncRepository.Interface;
using CncService.Interface;
using CncWebApi.Controllers;

namespace CncWebApi.Infrastructure
{
    /// <summary>
    /// 简单的依赖注入解析器
    /// 手动注册 Repository 和 Service 的映射关系
    /// </summary>
    public class ServiceResolver : IDependencyResolver
    {
        private static readonly string _businessConn = ConfigurationManager.ConnectionStrings["BusinessConnection"]?.ConnectionString;
        private static readonly string _logConn = ConfigurationManager.ConnectionStrings["LogConnection"]?.ConnectionString;
        private static readonly string _jwtSecret = ConfigurationManager.AppSettings["JwtSecret"];

        /// <inheritdoc/>
        public IDependencyScope BeginScope()
        {
            return this;
        }

        /// <inheritdoc/>
        public object GetService(Type serviceType)
        {
            // Controller 解析
            if (serviceType == typeof(Controllers.AuthController))
                return new Controllers.AuthController(
                    ResolveAuthService());
            if (serviceType == typeof(Controllers.DashboardController))
                return new Controllers.DashboardController(
                    ResolveDashboardService(),
                    ResolveSysConfigRepository());
            if (serviceType == typeof(Controllers.MachineController))
                return new Controllers.MachineController(
                    ResolveMachineService());
            if (serviceType == typeof(Controllers.BrandController))
                return new Controllers.BrandController(
                    ResolveBrandService());
            if (serviceType == typeof(Controllers.CollectAddressController))
                return new Controllers.CollectAddressController(
                    ResolveCollectAddressService());
            if (serviceType == typeof(Controllers.WorkerController))
                return new Controllers.WorkerController(
                    ResolveWorkerService());
            if (serviceType == typeof(Controllers.ProductionController))
                return new Controllers.ProductionController(
                    ResolveProductionService());
            if (serviceType == typeof(Controllers.AlertController))
                return new Controllers.AlertController(
                    ResolveAlertService());
            if (serviceType == typeof(Controllers.SettingsController))
                return new Controllers.SettingsController(
                    new CncRepository.Impl.SysConfigRepository(_businessConn),
                    ResolveWorkshopService());
            if (serviceType == typeof(Controllers.LogController))
                return new Controllers.LogController(
                    ResolveSystemLogService(),
                    ResolveProductionAdjustmentRepository());
            if (serviceType == typeof(Controllers.ReplayController))
                return new Controllers.ReplayController(ResolveReplayService());
            if (serviceType == typeof(Controllers.ScreenConfigController))
                return new Controllers.ScreenConfigController(
                    ResolveScreenService());
            if (serviceType == typeof(Controllers.ScreenController))
                return new Controllers.ScreenController(
                    ResolveDashboardService(),
                    ResolveScreenService(),
                    new CncRepository.Impl.SysConfigRepository(_businessConn));
            if (serviceType == typeof(Controllers.OptionController))
                return new Controllers.OptionController(
                    ResolveWorkshopService(),
                    ResolveBrandService(),
                    ResolveMachineService(),
                    ResolveWorkerService(),
                    ResolveCollectAddressService());
            if (serviceType == typeof(Controllers.HealthController))
                return new Controllers.HealthController();
            if (serviceType == typeof(Controllers.CollectLogController))
                return new Controllers.CollectLogController(
                    ResolveCollectLogService(),
                    new CncRepository.Impl.Log.CollectRawRepository(_logConn));
            if (serviceType == typeof(Controllers.SimulatorController))
                return new Controllers.SimulatorController();

            return null;
        }

        /// <inheritdoc/>
        public IEnumerable<object> GetServices(Type serviceType)
        {
            return Enumerable.Empty<object>();
        }

        /// <inheritdoc/>
        public void Dispose() { }

        #region Service 解析

        private IAuthService ResolveAuthService()
        {
            return new CncService.Impl.AuthService(
                new CncRepository.Impl.SysConfigRepository(_businessConn),
                _jwtSecret);
        }

        private IDashboardService ResolveDashboardService()
        {
            // 注入 WindowsServiceChecker 以便在后端查询 Windows 服务状态
            var serviceChecker = new CncService.Impl.WindowsServiceChecker();
            return new CncService.Impl.DashboardService(
                new CncRepository.Impl.Dashboard.DashboardRepository(_businessConn),
                new CncRepository.Impl.Log.CollectorHeartbeatRepository(_logConn),
                new CncRepository.Impl.SysConfigRepository(_businessConn),
                serviceChecker);
        }

        private IMachineService ResolveMachineService()
        {
            return new CncService.Impl.MachineService(
                new CncRepository.Impl.MachineRepository(_businessConn),
                new CncRepository.Impl.CollectAddressRepository(_businessConn),
                new CncRepository.Impl.WorkerMachineRepository(_businessConn),
                new CncRepository.Impl.BrandRepository(_businessConn));
        }

        private IBrandService ResolveBrandService()
        {
            return new CncService.Impl.BrandService(
                new CncRepository.Impl.BrandRepository(_businessConn),
                new CncRepository.Impl.BrandFieldMappingRepository(_businessConn),
                new CncRepository.Impl.CollectAddressRepository(_businessConn));
        }

        private ICollectAddressService ResolveCollectAddressService()
        {
            return new CncService.Impl.CollectAddressService(
                new CncRepository.Impl.CollectAddressRepository(_businessConn),
                new CncRepository.Impl.MachineRepository(_businessConn),
                new CncRepository.Impl.BrandRepository(_businessConn),
                new CncRepository.Impl.WorkshopRepository(_businessConn),
                new CncRepository.Impl.Log.CollectRawRepository(_logConn),
                new CncRepository.Impl.SysConfigRepository(_businessConn));
        }

        private IWorkerService ResolveWorkerService()
        {
            return new CncService.Impl.WorkerService(
                new CncRepository.Impl.WorkerRepository(_businessConn),
                new CncRepository.Impl.WorkerMachineRepository(_businessConn),
                new CncRepository.Impl.MachineRepository(_businessConn),
                new CncRepository.Impl.SysConfigRepository(_businessConn));
        }

        private IProductionService ResolveProductionService()
        {
            return new CncService.Impl.ProductionService(
                new CncRepository.Impl.DailyProductionRepository(_businessConn),
                new CncRepository.Impl.ProductionSegmentRepository(_businessConn),
                new CncRepository.Impl.ProductionAdjustmentRepository(_businessConn));
        }

        private IAlertService ResolveAlertService()
        {
            return new CncService.Impl.AlertService(
                new CncRepository.Impl.AlertRepository(_businessConn));
        }

        private IWorkshopService ResolveWorkshopService()
        {
            return new CncService.Impl.WorkshopService(
                new CncRepository.Impl.WorkshopRepository(_businessConn));
        }

        private IScreenService ResolveScreenService()
        {
            return new CncService.Impl.ScreenService(
                new CncRepository.Impl.ScreenConfigRepository(_businessConn),
                new CncRepository.Impl.ScreenFilterRepository(_businessConn),
                new CncRepository.Impl.WorkshopRepository(_businessConn));
        }

        private ISystemLogService ResolveSystemLogService()
        {
            return new CncService.Impl.SystemLogService(
                new CncRepository.Impl.SystemLogRepository(_logConn));
        }

        private IProductionAdjustmentRepository ResolveProductionAdjustmentRepository()
        {
            return new CncRepository.Impl.ProductionAdjustmentRepository(_businessConn);
        }

        private ICollectLogService ResolveCollectLogService()
        {
            return new CncService.Impl.CollectLogService(
                new CncRepository.Impl.Log.CollectAnalysisRepository(_logConn),
                new CncRepository.Impl.Log.CollectCycleRepository(_logConn));
        }

        private ISysConfigRepository ResolveSysConfigRepository()
        {
            return new CncRepository.Impl.SysConfigRepository(_businessConn);
        }

        private IReplayService ResolveReplayService()
        {
            return new CncService.Impl.ReplayService(_businessConn, _logConn);
        }

        #endregion
    }
}
