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

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import it.integry.integrywmsnative.ui.DeactivatableViewPager;
import it.integry.integrywmsnative.view.dialogs.basket_lu.interfaces.IDialogBasketLUViewModel;

public class DialogBasketLU_ViewPagerAdapter extends PagerAdapter implements ViewPager.OnPageChangeListener {

    private final Context mContext;

    private final List<Map.Entry<Integer, Class<?>>> mDatasetLayout;
    private final List<Map.Entry<ViewDataBinding, IDialogBasketLUViewModel>> mDatasetViews;

    private DeactivatableViewPager viewPager;

    public DialogBasketLU_ViewPagerAdapter(Context context, List<Map.Entry<Integer, Class<?>>> datasetLayouts) {
        this.mContext = context;
        this.mDatasetLayout = datasetLayouts;
        this.mDatasetViews = new ArrayList<>();
    }

    @Override
    public View instantiateItem(ViewGroup container, int position) {
        viewPager = (DeactivatableViewPager) container;

        Integer viewId = mDatasetLayout.get(position).getKey();


        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
        ViewDataBinding mBinding = DataBindingUtil.inflate(inflater, viewId, null, false);

        Class<?> viewModelClass = mDatasetLayout.get(position).getValue();
        IDialogBasketLUViewModel viewModel = null;

        try {
            viewModel = (IDialogBasketLUViewModel) viewModelClass.newInstance();
            viewModel.setBinding(mBinding);
            viewModel.setContext(mContext);
            viewModel.setOnDone(data -> {
                int newIndex = viewPager.getCurrentItem() + 1;

                if(newIndex <= getCount()) {
                    this.mDatasetViews.get(newIndex).getValue().setInputData(data);
                    this.mDatasetViews.get(newIndex).getValue().onPreShow();
                    viewPager.setCurrentItem(newIndex, true);
                }
            });
            viewModel.setOnPrev(() -> {
                int newIndex = viewPager.getCurrentItem() - 1;

                if(newIndex >= 0) {
                    viewPager.setCurrentItem(newIndex, true);
                }
            });
        } catch (IllegalAccessException | InstantiationException e) {
            e.printStackTrace();
        }

        mBinding.setVariable(it.integry.integrywmsnative.BR.viewmodel, viewModel);

        container.addView(mBinding.getRoot());
        this.mDatasetViews.add(new AbstractMap.SimpleEntry<>(mBinding, viewModel));

        return mBinding.getRoot();
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((LinearLayout)object);
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
        return view == o;
    }


    @Override
    public int getCount() {
        return mDatasetLayout.size();
    }

    public IDialogBasketLUViewModel getViewModel(@NonNull int layoutID) {
        for(int i = 0; i < mDatasetLayout.size(); i++) {
            if(mDatasetLayout.get(i).getKey() == layoutID) {
                return this.mDatasetViews.get(i).getValue();
            }
        }
        return null;
    }

    private int lastPage = -1;

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        if(position != lastPage) {
            mDatasetViews.get(position).getValue().onShow();
            viewPager.onPageChanged(mDatasetViews.get(position).getKey().getRoot());
            lastPage = position;
        }
    }

    @Override
    public void onPageSelected(int position) {
        // mDatasetViews.get(position).getValue().onShow();
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

}
