using Microsoft.AspNetCore.Mvc.Rendering;
using Newtonsoft.Json;
using Serilog;
using WorkFlowCheck.Common.DTO;
using WorkFlowCheck.Web.Services.Interfaces;

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

        public async Task<List<CheckPointDTO>> GetAllCheckPoints()
        {
            string endpoint = $"{_httpClient.BaseAddress}api/Sync/GetAllCheckPoints";
            var retVal = new List<CheckPointDTO>();
            try
            {
                var response = await _httpClient.GetFromJsonAsync<ApiResponseDTO<List<CheckPointDTO>>>(endpoint);
                if (response != null)
                {
                    if (response.IsSuccess)
                    {
                        return response.Data;
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
            }
            return retVal;

        }
        public async Task<List<EquipmentDTO>> GetAllEquipments()
        {
            string endpoint = $"{_httpClient.BaseAddress}api/Sync/GetAllEquipments";
            var retVal = new List<EquipmentDTO>();
            try
            {
                var response = await _httpClient.GetFromJsonAsync<ApiResponseDTO<List<EquipmentDTO>>>(endpoint);
                if (response != null)
                {
                    if (response.IsSuccess)
                    {
                        return response.Data;
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
            }
            return retVal;

        }
        public async Task<List<LocationDTO>> GetAllLocations()
        {
            string endpoint = $"{_httpClient.BaseAddress}api/Sync/GetAllLocations";
            var retVal = new List<LocationDTO>();
            try
            {
                var response = await _httpClient.GetFromJsonAsync<ApiResponseDTO<List<LocationDTO>>>(endpoint);
                if (response != null)
                {
                    if (response.IsSuccess)
                    {
                        return response.Data;
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
            }
            return retVal;

        }

        public async Task<CheckPointDTO> GetCheckPoint(int id)
        {
            string endpoint = $"{_httpClient.BaseAddress}api/Sync/GetCheckPoint/{id}";
            var retVal = new CheckPointDTO();
            try
            {
                var response = await _httpClient.GetFromJsonAsync<ApiResponseDTO<CheckPointDTO>>(endpoint);
                if (response != null)
                {
                    if (response.IsSuccess)
                    {
                        response.Data.CheckListTemplateRowDTO = response.Data.CheckListTemplateRowDTO?.OrderBy(o => o.RowIndex).ToList();
                        return response.Data;
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
            }
            return retVal;
        }
        public async Task<EquipmentDTO> GetEquipment(int id)
        {
            string endpoint = $"{_httpClient.BaseAddress}api/Sync/GetEquipment/{id}";
            var retVal = new EquipmentDTO();
            try
            {
                var response = await _httpClient.GetFromJsonAsync<ApiResponseDTO<EquipmentDTO>>(endpoint);
                if (response != null)
                {
                    if (response.IsSuccess)
                    {
                        return response.Data;
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
            }
            return retVal;
        }
        public async Task<LocationDTO> GetLocation(int id)
        {
            string endpoint = $"{_httpClient.BaseAddress}api/Sync/GetLocation/{id}";
            var retVal = new LocationDTO();
            try
            {
                var response = await _httpClient.GetFromJsonAsync<ApiResponseDTO<LocationDTO>>(endpoint);
                if (response != null)
                {
                    if (response.IsSuccess)
                    {
                        return response.Data;
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
            }
            return retVal;
        }

        public async Task<bool> DeleteLocation(int id)
        {
            string endpoint = $"{_httpClient.BaseAddress}api/Sync/DeleteLocation/{id}";
            var retVal = false;
            try
            {
                var response = await _httpClient.GetFromJsonAsync<ApiResponseDTO<bool>>(endpoint);
                if (response != null)
                {
                    if (response.IsSuccess)
                    {
                        return response.Data;
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
            }
            return retVal;
        }
        public async Task<bool> DeleteEquipment(int id)
        {
            string endpoint = $"{_httpClient.BaseAddress}api/Sync/DeleteEquipment/{id}";
            var retVal = false;
            try
            {
                var response = await _httpClient.GetFromJsonAsync<ApiResponseDTO<bool>>(endpoint);
                if (response != null)
                {
                    if (response.IsSuccess)
                    {
                        return response.Data;
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
            }
            return retVal;
        }
        public async Task<bool> DeleteCheckPoint(int id)
        {
            string endpoint = $"{_httpClient.BaseAddress}api/Sync/DeleteCheckPoint/{id}";
            var retVal = false;
            try
            {
                var response = await _httpClient.GetFromJsonAsync<ApiResponseDTO<bool>>(endpoint);
                if (response != null)
                {
                    if (response.IsSuccess)
                    {
                        return response.Data;
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
            }
            return retVal;
        }

        public async Task<ApiResponseDTO<CheckPointDTO>> UpdateCheckPoint(CheckPointDTO checkPointDTO)
        {
            try
            {
                string endpoint = $"{_httpClient.BaseAddress}api/sync/updatecheckpoint";

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

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

                    return response ?? new ApiResponseDTO<CheckPointDTO>
                    {
                        IsSuccess = false,
                    };
                }
            }
            catch (Exception ex)
            {
                // Hiba visszaadása
                return new ApiResponseDTO<CheckPointDTO>
                {
                    IsSuccess = false
                };
            }
        }
        public async Task<ApiResponseDTO<EquipmentDTO>> UpdateEquipment(EquipmentDTO EquipmentDTO)
        {
            try
            {
                string endpoint = $"{_httpClient.BaseAddress}api/sync/updateEquipment";

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

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

                    return response ?? new ApiResponseDTO<EquipmentDTO>
                    {
                        IsSuccess = false,
                    };
                }
            }
            catch (Exception ex)
            {
                // Hiba visszaadása
                return new ApiResponseDTO<EquipmentDTO>
                {
                    IsSuccess = false
                };
            }
        }
        public async Task<ApiResponseDTO<LocationDTO>> UpdateLocation(LocationDTO LocationDTO)
        {
            try
            {
                string endpoint = $"{_httpClient.BaseAddress}api/sync/updateLocation";

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

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

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

        public async Task<List<NumberGeneratorTemplateDTO>> GetNumbergenerators()
        {
            string endpoint = $"{_httpClient.BaseAddress}api/Sync/GetAllNumbergenerators";
            var retVal = new List<NumberGeneratorTemplateDTO>();
            try
            {
                var response = await _httpClient.GetFromJsonAsync<ApiResponseDTO<List<NumberGeneratorTemplateDTO>>>(endpoint);
                if (response != null)
                {
                    if (response.IsSuccess)
                    {
                        return response.Data;
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
            }
            return retVal;
        }

    }
}
