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

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;

import androidx.databinding.DataBindingUtil;

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

import it.integry.integrywmsnative.R;
import it.integry.integrywmsnative.core.expansion.RunnableArgs;
import it.integry.integrywmsnative.core.model.MtbColt;
import it.integry.integrywmsnative.core.utility.UtilityDialog;
import it.integry.integrywmsnative.databinding.DialogBasketLuBinding;
import it.integry.integrywmsnative.view.dialogs.basket_lu.pages.page1.DialogBasketLU_Page1_ViewModel;
import it.integry.integrywmsnative.view.dialogs.basket_lu.pages.page2.DialogBasketLU_Page2_ViewModel;

public class DialogBasketLU {

    private Context mContext;
    private Dialog mDialog;
    private DialogBasketLuBinding mBinding;

    private RunnableArgs<MtbColt> mOnComplete;

    public static Dialog make(final Context context, RunnableArgs<MtbColt> onComplete) {
        return new DialogBasketLU(context, onComplete).mDialog;
    }


    private DialogBasketLU(Context context, RunnableArgs<MtbColt> onComplete) {
        mContext = context;
        mOnComplete = onComplete;

        LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
        mBinding = DataBindingUtil.inflate(inflater, R.layout.dialog_basket_lu, null, false);

        mDialog = new Dialog(context);

        mDialog.setContentView(mBinding.getRoot());

        mDialog.setCanceledOnTouchOutside(false);
        mDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

        UtilityDialog.setTo90PercentWidth(context, mDialog);

        this.initViewPager();
    }

    private void initViewPager() {
        List<Map.Entry<Integer, Class<?>>> views = new ArrayList<>();
        views.add(new AbstractMap.SimpleEntry<>(R.layout.dialog_basket_lu__page1, DialogBasketLU_Page1_ViewModel.class));
        views.add(new AbstractMap.SimpleEntry<>(R.layout.dialog_basket_lu__page2, DialogBasketLU_Page2_ViewModel.class));

        DialogBasketLU_ViewPagerAdapter viewPagerAdapter = new DialogBasketLU_ViewPagerAdapter(mContext, views);
        mBinding.viewpager.setAdapter(viewPagerAdapter);
        mBinding.viewpager.beginFakeDrag();
        mBinding.viewpager.addOnPageChangeListener(viewPagerAdapter);
        mBinding.viewpager.setEnabled(false);


        mDialog.setOnShowListener(v -> {


            viewPagerAdapter.getViewModel(R.layout.dialog_basket_lu__page2).setOnConfirmClickListener(data -> {
                MtbColt mtbColt = (MtbColt) data;

                mOnComplete.run(mtbColt);
                mDialog.dismiss();
            });

        });
    }

}
