using Microsoft.AspNetCore.Mvc.Rendering;
using Newtonsoft.Json;
using Serilog;
using System.ComponentModel.DataAnnotations;
using System.Net.Http;
using System.Reflection;
using WorkFlowCheck.Common.DTO;
using WorkFlowCheck.Common.Enums;
using WorkFlowCheck.Web.Services.Interfaces;

namespace WorkFlowCheck.Web.Services
{
    public class CheckListService : BaseService, ICheckListService
    {
        public CheckListService(HttpClient httpClient, IConfiguration configuration, IHttpContextAccessor httpContextAccessor) : base(httpClient, configuration, httpContextAccessor)
        {
        }

        public async Task<List<CheckListHeaderDTO>> GetAllCheckListHeaderAsync()
        {
            var endpoint = $"{_httpClient.BaseAddress}api/CheckList/GetAllCheckListHeaders";
            var retVal = new List<CheckListHeaderDTO>();
            try
            {
                var response = await _httpClient.GetFromJsonAsync<ApiResponseDTO<List<CheckListHeaderDTO>>>(endpoint);
                if (response != null)
                {
                    if (response.IsSuccess)
                    {
                        return response.Data;
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
            }
            return retVal;
        }
        public async Task<CheckListHeaderDTO> GetCheckListHeader(int id)
        {
            var endpoint = $"{_httpClient.BaseAddress}api/CheckList/GetCheckListHeader/{id}";
            var retVal = new CheckListHeaderDTO() { CheckListRowDTO = new List<CheckListRowDTO>() };
            try
            {
                var response = await _httpClient.GetFromJsonAsync<ApiResponseDTO<CheckListHeaderDTO>>(endpoint);
                if (response != null)
                {
                    if (response.IsSuccess)
                    {
                        return response.Data;
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
            }
            return retVal;
        }
        public async Task<ApiResponseDTO<CheckListHeaderDTO>> UpdateCheckListHeader(CheckListHeaderDTO checkListHeaderDTO) => throw new NotImplementedException();

        public async Task<ApiResponseDTO<byte[]>> CreatePDF(int id)
        {
            var retVal = new ApiResponseDTO<byte[]>();
            var endpoint = $"{_httpClient.BaseAddress}api/CheckList/CreatePDF/{id}";
            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<CheckListTemplateHeaderDTO>> GetAllCheckListTemplateHeaderAsync()
        {
            var endpoint = $"{_httpClient.BaseAddress}api/CheckList/GetAllCheckListTemplateHeaders";
            var retVal = new List<CheckListTemplateHeaderDTO>();
            try
            {
                var response = await _httpClient.GetFromJsonAsync<ApiResponseDTO<List<CheckListTemplateHeaderDTO>>>(endpoint);
                if (response != null)
                {
                    if (response.IsSuccess)
                    {
                        return response.Data;
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
            }
            return retVal;
        }
        public async Task<CheckListTemplateHeaderDTO> GetCheckListTemplateHeader(int id)
        {
            var endpoint = $"{_httpClient.BaseAddress}api/CheckList/GetCheckListTemplateHeader/{id}";
            var retVal = new CheckListTemplateHeaderDTO() { CheckListTemplateRowDTO = new List<CheckListTemplateRowDTO>() };
            try
            {
                var response = await _httpClient.GetFromJsonAsync<ApiResponseDTO<CheckListTemplateHeaderDTO>>(endpoint);
                if (response != null)
                {
                    if (response.IsSuccess)
                    {
                        return response.Data;
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
            }
            return retVal;
        }
        public async Task<ApiResponseDTO<CheckListTemplateHeaderDTO>> UpdateCheckListTemplateHeader(CheckListTemplateHeaderDTO checkListTemplateHeaderDTO)
        {
            try
            {
                string endpoint = $"{_httpClient.BaseAddress}api/CheckList/UpdateCheckListTemplateHeader";

                using (HttpResponseMessage httpResponseMessage = await _httpClient.PostAsJsonAsync(endpoint, checkListTemplateHeaderDTO))
                {
                    httpResponseMessage.EnsureSuccessStatusCode();

                    var jsonString = await httpResponseMessage.Content.ReadAsStringAsync();
                    var response = JsonConvert.DeserializeObject<ApiResponseDTO<CheckListTemplateHeaderDTO>>(jsonString);

                    return response ?? new ApiResponseDTO<CheckListTemplateHeaderDTO>
                    {
                        IsSuccess = false,
                    };
                }
            }
            catch (Exception ex)
            {
                // Hiba visszaadása
                return new ApiResponseDTO<CheckListTemplateHeaderDTO>
                {
                    IsSuccess = false
                };
            }
        }

        public async Task<CheckListTemplateRowDTO> GetCheckListTemplateRow(int id)
        {
            string endpoint = $"{_httpClient.BaseAddress}api/CheckList/GetCheckListTemplateRow/{id}";
            var retVal = new CheckListTemplateRowDTO();
            try
            {
                var response = await _httpClient.GetFromJsonAsync<ApiResponseDTO<CheckListTemplateRowDTO>>(endpoint);
                if (response != null)
                {
                    if (response.IsSuccess)
                    {
                        return response.Data;
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
            }
            return retVal;
        }
        public async Task<ApiResponseDTO<CheckListTemplateRowDTO>> UpdateCheckListTemplateRow(CheckListTemplateRowDTO checkListTemplateRowDTO)
        {
            try
            {
                string endpoint = $"{_httpClient.BaseAddress}api/CheckList/UpdateCheckListTemplateRow";

                using (HttpResponseMessage httpResponseMessage = await _httpClient.PostAsJsonAsync(endpoint, checkListTemplateRowDTO))
                {
                    httpResponseMessage.EnsureSuccessStatusCode();

                    var jsonString = await httpResponseMessage.Content.ReadAsStringAsync();
                    var response = JsonConvert.DeserializeObject<ApiResponseDTO<CheckListTemplateRowDTO>>(jsonString);

                    return response ?? new ApiResponseDTO<CheckListTemplateRowDTO>
                    {
                        IsSuccess = false,
                    };
                }
            }
            catch (Exception ex)
            {
                // Hiba visszaadása
                return new ApiResponseDTO<CheckListTemplateRowDTO>
                {
                    IsSuccess = false
                };
            }
        }

        public async Task<CheckListRowDTO> GetCheckListRow(int id)
        {
            string endpoint = $"{_httpClient.BaseAddress}api/CheckList/GetCheckListRow/{id}";
            var retVal = new CheckListRowDTO();
            try
            {
                var response = await _httpClient.GetFromJsonAsync<ApiResponseDTO<CheckListRowDTO>>(endpoint);
                if (response != null)
                {
                    if (response.IsSuccess)
                    {
                        return response.Data;
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
            }
            return retVal;
        }
        public async Task<ApiResponseDTO<CheckListRowDTO>> UpdateCheckListRow(CheckListRowDTO checkListRowDTO)
        {
            try
            {
                string endpoint = $"{_httpClient.BaseAddress}api/CheckList/UpdateCheckListRow";

                using (HttpResponseMessage httpResponseMessage = await _httpClient.PostAsJsonAsync(endpoint, checkListRowDTO))
                {
                    httpResponseMessage.EnsureSuccessStatusCode();

                    var jsonString = await httpResponseMessage.Content.ReadAsStringAsync();
                    var response = JsonConvert.DeserializeObject<ApiResponseDTO<CheckListRowDTO>>(jsonString);

                    return response ?? new ApiResponseDTO<CheckListRowDTO>
                    {
                        IsSuccess = false,
                    };
                }
            }
            catch (Exception ex)
            {
                // Hiba visszaadása
                return new ApiResponseDTO<CheckListRowDTO>
                {
                    IsSuccess = false
                };
            }
        }

        public List<SelectListItem> GetCheckStatus()
        {
            return Enum.GetValues(typeof(CheckStatus))
            .Cast<CheckStatus>()
            .Select(e => new SelectListItem
            {
                Value = ((int)e).ToString(),
                Text = GetEnumDisplayName(e)
            }).ToList();
        }
        private string GetEnumDisplayName(Enum enumValue)
        {
            return enumValue.GetType()
                .GetMember(enumValue.ToString())
                .First()
                .GetCustomAttribute<DisplayAttribute>()?.Name ?? enumValue.ToString();
        }
    }

}
