package it.integry.integrywmsnative.gest.lista_bancali.viewmodel;

import android.app.ProgressDialog;

import androidx.databinding.ObservableArrayList;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;

import java.util.List;

import it.integry.integrywmsnative.R;
import it.integry.integrywmsnative.core.REST.consumers.ColliMagazzinoRESTConsumer;
import it.integry.integrywmsnative.core.interfaces.IRecyclerItemClicked;
import it.integry.integrywmsnative.core.model.MtbColr;
import it.integry.integrywmsnative.core.model.MtbColt;
import it.integry.integrywmsnative.core.utility.UtilityExceptions;
import it.integry.integrywmsnative.gest.lista_bancali.ListaBancaliActivity;
import it.integry.integrywmsnative.gest.lista_bancali.core.MainListListaColliAdapter;

public class ListaBancaliViewModel implements IRecyclerItemClicked<MtbColt> {

    private ListaBancaliActivity mContext;
    private ObservableArrayList<MtbColt> mMtbColts;

    private MainListListaColliAdapter mAdapter;

    public ListaBancaliViewModel(ListaBancaliActivity context, List<MtbColt> mtbColts){
        mContext = context;
        mMtbColts = new ObservableArrayList<>();
        mMtbColts.addAll(mtbColts);

        initRecyclerView();
    }

    public void initRecyclerView() {
        mContext.bindings.listaColliMainList.setHasFixedSize(true);

        mContext.bindings.listaColliMainList.setLayoutManager(new LinearLayoutManager(mContext));

        DividerItemDecoration itemDecorator = new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL);
        itemDecorator.setDrawable(ContextCompat.getDrawable(mContext, R.drawable.divider));
        mContext.bindings.listaColliMainList.addItemDecoration(itemDecorator);


        mAdapter = new MainListListaColliAdapter(mContext, mMtbColts, this, mContext.bindings.listaBancaliEmptyView);
        mContext.bindings.listaColliMainList.setAdapter(mAdapter);

        mContext.bindings.waterfallToolbar.setRecyclerView(mContext.bindings.listaColliMainList);

    }

    @Override
    public void onItemClick(MtbColt item, int position) {

        final ProgressDialog progress = ProgressDialog.show(mContext, mContext.getText(R.string.waiting),
                mContext.getText(R.string.loading) + " ...", true);

        try {
            ColliMagazzinoRESTConsumer.getByTestata(item, true, false, mtbColt -> {

                ObservableArrayList<MtbColr> mtbColrObservableArrayList = new ObservableArrayList<>();
                mtbColrObservableArrayList.addAll(mtbColt.getMtbColr());
                item.setMtbColr(mtbColrObservableArrayList);

                progress.dismiss();

                mContext.startContenutoBancaleActivity(item);

            }, ex -> UtilityExceptions.defaultException(mContext, ex, progress));

            /*String sql = "SELECT *, mtb_aart.descrizione, mtb_aart.unt_mis " +
                    "FROM mtb_colr " +
                    "LEFT OUTER JOIN mtb_aart ON mtb_colr.cod_mart = mtb_aart.cod_mart " +
                    "WHERE " +
                    "mtb_colr.data_collo = " + UtilityDB.valueToString(item.getDataColloD()) + " AND " +
                    "mtb_colr.ser_collo = " + UtilityDB.valueToString(item.getSerCollo()) + " AND " +
                    "mtb_colr.num_collo = " + UtilityDB.valueToString(item.getNumCollo()) + " AND " +
                    "mtb_colr.gestione = " + UtilityDB.valueToString(item.getGestione());


            Type typeOfObjectsList = new TypeToken<ArrayList<MtbColr>>() {}.getType();
            SystemRESTConsumer.processSql(sql, typeOfObjectsList, new ISimpleOperationCallback<List<MtbColr>>() {
                @Override
                public void onSuccess(List<MtbColr> value) {
                    ObservableArrayList<MtbColr> mtbColrObservableArrayList = new ObservableArrayList<>();
                    mtbColrObservableArrayList.addAll(value);
                    item.setMtbColr(mtbColrObservableArrayList);

                    progress.dismiss();

                    mContext.startContenutoBancaleActivity(item);

                }

                @Override
                public void onFailed(Exception ex) {
                    UtilityExceptions.defaultException(mContext, ex, progress);
                }
            });
            */


        } catch (Exception e) {
            e.printStackTrace();
            UtilityExceptions.defaultException(mContext, e, progress);
        }
    }

}
