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

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.MtbColr;
import it.integry.integrywmsnative.core.model.MtbColt;
import it.integry.integrywmsnative.databinding.DialogBasketLuPage2Binding;
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_Page2_ViewModel extends DialogBasketLU_BaseViewModel implements IDialogBasketLUViewModel {

    private Context mContext;
    private DialogBasketLuPage2Binding mBinding;

    private MtbColt mMtbColt;
    private DialogBasketLU_Page2_ListAdapter mAdapter;

    private RunnableArgs<Object> mOnConfirm;

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

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

    @Override
    public void setInputData(Object data) {
        mMtbColt = (MtbColt) data;

    }

    @Override
    public void onPreShow() {
        if(mMtbColt != null && mMtbColt.getMtbColr() != null && mMtbColt.getMtbColr().size() > 0) initRecyclerView(mMtbColt.getMtbColr());
        mBinding.emptyView.setVisibility(mMtbColt != null && mMtbColt.getMtbColr() != null && mMtbColt.getMtbColr().size() > 0 ? View.GONE : View.VISIBLE);

        mBinding.buttonAbort.setOnClickListener(v -> {
            previous();
        });

        mBinding.buttonConfirm.setOnClickListener(v -> {
            mOnConfirm.run(mMtbColt);
        });
    }

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

    @Override
    public void setOnConfirmClickListener(RunnableArgs<Object> onConfirm) {
        mOnConfirm = onConfirm;
    }

    @Override
    public void setOnAbortClickListener(Runnable onAbort) {

    }


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

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

        mAdapter = new DialogBasketLU_Page2_ListAdapter(mContext, dataset);

        mBinding.dialogBasketLuMtbColrList.setAdapter(mAdapter);
    }
}
