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

import android.app.Dialog;
import android.os.Bundle;
import android.view.LayoutInflater;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.google.android.material.dialog.MaterialAlertDialogBuilder;

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.BaseDialogFragment;
import it.integry.integrywmsnative.core.expansion.RunnableArgs;
import it.integry.integrywmsnative.core.model.MtbColt;
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 DialogBasketLUView extends BaseDialogFragment {

    private final RunnableArgs<MtbColt> mOnComplete;
    private DialogBasketLuBinding mBindings;



    public DialogBasketLUView(RunnableArgs<MtbColt> onComplete) {
        mOnComplete = onComplete;
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        mBindings = DialogBasketLuBinding.inflate(LayoutInflater.from(requireContext()), null, false);

        var alertDialog =  new MaterialAlertDialogBuilder(requireContext())
                .setView(mBindings.getRoot())
                .setCancelable(false)
                .create();

        alertDialog.setCanceledOnTouchOutside(false);
        this.initViewPager(alertDialog);

        return alertDialog;
    }

    private void initViewPager(Dialog dialog) {
        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(requireContext(), views);
        mBindings.viewpager.setAdapter(viewPagerAdapter);
        mBindings.viewpager.beginFakeDrag();
        mBindings.viewpager.addOnPageChangeListener(viewPagerAdapter);
        mBindings.viewpager.setEnabled(false);


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

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

        });
    }

}
