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

import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.FragmentActivity;

import org.jetbrains.annotations.NotNull;

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.RunnableArgss;
import it.integry.integrywmsnative.core.model.VtbDest;
import it.integry.integrywmsnative.core.utility.UtilityDialog;
import it.integry.integrywmsnative.core.utility.UtilityString;
import it.integry.integrywmsnative.databinding.DialogAskClienteBinding;
import it.integry.integrywmsnative.view.dialogs.ask_cliente.dto.DialogAskClienteDestinatarioDTO;
import it.integry.integrywmsnative.view.dialogs.ask_cliente.viewmodel.DialogAskCliente_Page1ViewModel;
import it.integry.integrywmsnative.view.dialogs.ask_cliente.viewmodel.DialogAskCliente_Page2ViewModel;

public class DialogAskClienteView extends BaseDialogFragment {

    private final RunnableArgss<VtbDest, String> mOnComplete;
    private final Runnable mOnAbort;

    private DialogAskClienteAdapter mAdapter;

    private FragmentActivity mContext;
    private DialogAskClienteBinding mBindings;

    private boolean mIsClienteSelected = false;

    public static DialogAskClienteView newInstance(@NotNull RunnableArgss<VtbDest, String> onComplete, @NotNull Runnable onAbort) {
        return new DialogAskClienteView( onComplete, onAbort);
    }

    private DialogAskClienteView(@NotNull RunnableArgss<VtbDest, String> onComplete, @NotNull Runnable onAbort) {
        super();

        this.mOnComplete = onComplete;
        this.mOnAbort = onAbort;
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        this.mContext = getActivity();

        mBindings = DataBindingUtil.inflate(inflater, R.layout.dialog_ask_cliente, container, false);
        mBindings.setLifecycleOwner(this);

        getDialog().setCanceledOnTouchOutside(false);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);


        List<Map.Entry<Integer, Class>> views = new ArrayList<>();
        views.add(new AbstractMap.SimpleEntry<>(R.layout.dialog_ask_cliente__page1, DialogAskCliente_Page1ViewModel.class));
        views.add(new AbstractMap.SimpleEntry<>(R.layout.dialog_ask_cliente__page2, DialogAskCliente_Page2ViewModel.class));

        mAdapter = new DialogAskClienteAdapter(mContext, views);
        mBindings.viewpager.setAdapter(mAdapter);
        mBindings.viewpager.beginFakeDrag();
        mBindings.viewpager.addOnPageChangeListener(mAdapter);
        mBindings.viewpager.setCurrentItem(0, false);

        getDialog().setOnShowListener(d -> {
            this.initView();
        });

        return mBindings.getRoot();
    }

    @Override
    public void onStart() {
        super.onStart();
        UtilityDialog.setTo90PercentWidth(this.mContext, this);
    }


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    private void initView() {
        DialogAskCliente_Page1ViewModel viewModel1 = (DialogAskCliente_Page1ViewModel) mAdapter.getViewModel(R.layout.dialog_ask_cliente__page1);
        DialogAskCliente_Page2ViewModel viewModel2 = (DialogAskCliente_Page2ViewModel) mAdapter.getViewModel(R.layout.dialog_ask_cliente__page2);

        viewModel1
                .setOnConfirmClickListener(() -> {
                    String codAnag = viewModel1.getCurrentCliente();

                    if(UtilityString.isNullOrEmpty(codAnag)) {
                        this.mIsClienteSelected = true;
                        dismiss();
                        mOnComplete.run(null, null);
                    }

                    viewModel2.setCodAnag(codAnag);

                    mBindings.viewpager.setCurrentItem(mBindings.viewpager.getCurrentItem() + 1, true);
                });

        viewModel2.setOnConfirmClickListener(() -> {
            DialogAskClienteDestinatarioDTO cliente = viewModel2.getCurrentDestinatario();

            this.mIsClienteSelected = true;
            dismiss();
            mOnComplete.run(cliente != null ? cliente.toVtbDestModel() : null, viewModel1.getCurrentCommessa());
        });


        viewModel1.setOnAbortClickListener(this::dismiss);
        viewModel2.setOnAbortClickListener(this::dismiss);
    }


    @Override
    public void onDismiss(@NonNull DialogInterface dialog) {
        mAdapter.onDismiss();
        if(!mIsClienteSelected) {
            this.mOnAbort.run();
        }
        super.onDismiss(dialog);
    }

}
