using System.Net.Http;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Json;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using WorkFlowCheck.Common.DTO;
using System.Text.Json.Serialization;
using Newtonsoft.Json;
using WorkFlowCheck.MAUI.DataLayer;
using AutoMapper;
using WorkFlowCheck.MAUI.Services.Interfaces;

namespace WorkFlowCheck.MAUI.Services
{
    public class UserService : BaseService, IUserService
    {
        public UserDTO? CurrentUser { get; private set; }
        public UserService(HttpClient httpClient, IConfiguration configuration, AppDbContext dbContext, IMapper mapper) : base(httpClient, configuration, dbContext, mapper)
        {
        }
        public async Task<ApiResponseDTO<UserDTO>> GetUserAsync()
        {
            try
            {
                await PrepareAuthenticatedRequestAsync();
                // Az API végpont meghatározása
                string endpoint = $"{_httpClient.BaseAddress}api/User/1";

                // HTTP GET kérés küldése
                var response = await _httpClient.GetFromJsonAsync<ApiResponseDTO<UserDTO>>(endpoint);

                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,
                     
                };

                // HTTP POST kérés küldése

                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 void SetCurrentUser(UserDTO user)
        {
            CurrentUser = user;
        }

        public UserDTO? GetCurrentUser() => CurrentUser;
    }
}
