package it.integry.integrywmsnative.gest.vendita_ordine_inevaso.core;

import android.content.Context;
import androidx.databinding.ObservableArrayList;
import android.view.View;

import com.annimon.stream.Stream;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import it.integry.integrywmsnative.R;
import it.integry.integrywmsnative.core.model.MtbColr;
import it.integry.integrywmsnative.core.utility.UtilityString;
import it.integry.integrywmsnative.gest.vendita.dto.PickingObjectDTO;
import it.integry.integrywmsnative.gest.vendita_ordine_inevaso.dto.VenditaOrdineInevasoListViewModel;

public class VenditaOrdineInevasoHelper {

    private Context mContext;

    private ObservableArrayList<VenditaOrdineInevasoListViewModel> mListViewModels = new ObservableArrayList<>();

    public VenditaOrdineInevasoHelper(Context context){
        mContext = context;
    }

    public MainListOrdineVenditaInevasoAdapter getRightListAdapter(View emptyView) {
        return new MainListOrdineVenditaInevasoAdapter(mContext, mListViewModels, emptyView);
    }

    public void updateListModel(List<List<PickingObjectDTO>> mDataset, List<MtbColr> mtbColrs) throws Exception {

        mListViewModels.clear();

        mListViewModels.addAll(getListViewModelPerPosizione(mDataset, mtbColrs));

    }

    public List<List<PickingObjectDTO>> getPickingListRaggruppataPerPosizione(List<PickingObjectDTO> mPickingList, boolean forceHiddenCheck){

        List<String> listOfKnownPositions = Stream.of(mPickingList)
                .map(PickingObjectDTO::getPosizione)
                .distinctBy(x -> x)
                .withoutNulls()
                .sortBy(x -> x)
                .toList();

        listOfKnownPositions.add(null);

        List<List<PickingObjectDTO>> groupedRighe = new ArrayList<>();
        for (String position : listOfKnownPositions) {

            List<PickingObjectDTO> tmpList = Stream.of(mPickingList)
                    .filter(x -> (x.getPosizione() == null && position == null) ||
                            (x.getPosizione() != null && x.getPosizione().equalsIgnoreCase(position))).toList();

            for(int i = 0; i < tmpList.size(); i++){
                if(tmpList.get(i).isHidden() == null || forceHiddenCheck) {
                    PickingObjectDTO tmpItem = tmpList.get(i);

                    if (tmpItem.getQtaCollo().floatValue() <= 0) {
                        tmpItem.setHidden(true);
                    } else tmpItem.setHidden(false);

                    tmpItem.setTempHidden(false);
                }
            }

            if(tmpList.size() > 0){
                groupedRighe.add(tmpList);
            }

        }

        return groupedRighe;
    }


    public List<VenditaOrdineInevasoListViewModel> getListViewModelPerPosizione(List<List<PickingObjectDTO>> groupedRighe, List<MtbColr> mtbColrs) {

        List<VenditaOrdineInevasoListViewModel> listModel = new ArrayList<>();

        for(int i = 0; i < groupedRighe.size(); i++) {
            List<PickingObjectDTO> currentGroup = groupedRighe.get(i);

            VenditaOrdineInevasoListViewModel itemModel = new VenditaOrdineInevasoListViewModel();


            String posizione = UtilityString.isNullOrEmpty(currentGroup.get(0).getPosizione()) ? "N.A." : currentGroup.get(0).getPosizione();
            itemModel.descrizioneGroup = mContext.getResources().getText(R.string.position_text) + ": " + posizione;
            itemModel.rows = new ArrayList<>();

            for(int k = 0; k < currentGroup.size(); k++) {
                final PickingObjectDTO currentItem = currentGroup.get(k);
                final VenditaOrdineInevasoListViewModel.SubItem rowModel = new VenditaOrdineInevasoListViewModel.SubItem();



                String badge1 = currentItem.getCodAlis() + " - " + (!UtilityString.isNullOrEmpty(currentItem.getCodArtFor()) ? currentItem.getCodArtFor() : currentItem.getCodMart());
                rowModel.setBadge1(badge1);

                rowModel.setBadge2(UtilityString.isNullOrEmpty(currentItem.getCodJcom()) ? null : currentItem.getCodJcom());

                rowModel.setDescrizione(currentItem.getDescrizioneEstesa());

                if(!UtilityString.isNullOrEmpty(currentItem.getPartitaMag())) {
                    rowModel.setSubDescrizione1(String.format(mContext.getResources().getText(R.string.batch_lot_text).toString(), currentItem.getPartitaMag()));
                }

                if(currentItem.getNumCollo() != null) {
                    rowModel.setSubDescrizione2(String.format(mContext.getResources().getText(R.string.lu_number_data_text).toString(), currentItem.getNumCollo(), currentItem.getDataColloHuman()));
                }

                rowModel.setQtaRiservata(getRigaQuantityEvasa(currentItem, mtbColrs));
                rowModel.setQtaOrdinata(currentItem.getNumCollo() != null ? currentItem.getQtaCollo() : currentItem.getQtaOrd());

                rowModel.setOriginalModel(currentItem);

                itemModel.rows.add(rowModel);
            }
            listModel.add(itemModel);
        }

        return listModel;

    }

    private BigDecimal getRigaQuantityEvasa(PickingObjectDTO item, List<MtbColr> mtbColrs){
        BigDecimal currentQtaEvasa = BigDecimal.ZERO;

        if(mtbColrs != null) {


//            List<MtbColr> filteredMtbColrs = Stream.of(mtbColrs).filter(
//                    x -> x.getCodMart().equalsIgnoreCase(item.getCodMart()) &&
//                            ((x.getCodJcom() == null && item.getCodJcom() == null) || (x.getCodJcom() != null && x.getCodJcom().equalsIgnoreCase(item.getCodJcom()))) &&
//                            (!SettingsManager.iDB().isEnableCheckPartitaMagCheckPickingV() || (SettingsManager.iDB().isEnableCheckPartitaMagCheckPickingV() && ((x.getPartitaMag() == null && item.getPartitaMag() == null) || (x.getPartitaMag() != null && x.getPartitaMag().equalsIgnoreCase(item.getPartitaMag()))))) &&
//                            ((x.getNumColloRif() == null && item.getNumCollo() == null) || (x.getNumColloRif() != null && x.getNumColloRif().equals(item.getNumCollo())))).toList();

            for (MtbColr mtbColr : item.getWithdrawRows()) {
                currentQtaEvasa = currentQtaEvasa.add(mtbColr.getQtaCol());
            }
        }

        return currentQtaEvasa;
    }

}
