using Serilog;
using WorkFlowCheck.Common.DTO;
using WorkFlowCheck.Web.Services.Interfaces;

namespace WorkFlowCheck.Web.Services
{
    public class SystemService:BaseService, ISystemService
    {
        public SystemService(HttpClient httpClient, IConfiguration configuration, IHttpContextAccessor httpContextAccessor) : base(httpClient, configuration, httpContextAccessor)
        {
        }
        public async Task<List<ImageFileDTO>> GetAllImageFilesAsync()
        {
            var endpoint = $"{_httpClient.BaseAddress}api/System/GetAllImageFiles";
            var retVal = new List<ImageFileDTO>();
            try
            {
                var response = await _httpClient.GetFromJsonAsync<ApiResponseDTO<List<ImageFileDTO>>>(endpoint);
                if (response != null)
                {
                    if (response.IsSuccess)
                    {
                        return response.Data;
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
            }
            return retVal;
        }
        public async Task<ApiResponseDTO<byte[]>> DownloadFile(string filename)
        {
            var retVal = new ApiResponseDTO<byte[]>();
            var endpoint = $"{_httpClient.BaseAddress}api/System/DownloadFile/{filename}";
            try
            {
                var response = await _httpClient.GetFromJsonAsync<ApiResponseDTO<byte[]>>(endpoint);
                if (response != null)
                {
                    if (response.IsSuccess)
                    {
                        return response;
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
            }
            return retVal;
        }
        public async Task<List<PDFFileDTO>> GetAllPDFFilesAsync()
        {
            var endpoint = $"{_httpClient.BaseAddress}api/System/GetAllPDFFiles";
            var retVal = new List<PDFFileDTO>();
            try
            {
                var response = await _httpClient.GetFromJsonAsync<ApiResponseDTO<List<PDFFileDTO>>>(endpoint);
                if (response != null)
                {
                    if (response.IsSuccess)
                    {
                        return response.Data;
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
            }
            return retVal;
        }
        public async Task<ApiResponseDTO<byte[]>> DownloadPDFFile(string filename)
        {
            var retVal = new ApiResponseDTO<byte[]>();
            var endpoint = $"{_httpClient.BaseAddress}api/System/DownloadPDFFile/{filename}";
            try
            {
                var response = await _httpClient.GetFromJsonAsync<ApiResponseDTO<byte[]>>(endpoint);
                if (response != null)
                {
                    if (response.IsSuccess)
                    {
                        return response;
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
            }
            return retVal;
        }

        public async Task<List<LogFileDTO>> GetAllLogAuditFilesAsync()
        {
            var endpoint = $"{_httpClient.BaseAddress}api/System/GetAllLogAuditFiles";
            var retVal = new List<LogFileDTO>();
            try
            {
                var response = await _httpClient.GetFromJsonAsync<ApiResponseDTO<List<LogFileDTO>>>(endpoint);
                if (response != null)
                {
                    if (response.IsSuccess)
                    {
                        return response.Data;
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
            }
            return retVal;
        }
        public async Task<ApiResponseDTO<byte[]>> DownloadAuditLogFile(string filename)
        {
            var retVal = new ApiResponseDTO<byte[]>();
            var endpoint = $"{_httpClient.BaseAddress}api/System/DownloadAuditLogFile/{filename}";
            try
            {
                var response = await _httpClient.GetFromJsonAsync<ApiResponseDTO<byte[]>>(endpoint);
                if (response != null)
                {
                    if (response.IsSuccess)
                    {
                        return response;
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
            }
            return retVal;
        }

        public async Task<List<LogFileDTO>> GetAllLogBusinessFilesAsync()
        {
            var endpoint = $"{_httpClient.BaseAddress}api/System/GetAllLogBusinessFiles";
            var retVal = new List<LogFileDTO>();
            try
            {
                var response = await _httpClient.GetFromJsonAsync<ApiResponseDTO<List<LogFileDTO>>>(endpoint);
                if (response != null)
                {
                    if (response.IsSuccess)
                    {
                        return response.Data;
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
            }
            return retVal;
        }
        public async Task<ApiResponseDTO<byte[]>> DownloadBusinessLogFile(string filename)
        {
            var retVal = new ApiResponseDTO<byte[]>();
            var endpoint = $"{_httpClient.BaseAddress}api/System/DownloadBusinessLogFile/{filename}";
            try
            {
                var response = await _httpClient.GetFromJsonAsync<ApiResponseDTO<byte[]>>(endpoint);
                if (response != null)
                {
                    if (response.IsSuccess)
                    {
                        return response;
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
            }
            return retVal;
        }
    }
}
