using IntegryApiClient.Core.Domain.Abstraction.Contracts.Account;
using IntegryApiClient.Core.Domain.RestClient.Contacts;
using salesbook.Shared.Core.Dto;
using salesbook.Shared.Core.Dto.Activity;
using salesbook.Shared.Core.Dto.JobProgress;
using salesbook.Shared.Core.Entity;
using salesbook.Shared.Core.Interface;
using System.Net.Http.Headers;
using salesbook.Shared.Core.Dto.Contact;
using salesbook.Shared.Core.Interface.IntegryApi;

namespace salesbook.Shared.Core.Services;

public class IntegryApiService(IIntegryApiRestClient integryApiRestClient, IUserSession userSession)
    : IIntegryApiService
{
    public async Task<bool> SystemOk()
    {
        try
        {
            await integryApiRestClient.Get<object>("system/ok");
            return true;
        }
        catch (Exception e)
        {
            return false;
        }
    }
    
    public Task<List<StbActivity>?> RetrieveActivity(CRMRetrieveActivityRequestDTO activityRequest) =>
        integryApiRestClient.AuthorizedPost<List<StbActivity>?>("crm/retrieveActivity", activityRequest);

    public Task<List<JtbComt>?> RetrieveAllCommesse(string? dateFilter)
    {
        var queryParams = new Dictionary<string, object>();

        if (dateFilter != null)
        {
            queryParams.Add("dateFilter", dateFilter);
        }

        return integryApiRestClient.AuthorizedGet<List<JtbComt>?>("crm/retrieveCommesse", queryParams);
    }

    public Task<UsersSyncResponseDTO> RetrieveAnagClie(CRMAnagRequestDTO request) => 
        integryApiRestClient.AuthorizedPost<UsersSyncResponseDTO>("crm/retrieveClienti", request)!;

    public Task<UsersSyncResponseDTO> RetrieveProspect(CRMProspectRequestDTO request) => 
        integryApiRestClient.AuthorizedPost<UsersSyncResponseDTO>("crm/retrieveProspect", request)!;

    public Task<SettingsResponseDTO> RetrieveSettings() =>
        integryApiRestClient.AuthorizedGet<SettingsResponseDTO>("crm/retrieveSettings")!;

    public Task<List<CRMAttachedResponseDTO>?> RetrieveAttached(string codJcom)
    {
        var queryParams = new Dictionary<string, object>
        {
            { "codJcom", codJcom }
        };

        return integryApiRestClient.AuthorizedGet<List<CRMAttachedResponseDTO>?>("crm/retrieveAttachedForCodJcom",
            queryParams);
    }

    public Task DeleteActivity(string activityId)
    {
        var queryParams = new Dictionary<string, object>
        {
            { "activityId", activityId }
        };

        return integryApiRestClient.AuthorizedGet<object>($"activity/delete", queryParams);
    }

    public Task<List<StbActivity>?> SaveActivity(ActivityDTO activity) =>
        integryApiRestClient.AuthorizedPost<List<StbActivity>?>("crm/saveActivity", activity);

    public Task<CRMCreateContactResponseDTO?> SaveContact(CRMCreateContactRequestDTO request) =>
        integryApiRestClient.AuthorizedPost<CRMCreateContactResponseDTO>("crm/createContact", request);

    public Task<CRMTransferProspectResponseDTO> TransferProspect(CRMTransferProspectRequestDTO request) =>
        integryApiRestClient.AuthorizedPost<CRMTransferProspectResponseDTO>("crm/transferProspect", request)!;

    public Task<CheckVatResponseDTO> CheckVat(CheckVatRequestDTO request) =>
        integryApiRestClient.Post<CheckVatResponseDTO>("checkPartitaIva", request)!;

    public Task<List<IndirizzoDTO>?> Geocode(string address)
    {
        var queryParams = new Dictionary<string, object>
        {
            { "address", address },
            { "retrieveAll", true }
        };

        return integryApiRestClient.Get<List<IndirizzoDTO>>("geocode", queryParams);
    }

    public Task<List<AutoCompleteAddressDTO>?> AutoCompleteAddress(string address, string language, string uuid)
    {
        var queryParams = new Dictionary<string, object>
        {
            { "address", address },
            { "language", language },
            { "uuid", uuid }
        };

        return integryApiRestClient.Get<List<AutoCompleteAddressDTO>>("google/places/autoCompleteAddress", queryParams);
    }

    public Task<IndirizzoDTO?> PlaceDetails(string placeId, string uuid)
    {
        var queryParams = new Dictionary<string, object>
        {
            { "placeId", placeId },
            { "uuid", uuid }
        };

        return integryApiRestClient.Get<IndirizzoDTO>("google/places/placeDetails", queryParams);
    }

    public Task UploadFile(string activityId, byte[] file, string fileName)
    {
        var queryParams = new Dictionary<string, object> { { "activityId", activityId } };

        var content = new MultipartFormDataContent();
        var fileContent = new ByteArrayContent(file);
        fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/form-data");
        content.Add(fileContent, "files", fileName);

        return integryApiRestClient.Post<object>($"uploadStbActivityFileAttachment", content, queryParams);
    }

    public Task<List<ActivityFileDto>> GetActivityFile(string activityId)
    {
        var queryParams = new Dictionary<string, object>
        {
            { "activityId", activityId }
        };

        return integryApiRestClient.AuthorizedGet<List<ActivityFileDto>>($"activity/getActivityFile", queryParams)!;
    }

    public Task<Stream> DownloadFile(string activityId, string fileName) =>
        integryApiRestClient.Download($"downloadStbFileAttachment/{activityId}/{fileName}")!;

    public Task<Stream> DownloadFileFromRefUuid(string refUuid, string fileName)
    {
        var queryParams = new Dictionary<string, object>
        {
            { "refUuid", refUuid },
            { "fileName", fileName }
        };

        return integryApiRestClient.Download("downloadFileFromRefUuid", queryParams);
    }

    public Task<PositionDTO> SavePosition(PositionDTO position) =>
        integryApiRestClient.Post<PositionDTO>("savePosition", position)!;

    public Task<PositionDTO> RetrievePosition(string id)
    {
        var queryParams = new Dictionary<string, object> { { "id", id } };

        return integryApiRestClient.Get<PositionDTO>("retrievePosition", queryParams)!;
    }

    public Task<CRMJobProgressResponseDTO> RetrieveJobProgress(string codJcom)
    {
        var queryParams = new Dictionary<string, object> { { "codJcom", codJcom } };

        return integryApiRestClient.Get<CRMJobProgressResponseDTO>("crm/retrieveJobProgress", queryParams)!;
    }
}