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

import com.annimon.stream.Stream;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import it.integry.integrywmsnative.core.interfaces.viewmodel_listeners.ILoadingListener;
import it.integry.integrywmsnative.core.settings.SettingsManager;
import it.integry.integrywmsnative.gest.contab_doc_interni.dto.DocInterniSetupDTO;
import it.integry.integrywmsnative.gest.contab_doc_interni.dto.FornitoreDTO;
import it.integry.integrywmsnative.gest.contab_doc_interni.dto.GruppiArticoloDTO;
import it.integry.integrywmsnative.gest.contab_doc_interni.dto.TipiDocDTO;


public class DialogSelectDocInfoViewModel {


    private DocInterniSetupDTO mDocInterniSetupDTO;

    private String mTipoDoc;

    private String mGruppoArt;

    private String mFornitore;

    private Date mDataDoc;

    private String mNumDoc;

    private String mNote;

    private Listener mListener;

    public void init(DocInterniSetupDTO initialList) {
        this.mDocInterniSetupDTO = initialList;
        this.mTipoDoc = null;
        this.mGruppoArt = null;
        this.mFornitore = null;
        this.mDataDoc = null;
        this.mNumDoc = null;
        this.mNote = null;
        this.mListener = null;
    }

    public String getCodDtip() {
        return mTipoDoc;
    }

    public void setCodDtip(String codDtip) {
        this.mTipoDoc = codDtip;
        if (this.getSelectedTipoDoc() != null) {
            this.mListener.onDocTypeSelected();
        }
    }

    public String getCodMgrp() {
        return mGruppoArt;
    }

    public void setCodMgrp(String codMgrp) {
        this.mGruppoArt = codMgrp;
    }

    public void setCodAnag(String codAnag) {
        this.mFornitore = codAnag;
    }

    public Date getDataDoc() {
        return mDataDoc;
    }

    public void setDataDoc(Date mDataDoc) {
        this.mDataDoc = mDataDoc;
    }

    public String getNumDoc() {
        return mNumDoc;
    }

    public void setNumDoc(String numDoc) {
        this.mNumDoc = numDoc;
    }

    public String getNote() {
        return mNote;
    }

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

    public DialogSelectDocInfoViewModel setListeners(Listener listener) {
        this.mListener = listener;
        return this;
    }

    public List<String> getAvailableCodDtips() {
        return mDocInterniSetupDTO.getTipiDoc() != null ? Stream.of(mDocInterniSetupDTO.getTipiDoc()).map(TipiDocDTO::getLabel).toList() : new ArrayList<>();
    }

    public List<String> getAvailableCodMgrps() {
        return mDocInterniSetupDTO.getGruppiArt() != null ? Stream.of(mDocInterniSetupDTO.getGruppiArt()).map(GruppiArticoloDTO::getLabel).toList() : new ArrayList<>();
    }

    public List<String> getAvailableFornitori() {
        if (!this.requiresFornitore()) {
            return new ArrayList<>();
        }
        TipiDocDTO selectedDocType = getSelectedTipoDoc();

        return Stream.of(mDocInterniSetupDTO.getFornitori())
                .filter(forn -> {
                    if (selectedDocType.getGestioneDoc().equalsIgnoreCase("T")) {
                        return forn.getTipoAnag().equalsIgnoreCase("D") &&
                                forn.getGestioneAnag().equalsIgnoreCase(
                                        selectedDocType.getGestione().equalsIgnoreCase("V") ? "V" : "A"
                                );
                    } else {
                        return forn.getTipoAnag().equalsIgnoreCase("F");
                    }
                })
                .map(FornitoreDTO::getLabel).toList();
    }

    public TipiDocDTO getSelectedTipoDoc() {
        return Stream.of(mDocInterniSetupDTO.getTipiDoc()).filter(x -> x.getLabel().equals(mTipoDoc)).findFirstOrElse(null);
    }

    public FornitoreDTO getSelectedFornitore() {
        return Stream.of(mDocInterniSetupDTO.getFornitori()).filter(x -> x.getLabel().equals(mFornitore)).findFirstOrElse(null);
    }

    public GruppiArticoloDTO getSelectedGruppoArt() {
        if (mGruppoArt == null) {
            return null;
        }

        return Stream.of(mDocInterniSetupDTO.getGruppiArt()).filter(x -> x.getLabel().equals(mGruppoArt)).findFirstOrElse(null);
    }

    public boolean requiresFornitore() {
        return this.getSelectedTipoDoc() != null && (
                this.getSelectedTipoDoc().getGestioneDoc().equals("T") ||
                        this.getSelectedTipoDoc().getGestioneDoc().equals("A") ||
                        (
                                this.getSelectedTipoDoc().getGestioneDoc().equals("P") &&
                                        !this.getSelectedTipoDoc().getGestione().equals("L")
                        )
        );
    }

    public boolean requiresNote() {
        return this.getSelectedTipoDoc() != null &&
                this.getSelectedTipoDoc().getGestioneDoc().equals("P") &&
                this.getSelectedTipoDoc().getGestione().equals("L");
    }

    public boolean isInputValid() {
        if (this.getSelectedTipoDoc() == null) {
            return false;
        }

        if (this.requiresFornitore() && this.getSelectedFornitore() == null) {
            return false;
        }

        if (this.requiresDocData() && (this.getDataDoc() == null || this.getNumDoc() == null)) {
            return false;
        }


        if (this.requiresNote() && (this.getNote() == null || this.getNote().length() <= 0)) {
            return false;
        }

        return true;
    }

    public boolean requiresDocData() {
        TipiDocDTO doc = this.getSelectedTipoDoc();
        return doc != null && doc.getGestioneDoc().equalsIgnoreCase("P") && !doc.getGestione().equalsIgnoreCase("L");
    }

    public List<String> getAdditionalNotes() {
        return SettingsManager.iDB().getNotePerditaDocInterni();
    }

    public interface Listener extends ILoadingListener {

        void onError(Exception ex);

        void onDocTypeSelected();

    }
}
