using salesbook.Shared.Core.Helpers;
using salesbook.Shared.Core.Interface;

namespace salesbook.Maui.Core.Services;

public class SyncDbService(IIntegryApiService integryApiService, LocalDbService localDb) : ISyncDbService
{
    public async Task GetAndSaveActivity(string? dateFilter)
    {
        var allActivity = await integryApiService.RetrieveActivity(dateFilter);

        if (!allActivity.IsNullOrEmpty())
            if (dateFilter is null)
                await localDb.InsertAll(allActivity!);
            else
                await localDb.InsertOrUpdate(allActivity!);
    }

    public async Task GetAndSaveCommesse(string? dateFilter)
    {
        var allCommesse = await integryApiService.RetrieveAllCommesse(dateFilter);

        if (!allCommesse.IsNullOrEmpty())
            if (dateFilter is null)
                await localDb.InsertAll(allCommesse!);
            else
                await localDb.InsertOrUpdate(allCommesse!);
    }

    public async Task GetAndSaveProspect(string? dateFilter)
    {
        var taskSyncResponseDto = await integryApiService.RetrieveProspect(dateFilter);

        if (!taskSyncResponseDto.PtbPros.IsNullOrEmpty())
            if (dateFilter is null)
                await localDb.InsertAll(taskSyncResponseDto.PtbPros!);
            else
                await localDb.InsertOrUpdate(taskSyncResponseDto.PtbPros!);

        if (!taskSyncResponseDto.PtbProsRif.IsNullOrEmpty())
            if (dateFilter is null)
                await localDb.InsertAll(taskSyncResponseDto.PtbProsRif!);
            else
                await localDb.InsertOrUpdate(taskSyncResponseDto.PtbProsRif!);
    }

    public async Task GetAndSaveClienti(string? dateFilter)
    {
        var taskSyncResponseDto = await integryApiService.RetrieveAnagClie(dateFilter);

        if (!taskSyncResponseDto.AnagClie.IsNullOrEmpty())
            if (dateFilter is null)
                await localDb.InsertAll(taskSyncResponseDto.AnagClie!);
            else
                await localDb.InsertOrUpdate(taskSyncResponseDto.AnagClie!);

        if (!taskSyncResponseDto.VtbDest.IsNullOrEmpty())
            if (dateFilter is null)
                await localDb.InsertAll(taskSyncResponseDto.VtbDest!);
            else
                await localDb.InsertOrUpdate(taskSyncResponseDto.VtbDest!);

        if (!taskSyncResponseDto.VtbCliePersRif.IsNullOrEmpty())
            if (dateFilter is null)
                await localDb.InsertAll(taskSyncResponseDto.VtbCliePersRif!);
            else
                await localDb.InsertOrUpdate(taskSyncResponseDto.VtbCliePersRif!);
    }

    public async Task GetAndSaveSettings(string? dateFilter)
    {
        if (dateFilter is not null)
            await localDb.ResetSettingsDb();

        var settingsResponse = await integryApiService.RetrieveSettings();

        if (!settingsResponse.ActivityResults.IsNullOrEmpty())
            await localDb.InsertAll(settingsResponse.ActivityResults!);

        if (!settingsResponse.ActivityTypes.IsNullOrEmpty())
            await localDb.InsertAll(settingsResponse.ActivityTypes!);

        if (!settingsResponse.StbUsers.IsNullOrEmpty())
            await localDb.InsertAll(settingsResponse.StbUsers!);

        if (!settingsResponse.VtbTipi.IsNullOrEmpty())
            await localDb.InsertAll(settingsResponse.VtbTipi!);

        if (!settingsResponse.Nazioni.IsNullOrEmpty())
            await localDb.InsertAll(settingsResponse.Nazioni!);
    }
}