using AipGateway.AIP;
using AipGateway.API.Application.Modules;
using AipGateway.API.Domain.Models;

namespace AipGateway.API.Services.impl
{
    public class ApiAipService : IApiAipService
    {
        private readonly ILogger<ApiFileService> _log;
        private readonly IServiceProvider _provider;

        public ApiAipService(ILogger<ApiFileService> logger, IServiceProvider provider)
        {
            _log = logger;
            _provider = provider;
            _log.LogError("ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo: ApiAipService");
        }

        public async Task<ApiResponseDto> DownloadAipInfo()
        {
            try
            {
                //AipFileApiService? aipFileApiService = ContainerService.provider.GetService<AipFileApiService>();
                AipFileApiService? aipFileApiService = _provider.GetService<AipFileApiService>();                
                if (aipFileApiService == null)
                {
                    return new ApiResponseDto
                    {
                        errorCode = 1,
                        errorMessage = "AIP File 관리 서비스를 찾을 수 없습니다.",
                        effectCount = 0,
                    };
                }

                var task = Task.Run(() =>
                {
                    aipFileApiService.DownloadAipFileInformations();
                    return ContainerService.aipLableMap.Count + ContainerService.aipPolicyMap.Count + ContainerService.aipProtectionMap.Count;
                });
                int result = await task;
                return new ApiResponseDto
                {
                    errorCode = 0,
                    errorMessage = GlobalConstants.API_RESULT_SUCCESS,
                    effectCount = result,
                };
            }
            catch (Exception)
            {
                throw;
            }

        }

        public async Task<List<AipLabel>> GetLabels()
        {
            try
            {
                var task = Task.Run(() =>
                {
                    return ContainerService.aipFileManager.SensitivityLabels();
                });
                List<AipLabel> result = await task;
                return result;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public async Task<List<AipLabel>> GetPolicies()
        {
            try
            {
                var task = Task.Run(() =>
                {
                    return ContainerService.aipFileManager.ListSensitivityLabels();
                });
                List<AipLabel> result = await task;
                return result;
            }
            catch (Exception)
            {
                throw;
            }
        }
        public async Task<List<AipTemplate>> GetProtections()
        {
            try
            {
                var task = Task.Run(() =>
                {
                    return ContainerService.aipFileManager.GetTemplates();
                });
                List<AipTemplate> result = await task;
                return result;
            }
            catch (Exception)
            {
                throw;
            }
        }

    }
}
