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

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.BR;
import it.integry.integrywmsnative.ui.DeactivatableViewPager;
import it.integry.integrywmsnative.view.dialogs.ask_cliente.viewmodel.IDialogAskClienteViewModel;

public class DialogAskClienteAdapter extends PagerAdapter implements ViewPager.OnPageChangeListener {

    private Context mContext;

    private List<Map.Entry<Integer, Class>> mDatasetLayout;
    private List<Map.Entry<ViewDataBinding, IDialogAskClienteViewModel>> mDatasetViews;

    private DeactivatableViewPager viewPager;

    public DialogAskClienteAdapter(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();
        IDialogAskClienteViewModel viewModel = null;

        try {
            viewModel = (IDialogAskClienteViewModel) viewModelClass.newInstance();
            viewModel.setBinding(mBinding);
            viewModel.setContext(mContext);
        } catch (IllegalAccessException | InstantiationException e) {
            e.printStackTrace();
        }

        mBinding.setVariable(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 IDialogAskClienteViewModel 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) {
            if(lastPage != -1) mDatasetViews.get(lastPage).getValue().onDismiss();
            viewPager.onPageChanged(mDatasetViews.get(position).getKey().getRoot());
            mDatasetViews.get(position).getValue().onShow();
            lastPage = position;
        }
    }

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

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    public void onDismiss() {
        if(lastPage != -1) mDatasetViews.get(lastPage).getValue().onDismiss();
    }
}
