package it.integry.integrywmsnative.core.services.inventario;

import androidx.databinding.ObservableArrayList;

import com.annimon.stream.Stream;

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

import javax.inject.Singleton;

import it.integry.integrywmsnative.core.expansion.RunnableArgs;
import it.integry.integrywmsnative.core.model.MtbAart;
import it.integry.integrywmsnative.core.model.MtbColr;
import it.integry.integrywmsnative.core.model.MtbColt;
import it.integry.integrywmsnative.core.model.MtbDepoPosizione;
import it.integry.integrywmsnative.core.model.MvwSitArtUdcDetInventario;
import it.integry.integrywmsnative.core.rest.consumers.GiacenzaRESTConsumer;

@Singleton
public class InventarioService {

    private final GiacenzaRESTConsumer mGiacenzaRESTConsumer;

    public InventarioService(GiacenzaRESTConsumer giacenzaRESTConsumer) {
        this.mGiacenzaRESTConsumer = giacenzaRESTConsumer;
    }

    public void getInventarioDaPosizione(MtbDepoPosizione posizione, RunnableArgs<List<MvwSitArtUdcDetInventario>> onSuccess, RunnableArgs<Exception> onError) {
        mGiacenzaRESTConsumer.getGiacenzeInPosizione(posizione, onSuccess, onError);
    }

    public void getInventarioDaPosizionePerArticolo(MtbDepoPosizione posizione, MtbAart articolo, RunnableArgs<List<MvwSitArtUdcDetInventario>> onSuccess, RunnableArgs<Exception> onError) {
        getInventarioDaPosizione(posizione, (inventario) -> {
            List<MvwSitArtUdcDetInventario> inventarioArticolo = Stream.of(inventario).filter(
                    x -> articolo.getCodMart().equalsIgnoreCase(x.getCodMart())
            ).toList();
            onSuccess.run(inventarioArticolo);
        }, onError);
    }

    public MtbColt mapInventarioToMtbColt(MvwSitArtUdcDetInventario inventario) {
        return mapInventarioToMtbColt(inventario, true);
    }

    public MtbColt mapInventarioToMtbColt(MvwSitArtUdcDetInventario inventario, boolean includeRows) {
        MtbColt mtbColt = new MtbColt();

        mtbColt.setGestione(inventario.getGestione());
        mtbColt.setDataCollo(inventario.getDataCollo());
        mtbColt.setSerCollo(inventario.getSerCollo());
        mtbColt.setNumCollo(inventario.getNumCollo());
        mtbColt.setSegno(inventario.getSegno());
        mtbColt.setCodMdep(inventario.getCodMdep());
        mtbColt.setPosizione(inventario.getPosizione());
        mtbColt.setCodJcom(inventario.getCodJcom());
        mtbColt.setPesoNettoKg(inventario.getPesoNettoKg());
        mtbColt.setPesoKg(inventario.getPesoLordoKg());
        mtbColt.setCodJfas(inventario.getCodJfas());

        if (includeRows) {
            ObservableArrayList<MtbColr> rows = new ObservableArrayList<>();
            rows.add(mapInventarioToMtbColr(inventario));
            mtbColt.setMtbColr(rows);
        }
        return mtbColt;
    }

    public MtbColr mapInventarioToMtbColr(MvwSitArtUdcDetInventario inventario) {
        MtbColr mtbColr = new MtbColr();

        mtbColr.setGestione(inventario.getGestione());
        mtbColr.setDataCollo(inventario.getDataCollo());
        mtbColr.setSerCollo(inventario.getSerCollo());
        mtbColr.setNumCollo(inventario.getNumCollo());
        mtbColr.setCodMart(inventario.getCodMart());
        mtbColr.setDescrizione(inventario.getDescrizioneEstesa());
        mtbColr.setPartitaMag(inventario.getPartitaMag());
        mtbColr.setCodJcom(inventario.getCodJcom());
        mtbColr.setUntMis(inventario.getUntMis());
        mtbColr.setQtaCol(inventario.getQtaCol());
        mtbColr.setNumCnf(inventario.getNumCnf());
        mtbColr.setQtaCnf(inventario.getQtaCnf());
        mtbColr.setPesoNettoKg(inventario.getPesoNettoKg());
        mtbColr.setPesoLordoKg(inventario.getPesoLordoKg());
        mtbColr.setMtbAart(inventario.getMtbAart());


        return mtbColr;
    }

    public List<MtbColt> mapInventarioToMtbColtList(List<MvwSitArtUdcDetInventario> inventario) {
        return new ArrayList<>(Stream.of(inventario).groupBy(x -> x.getNumCollo() + "-" + x.getDataCollo() + "-" + x.getSerCollo() + x.getGestione()).map(inv -> {
            List<MvwSitArtUdcDetInventario> rows = inv.getValue();
            MtbColt mtbColt = mapInventarioToMtbColt(rows.get(0), false);
            ObservableArrayList<MtbColr> mtbColrs = new ObservableArrayList<>();
            mtbColrs.addAll(Stream.of(rows).map(this::mapInventarioToMtbColr).toList());
            mtbColt.setMtbColr(mtbColrs);
            return mtbColt;
        }).toList());
    }


}
