package it.integry.integrywmsnative.view.dialogs.basket_lu.pages.page1;

import android.content.Context;
import android.view.View;

import androidx.core.content.ContextCompat;
import androidx.databinding.ViewDataBinding;
import androidx.recyclerview.widget.LinearLayoutManager;

import java.util.List;

import it.integry.integrywmsnative.R;
import it.integry.integrywmsnative.core.expansion.RunnableArgs;
import it.integry.integrywmsnative.core.model.MtbColt;
import it.integry.integrywmsnative.core.rest.consumers.ColliMagazzinoRESTConsumer;
import it.integry.integrywmsnative.databinding.DialogBasketLuPage1Binding;
import it.integry.integrywmsnative.ui.SimpleDividerItemDecoration;
import it.integry.integrywmsnative.view.dialogs.basket_lu.interfaces.DialogBasketLU_BaseViewModel;
import it.integry.integrywmsnative.view.dialogs.basket_lu.interfaces.IDialogBasketLUViewModel;

public class DialogBasketLU_Page1_ViewModel extends DialogBasketLU_BaseViewModel implements IDialogBasketLUViewModel {

    private Context mContext;
    private DialogBasketLuPage1Binding mBinding;

    private List<MtbColt> availableMtbColts;
    private DialogBasketLU_Page1_ListAdapter mAdapter;

    @Override
    public void setContext(Context context) {
        this.mContext = context;
    }

    @Override
    public void setBinding(ViewDataBinding binding) {
        this.mBinding = (DialogBasketLuPage1Binding) binding;
    }

    @Override
    public void setInputData(Object data) {

    }

    @Override
    public void onShow() {
        this.mBinding.getRoot().requestLayout();

        mBinding.constraintLayout.setMaxHeight((int) (mContext.getResources().getDisplayMetrics().heightPixels * 0.75));

        ColliMagazzinoRESTConsumer.retrieveBasketColli(mtbColts -> {
            availableMtbColts = mtbColts;
            if(mtbColts != null && mtbColts.size() > 0) initRecyclerView(availableMtbColts);

            mBinding.emptyView.setVisibility(mtbColts != null && mtbColts.size() > 0 ? View.GONE : View.VISIBLE);
        }, ex -> {
            mBinding.emptyView.setVisibility(View.VISIBLE);
        });
    }

    @Override
    public void onPreShow() {
    }

    @Override
    public void setOnConfirmClickListener(RunnableArgs<Object> onConfirm) {

    }

    @Override
    public void setOnAbortClickListener(Runnable onAbort) {

    }





    private void initRecyclerView(List<MtbColt> dataset) {
        mBinding.dialogBasketLuMainList.setNestedScrollingEnabled(false);
        mBinding.dialogBasketLuMainList.setHasFixedSize(true);
        mBinding.dialogBasketLuMainList.setLayoutManager(new LinearLayoutManager(mContext));

        SimpleDividerItemDecoration itemDecorator = new SimpleDividerItemDecoration(mContext, SimpleDividerItemDecoration.VERTICAL);
        itemDecorator.setDrawable(ContextCompat.getDrawable(mContext, R.drawable.divider));
        mBinding.dialogBasketLuMainList.addItemDecoration(itemDecorator);

        mAdapter = new DialogBasketLU_Page1_ListAdapter(mContext, dataset, (item, position) -> {
            this.done(item);
        });

        mBinding.dialogBasketLuMainList.setAdapter(mAdapter);
    }
}
