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

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


        public async Task<UserDTO> GetUser(int id)
        {
            string endpoint = $"{_httpClient.BaseAddress}api/User/GetUser/{id}";
            var retVal = new UserDTO() { RoleDTO = new List<RoleDTO>() };
            try
            {
                var response = await _httpClient.GetFromJsonAsync<ApiResponseDTO<UserDTO>>(endpoint);
                if (response != null)
                {
                    if (response.IsSuccess)
                    {
                        return response.Data;
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
            }
            return retVal;
        }
        public async Task<List<UserDTO>> GetAllUsers()
        {
            string endpoint = $"{_httpClient.BaseAddress}api/User/GetAllUsers";
            var retVal = new List<UserDTO>();
            try
            {
                var response = await _httpClient.GetFromJsonAsync<ApiResponseDTO<List<UserDTO>>>(endpoint);
                if (response != null)
                {
                    if (response.IsSuccess)
                    {
                        return response.Data;
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
            }
            return retVal;
        }
        public async Task<ApiResponseDTO<UserDTO>> UpdateUser(UserDTO userDTO) 
        {
            try
            {
                string endpoint = $"{_httpClient.BaseAddress}api/user/updateUser";

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

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

                    return response ?? new ApiResponseDTO<UserDTO>
                    {
                        IsSuccess = false,
                    };
                }
            }
            catch (Exception ex)
            {
                // Hiba visszaadása
                return new ApiResponseDTO<UserDTO>
                {
                    IsSuccess = false
                };
            }
        }
        public async Task<ApiResponseDTO<UserDTO>> Authenticate(string username, string password)
        {
            try
            {
                // Az API végpont meghatározása
                string endpoint = $"{_httpClient.BaseAddress}api/User/Authenticate";

                var userSimpleDTO = new UserSimpleDTO()
                {
                    UserName = username,
                    Password = password,
                    
                };
                

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

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

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


        public async Task<RoleDTO> GetRole(int id)
        {
            string endpoint = $"{_httpClient.BaseAddress}api/User/GetRole/{id}";
            var retVal = new RoleDTO();
            try
            {
                var response = await _httpClient.GetFromJsonAsync<ApiResponseDTO<RoleDTO>>(endpoint);
                if (response != null)
                {
                    if (response.IsSuccess)
                    {
                        return response.Data;
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
            }
            return retVal;
        }
        public async Task<List<RoleDTO>> GetAllRoles()
        {
            string endpoint = $"{_httpClient.BaseAddress}api/User/GetAllRoles";
            var retVal = new List<RoleDTO>();
            try
            {
                var response = await _httpClient.GetFromJsonAsync<ApiResponseDTO<List<RoleDTO>>>(endpoint);
                if (response != null)
                {
                    if (response.IsSuccess)
                    {
                        return response.Data;
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
            }
            return retVal;
        }
        public async Task<ApiResponseDTO<RoleDTO>> UpdateRole(RoleDTO roleDTO)
        {
            try
            {
                string endpoint = $"{_httpClient.BaseAddress}api/user/updateRole";

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

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

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


        public async Task<UserRoleDTO> GetUserRole(int id)
        {
            string endpoint = $"{_httpClient.BaseAddress}api/user/GetUserRole/{id}";
            var retVal = new UserRoleDTO();
            try
            {
                var response = await _httpClient.GetFromJsonAsync<ApiResponseDTO<UserRoleDTO>>(endpoint);
                if (response != null)
                {
                    if (response.IsSuccess)
                    {
                        return response.Data;
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
            }
            return retVal;
        }
        public async Task<List<UserRoleDTO>> GetAllUserRoles()
        {
            string endpoint = $"{_httpClient.BaseAddress}api/User/GetAllUserRoles";
            var retVal = new List<UserRoleDTO>();
            try
            {
                var response = await _httpClient.GetFromJsonAsync<ApiResponseDTO<List<UserRoleDTO>>>(endpoint);
                if (response != null)
                {
                    if (response.IsSuccess)
                    {
                        return response.Data;
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
            }
            return retVal;
        }
        public async Task<ApiResponseDTO<UserRoleDTO>> UpdateUserRole(UserRoleDTO userRoleDTO)
        {
            try
            {
                string endpoint = $"{_httpClient.BaseAddress}api/user/updateUserRole";

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

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

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

        public async Task<RoleCheckListTemplateHeaderDTO> GetRoleCheckListTemplateHeader(int id)
        {
            string endpoint = $"{_httpClient.BaseAddress}api/user/GetRoleCheckListTemplateHeader/{id}";
            var retVal = new RoleCheckListTemplateHeaderDTO();
            try
            {
                var response = await _httpClient.GetFromJsonAsync<ApiResponseDTO<RoleCheckListTemplateHeaderDTO>>(endpoint);
                if (response != null)
                {
                    if (response.IsSuccess)
                    {
                        return response.Data;
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
            }
            return retVal;
        }
        public async Task<List<RoleCheckListTemplateHeaderDTO>> GetAllRoleCheckListTemplates() 
        {
            string endpoint = $"{_httpClient.BaseAddress}api/User/GetAllRoleCheckListTemplateHeaders";
            var retVal = new List<RoleCheckListTemplateHeaderDTO>();
            try
            {
                var response = await _httpClient.GetFromJsonAsync<ApiResponseDTO<List<RoleCheckListTemplateHeaderDTO>>>(endpoint);
                if (response != null)
                {
                    if (response.IsSuccess)
                    {
                        return response.Data;
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
            }
            return retVal;
        }
        public async Task<ApiResponseDTO<RoleCheckListTemplateHeaderDTO>> UpdateRoleCheckListTemplateHeader(RoleCheckListTemplateHeaderDTO roleCheckListTemplateHeaderDTO)
        {
            try
            {
                string endpoint = $"{_httpClient.BaseAddress}api/user/UpdateRoleCheckListTemplateHeader";

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

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

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

        public async Task<RoleCheckPointDTO> GetRoleCheckPoint(int id)
        {
            string endpoint = $"{_httpClient.BaseAddress}api/user/GetRoleCheckPoint/{id}";
            var retVal = new RoleCheckPointDTO();
            try
            {
                var response = await _httpClient.GetFromJsonAsync<ApiResponseDTO<RoleCheckPointDTO>>(endpoint);
                if (response != null)
                {
                    if (response.IsSuccess)
                    {
                        return response.Data;
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
            }
            return retVal;
        }
        public async Task<List<RoleCheckPointDTO>> GetAllRoleCheckPoints()
        {
            string endpoint = $"{_httpClient.BaseAddress}api/User/GetAllRoleCheckPoints";
            var retVal = new List<RoleCheckPointDTO>();
            try
            {
                var response = await _httpClient.GetFromJsonAsync<ApiResponseDTO<List<RoleCheckPointDTO>>>(endpoint);
                if (response != null)
                {
                    if (response.IsSuccess)
                    {
                        return response.Data;
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
            }
            return retVal;
        }
        public async Task<ApiResponseDTO<RoleCheckPointDTO>> UpdateRoleCheckPoint(RoleCheckPointDTO roleCheckPointDTO)
        {
            try
            {
                string endpoint = $"{_httpClient.BaseAddress}api/user/UpdateRoleCheckPoint";

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

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

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