package it.integry.integrywmsnative.gest.contab_doc_interni.dialog;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import it.integry.integrywmsnative.core.utility.UtilityString;
import it.integry.integrywmsnative.gest.contab_doc_interni.dialog.exception.DocumentRequiredException;
import it.integry.integrywmsnative.gest.contab_doc_interni.dialog.exception.FornitoreRequiredException;
import it.integry.integrywmsnative.gest.contab_doc_interni.dialog.exception.NoteRequiredException;
import it.integry.integrywmsnative.gest.contab_doc_interni.dialog.exception.TipoDocRequiredException;
import it.integry.integrywmsnative.gest.contab_doc_interni.dto.FornitoreDTO;
import it.integry.integrywmsnative.gest.contab_doc_interni.dto.GruppoArticoloDTO;
import it.integry.integrywmsnative.gest.contab_doc_interni.dto.TipoDocDTO;

public class DialogSelectDocInfoViewModel extends ViewModel {

    private final MutableLiveData<TipoDocDTO> tipoDoc = new MutableLiveData<>();
    private final MutableLiveData<GruppoArticoloDTO> gruppoArt = new MutableLiveData<>();
    private final MutableLiveData<FornitoreDTO> fornitore = new MutableLiveData<>();
    private final MutableLiveData<Date> dataDoc = new MutableLiveData<>();
    private final MutableLiveData<Integer> numDoc = new MutableLiveData<>();
    private final MutableLiveData<String> note = new MutableLiveData<>();

    private final MutableLiveData<List<FornitoreDTO>> availableFornitori = new MutableLiveData<>();


    private final MutableLiveData<Boolean> fornitoreRequired = new MutableLiveData<>(false);
    private final MutableLiveData<Boolean> documentRequired = new MutableLiveData<>(false);
    private final MutableLiveData<Boolean> noteRequired = new MutableLiveData<>(false);

    public DialogSelectDocInfoViewModel() {
        tipoDoc.observeForever(val -> fornitoreRequired.postValue(val != null && (val.getGestioneDoc().equals("T") || val.getGestioneDoc().equals("A") || (val.getGestioneDoc().equals("P") && !val.getGestione().equals("L")))));

        tipoDoc.observeForever(val -> documentRequired.postValue(val != null && val.getGestioneDoc().equalsIgnoreCase("P") && !val.getGestione().equalsIgnoreCase("L")));

        tipoDoc.observeForever(val -> noteRequired.postValue(val != null && val.getGestioneDoc().equals("P") && val.getGestione().equals("L")));
    }

    public MutableLiveData<List<FornitoreDTO>> getAvailableFornitori() {
        return availableFornitori;
    }

    public void setFornitori(List<FornitoreDTO> fornitori) {
        tipoDoc.observeForever(val -> {
            List<FornitoreDTO> availableFornitoriList = fornitori.stream().filter(forn -> {
                if (val.getGestioneDoc().equalsIgnoreCase("T")) {
                    return forn.getTipoAnag().equalsIgnoreCase("D") && forn.getGestioneAnag().equalsIgnoreCase(val.getGestione().equalsIgnoreCase("V") ? "V" : "L");
                } else {
                    return forn.getTipoAnag().equalsIgnoreCase("F");
                }
            }).collect(Collectors.toList());

            availableFornitori.postValue(availableFornitoriList);
        });
    }


    public MutableLiveData<Boolean> isFornitoreRequired() {
        return fornitoreRequired;
    }

    public MutableLiveData<Boolean> isDocumentRequired() {
        return documentRequired;
    }

    public MutableLiveData<Boolean> isNoteRequired() {
        return noteRequired;
    }


    public MutableLiveData<TipoDocDTO> getTipoDoc() {
        return tipoDoc;
    }

    public DialogSelectDocInfoViewModel setTipoDoc(TipoDocDTO tipoDoc) {
        this.tipoDoc.postValue(tipoDoc);
        return this;
    }

    public GruppoArticoloDTO getGruppoArt() {
        return gruppoArt.getValue();
    }

    public DialogSelectDocInfoViewModel setGruppoArt(GruppoArticoloDTO gruppoArt) {
        this.gruppoArt.postValue(gruppoArt);
        return this;
    }

    public FornitoreDTO getFornitore() {
        return fornitore.getValue();
    }

    public DialogSelectDocInfoViewModel setFornitore(FornitoreDTO fornitore) {
        this.fornitore.postValue(fornitore);
        return this;
    }

    public Date getDataDoc() {
        return dataDoc.getValue();
    }

    public DialogSelectDocInfoViewModel setDataDoc(Date dataDoc) {
        this.dataDoc.postValue(dataDoc);
        return this;
    }

    public Integer getNumDoc() {
        return numDoc.getValue();
    }

    public DialogSelectDocInfoViewModel setNumDoc(Integer numDoc) {
        this.numDoc.postValue(numDoc);
        return this;
    }

    public String getNote() {
        return note.getValue();
    }

    public DialogSelectDocInfoViewModel setNote(String note) {
        this.note.postValue(note);
        return this;
    }


    public boolean validate() throws TipoDocRequiredException, FornitoreRequiredException, DocumentRequiredException, NoteRequiredException {
        if (this.getTipoDoc().getValue() == null) {
            throw new TipoDocRequiredException();
        }

        if (Boolean.TRUE.equals(this.isFornitoreRequired().getValue()) && this.getFornitore() == null) {
            throw new FornitoreRequiredException();
        }

        if (Boolean.TRUE.equals(this.isDocumentRequired().getValue()) && (this.getDataDoc() == null || this.getNumDoc() == null)) {
            throw new DocumentRequiredException();
        }


        if (Boolean.TRUE.equals(this.isNoteRequired().getValue()) && UtilityString.isNullOrEmpty(this.getNote())) {
            throw new NoteRequiredException();
        }

        return true;
    }
}