using e_suite.API.Common.models;
using e_suite.API.Common.repository;
using e_suite.Database.Audit;
using e_suite.Database.Core.Tables.Domain;
using e_suite.Database.Core.Tables.Forms;
using e_suite.Database.Core.Tables.Glossaries;
using e_suite.UnitTestCore;
using Sequence = e_suite.Database.Core.Tables.Sequences.Sequence;

namespace e_suite.Modules.CustomFieldManagerUnitTests;

public class FakeCustomFieldReferenceRepository : FakeRepository, ICustomFieldReferenceObjectRepository
{
    public List<CustomFieldFormTemplate> CustomFieldFormTemplates { get; set; } = [];
    public List<CustomFieldGlossary> CustomFieldGlossaries { get; set; } = [];

    public List<CustomFieldSequence> CustomFieldSequences { get; set; } = [];

    public List<CustomFieldNumber> CustomFieldNumber { get; set; } = [];

    public List<CustomFieldText> CustomFieldText { get; set; } = [];

    public List<Sequence> Sequences { get; set; } = [];

    public List<Glossary> Glossaries { get; set; } = [];

    public List<Domain> Domains { get; set; } = [];

    public List<FormTemplate> FormTemplates { get; set; } = [];

    public Task CreateFormReferenceAsync(AuditUserDetails auditUserDetails, long customFieldId, long formId, CancellationToken cancellationToken)
    {
        CustomFieldFormTemplates.Add(new CustomFieldFormTemplate
        {
            CustomFieldId = customFieldId,
            FormTemplateId = formId,
        });

        return Task.CompletedTask;
    }

    public Task CreateGlossaryReferenceAsync(AuditUserDetails auditUserDetails, long customField, long glossaryId, CancellationToken cancellationToken)
    {
        CustomFieldGlossaries.Add(new CustomFieldGlossary
        {
            GlossaryId = glossaryId,
            CustomFieldId = customField,
        });

        return Task.CompletedTask;
    }

    public Task CreateDomainReferenceAsync(AuditUserDetails auditUserDetails, long customField, long formId, CancellationToken cancellationToken)
    {
        throw new NotImplementedException();
    }

    public Task CreateSequenceReferenceAsync(AuditUserDetails auditUserDetails, long customField, long sequenceId, CancellationToken cancellationToken)
    {
        var seq = new CustomFieldSequence
        {
            CustomFieldId = customField,
            SequenceId = sequenceId,
        };
        CustomFieldSequences.Add(seq);
        return Task.CompletedTask;
    }


    public Task DeleteFormReferenceAsync(AuditUserDetails auditUserDetails, long cusotmFieldId, CancellationToken cancellationToken)
    {
        var customFieldForm = CustomFieldFormTemplates.FirstOrDefault(x => x.CustomFieldId == cusotmFieldId);
        CustomFieldFormTemplates.Remove(customFieldForm!);
        return Task.CompletedTask;
    }

    public Task DeleteGlossaryReferenceAsync(AuditUserDetails auditUserDetails, long customField, CancellationToken cancellationToken)
    {
        var customFieldGlossary = CustomFieldGlossaries.FirstOrDefault(g => g.CustomFieldId == customField);
        CustomFieldGlossaries.Remove(customFieldGlossary!);
        return Task.CompletedTask;
    }

    public Task DeleteDomainReferenceAsync(AuditUserDetails auditUserDetails, long customField, CancellationToken cancellationToken)
    {
        throw new NotImplementedException();
    }

    public Task DeleteSequenceReferenceAsync(AuditUserDetails auditUserDetails, long customFiled, CancellationToken cancellationToken)
    {
        var customFieldSeq = CustomFieldSequences.FirstOrDefault(x => x.CustomFieldId == customFiled);
        CustomFieldSequences.Remove(customFieldSeq!);
        return Task.CompletedTask;
    }


    public Task EditFormReferenceAsync(AuditUserDetails auditUserDetails, CustomFieldFormTemplate customFieldFormTemplate, CancellationToken cancellationToken)
    {
        var customFieldForm = CustomFieldFormTemplates.FirstOrDefault(x => x.CustomFieldId == customFieldFormTemplate.CustomFieldId);
        customFieldForm!.FormTemplateId = customFieldFormTemplate.FormTemplateId;
        return Task.CompletedTask;
    }

    public Task EditGlossaryReferenceAsync(AuditUserDetails auditUserDetails, CustomFieldGlossary customFieldGlossary, CancellationToken cancellationToken)
    {
        var customFieldGlossaryRef = CustomFieldGlossaries.FirstOrDefault(x => x.CustomFieldId == customFieldGlossary.CustomFieldId);
        customFieldGlossaryRef!.GlossaryId = customFieldGlossary.GlossaryId;
        return Task.CompletedTask;
    }

    public Task EditDomainReferenceAsync(AuditUserDetails auditUserDetails, CustomFieldGlossary customFieldGlossary, CancellationToken cancellationToken)
    {
        throw new NotImplementedException();
    }

    public Task EditSequenceReferenceAsync(AuditUserDetails auditUserDetails, CustomFieldSequence customFieldSequence, CancellationToken cancellationToken)
    {
        var customFieldSequenceRef = CustomFieldSequences.FirstOrDefault(x => x.CustomFieldId == customFieldSequence.CustomFieldId);
        customFieldSequenceRef!.SequenceId = customFieldSequence.SequenceId;
        return Task.CompletedTask;
    }
    

    public Task<CustomFieldFormTemplate?> GetFormReferenceAsync(long customFieldId, CancellationToken cancellationToken)
    {
        var res = CustomFieldFormTemplates.FirstOrDefault(x => x.CustomFieldId == customFieldId);
        return Task.FromResult(res);
    }
    
    public Task<CustomFieldSequence?> GetSequenceReferenceAsync(long customFieldId, CancellationToken cancellationToken)
    {
        var res = CustomFieldSequences.FirstOrDefault(x => x.CustomFieldId == customFieldId);
        return Task.FromResult(res);
    }

    public Task<CustomFieldGlossary?> GetGlossaryReferenceAsync(long cusotmFieldId, CancellationToken cancellationToken)
    {
        return Task.FromResult(CustomFieldGlossaries.FirstOrDefault(x => x.CustomFieldId == cusotmFieldId));
    }
    
    public Task<CustomFieldNumber?> GetNumbersParametersAsync(long customFieldId, CancellationToken cancellationToken)
    {
        return Task.FromResult(CustomFieldNumber.FirstOrDefault(x => x.CustomFieldId == customFieldId));
    }

    public Task CreateCustomFieldNumbersAsync(
        AuditUserDetails auditUserDetails,
        CustomFieldNumber customFieldNumber,
        CancellationToken cancellationToken
    )
    {
        CustomFieldNumber.Add(customFieldNumber);
        return Task.CompletedTask;
    }

    public Task EditCustomFieldNumbersAsync(
        AuditUserDetails auditUserDetails,
        CustomFieldNumber customFieldNumber,
        NumberFieldParameters numberFieldParameters,
        CancellationToken cancellationToken
    )
    {
        return Task.CompletedTask;
        //Does not need to do anything in the fake repo.
    }

    public Task DeleteCustomFieldNumbersAsync(
        AuditUserDetails auditUserDetails,
        CustomFieldNumber customFieldNumber,
        CancellationToken cancellationToken
    )
    {
        CustomFieldNumber.Remove(customFieldNumber);
        return Task.CompletedTask;
    }

    public Task<Glossary?> GetGlossaryByGeneralRefIdAsync(IGeneralIdRef? idRef, CancellationToken cancellationToken)
    {
        var res = Glossaries.FirstOrDefault(x => x.Id == idRef?.Id || x.Guid == idRef?.Guid);
        return Task.FromResult(res);
    }

    public Task<Domain?> GetDomainByGeneralRefIdAsync(IGeneralIdRef? idRef, CancellationToken cancellationToken)
    {
        var res = Domains.FirstOrDefault(x => x.Id == idRef?.Id || x.Guid == idRef?.Guid);
        return Task.FromResult(res);
    }

    public Task<Sequence?> GetSequenceByGeneralRefIdAsync(IGeneralIdRef? idRef, CancellationToken cancellationToken)
    {
        var res = Sequences.FirstOrDefault(x => x.Id == idRef?.Id || x.Guid == idRef?.Guid);
        return Task.FromResult(res);
    }
    public Task<FormTemplate?> GetFormTemplateByGeneralRefIdAsync(IGeneralIdRef? idRef, CancellationToken cancellationToken)
    {
        var res = FormTemplates.FirstOrDefault(x => x.Id == idRef?.Id || x.Guid == idRef?.Guid);
        return Task.FromResult(res);
    }

    public Task<CustomFieldText?> GetTextsParametersAsync(long customFieldId, CancellationToken cancellationToken)
    {
        return Task.FromResult(CustomFieldText.FirstOrDefault(x => x.CustomFieldId == customFieldId));
    }

    public Task CreateCustomFieldTextsAsync(AuditUserDetails auditUserDetails, CustomFieldText customFieldText, CancellationToken cancellationToken)
    {
        CustomFieldText.Add(customFieldText);
        return Task.CompletedTask;
    }

    public Task EditCustomFieldTextsAsync(AuditUserDetails auditUserDetails, CustomFieldText customFieldText, TextFieldParameters textFieldParameters, CancellationToken cancellationToken)
    {
        return Task.CompletedTask;
        //Does not need to do anything in the fake repo.
    }

    public Task DeleteCustomFieldTextsAsync(AuditUserDetails auditUserDetails, CustomFieldText customFieldText, CancellationToken cancellationToken)
    {
        CustomFieldText.Remove(customFieldText);
        return Task.CompletedTask;
    }
}