using AutoMapper;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using System.Net.Http.Json;
using WorkFlowCheck.Common.DTO;
using WorkFlowCheck.MAUI.DataLayer;
using WorkFlowCheck.MAUI.DataLayer.Entities;
using WorkFlowCheck.MAUI.Services.Interfaces;
using Location = WorkFlowCheck.MAUI.DataLayer.Entities.Location;

namespace WorkFlowCheck.MAUI.Services
{
    public class SyncService : BaseService, ISyncService
    {
        public SyncService(HttpClient httpClient, IConfiguration configuration, AppDbContext dbContext, IMapper mapper) : base(httpClient, configuration, dbContext, mapper)
        {

        }
        public async Task SyncDatas()
        {
            await SyncCheckPoints();
            await SyncLocations();
            await SyncEquipments();
            await SyncCheckListTemplates();
        }
        public async Task SyncCheckPoints()
        {
            try
            {
                await PrepareAuthenticatedRequestAsync();
                // Az API végpont meghatározása
                string endpoint = $"{_httpClient.BaseAddress}api/Sync/GetAllCheckPoints";

                // HTTP GET kérés küldése
                var response = await _httpClient.GetFromJsonAsync<ApiResponseDTO<List<CheckPointDTO>>>(endpoint);
                if (response != null)
                {
                    if (response.IsSuccess)
                    {
                        var entityList = await _dbContext.CheckPoints.ToListAsync();
                        var missingItems = response.Data.Where(f => !entityList.Any(s => s.Id == f.Id)).ToList();

                        var mappedMissingItems = _mapper.Map<List<CheckPoint>>(missingItems);

                        _dbContext.CheckPoints.AddRange(mappedMissingItems);
                        await _dbContext.SaveChangesAsync();
                    }
                }
            }
            catch (Exception ex)
            {
                // Hiba visszaadása

            }
        }
        public async Task SyncLocations()
        {
            try
            {
                await PrepareAuthenticatedRequestAsync();
                // Az API végpont meghatározása
                string endpoint = $"{_httpClient.BaseAddress}api/Sync/GetAllLocations";

                // HTTP GET kérés küldése
                var response = await _httpClient.GetFromJsonAsync<ApiResponseDTO<List<LocationDTO>>>(endpoint);
                if (response != null)
                {
                    if (response.IsSuccess)
                    {
                        var entityList = await _dbContext.Locations.ToListAsync();
                        var missingItems = response.Data.Where(f => !entityList.Any(s => s.Id == f.Id)).ToList();

                        var mappedMissingItems = _mapper.Map<List<Location>>(missingItems);

                        _dbContext.Locations.AddRange(mappedMissingItems);
                        await _dbContext.SaveChangesAsync();
                    }
                }
            }
            catch (Exception ex)
            {
                // Hiba visszaadása

            }
        }
        public async Task SyncEquipments()
        {
            try
            {
                await PrepareAuthenticatedRequestAsync();
                // Az API végpont meghatározása
                string endpoint = $"{_httpClient.BaseAddress}api/Sync/GetAllEquipments";

                // HTTP GET kérés küldése
                var response = await _httpClient.GetFromJsonAsync<ApiResponseDTO<List<EquipmentDTO>>>(endpoint);
                if (response != null)
                {
                    if (response.IsSuccess)
                    {
                        var entityList = await _dbContext.Locations.ToListAsync();
                        var missingItems = response.Data.Where(f => !entityList.Any(s => s.Id == f.Id)).ToList();

                        var mappedMissingItems = _mapper.Map<List<Equipment>>(missingItems);

                        _dbContext.Equipments.AddRange(mappedMissingItems);
                        await _dbContext.SaveChangesAsync();
                    }
                }
            }
            catch (Exception ex)
            {
                // Hiba visszaadása

            }
        }
        public async Task SyncCheckListTemplates()
        {
            try
            {
                await PrepareAuthenticatedRequestAsync();
                // Az API végpont meghatározása
                string endpoint = $"{_httpClient.BaseAddress}api/Sync/GetAllCheckListTemplateHeader";

                // HTTP GET kérés küldése
                var response = await _httpClient.GetFromJsonAsync<ApiResponseDTO<List<CheckListTemplateHeaderDTO>>>(endpoint);
                if (response != null)
                {
                    if (response.IsSuccess)
                    {
                        var checkListTemplateHeaderList = await _dbContext.CheckListTemplateHeaders.ToListAsync();
                        var missingItems = response.Data.Where(f => !checkListTemplateHeaderList.Any(s => s.Id == f.Id)).ToList();

                        var missingCheckListTemplateHeaders = _mapper.Map<List<CheckListTemplateHeader>>(missingItems);

                        _dbContext.CheckListTemplateHeaders.AddRange(missingCheckListTemplateHeaders);
                        await _dbContext.SaveChangesAsync();
                    }
                }
            }
            catch (Exception ex)
            {
                // Hiba visszaadása

            }
        }
    }
}
