package it.integry.integrywmsnative.gest.pv_ordine_acquisto_edit.dialog.selectArtFromList;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;

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

import com.annimon.stream.Stream;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.ravikoradiya.liveadapter.LiveAdapter;
import com.ravikoradiya.liveadapter.Type;

import java.util.List;

import javax.inject.Inject;

import it.integry.integrywmsnative.BR;
import it.integry.integrywmsnative.MainApplication;
import it.integry.integrywmsnative.R;
import it.integry.integrywmsnative.core.expansion.BaseDialogFragment;
import it.integry.integrywmsnative.core.expansion.RunnableArgs;
import it.integry.integrywmsnative.databinding.DialogSelectArtToOrderBinding;
import it.integry.integrywmsnative.databinding.DialogSelectArtToOrderItemModelBinding;

/**
 * @noinspection rawtypes
 */
public class DialogSelectArtToOrderView extends BaseDialogFragment {


    @Inject
    DialogSelectArtToOrderViewModel mViewModel;

    private DialogSelectArtToOrderBinding mBindings;
    private Context mContext;

    private final List<DialogSelectArtToOrderItemListModel<Object>> listArts;
    private final RunnableArgs<DialogSelectArtToOrderItemListModel> onConfirm;
    private final Runnable onAbort;

    //Pass here all external parameters
    public static DialogSelectArtToOrderView newInstance(List<DialogSelectArtToOrderItemListModel<Object>> listArts,
                                                         RunnableArgs<DialogSelectArtToOrderItemListModel> onConfirm,
                                                         Runnable onAbort) {
        return new DialogSelectArtToOrderView(listArts, onConfirm, onAbort);
    }

    private DialogSelectArtToOrderView(List<DialogSelectArtToOrderItemListModel<Object>> listArts,
                                       RunnableArgs<DialogSelectArtToOrderItemListModel> onConfirm,
                                       Runnable onAbort) {
        super();

        this.listArts = listArts;
        this.onConfirm = onConfirm;
        this.onAbort = onAbort;
    }


    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        this.mContext = requireContext();

        mBindings = DialogSelectArtToOrderBinding.inflate(LayoutInflater.from(this.mContext), null, false);
        mBindings.setLifecycleOwner(this);

        MainApplication.appComponent
                .dialogSelectArtToOrderNewComponent()
                .create()
                .inject(this);

        setCancelable(false);

        var alertDialog = new MaterialAlertDialogBuilder(this.mContext)
                .setView(mBindings.getRoot())
                .setCancelable(isCancelable())
                .setPositiveButton(R.string.confirm, (dialog, which) -> {
                    final DialogSelectArtToOrderItemListModel<Object> selectedItem = Stream.of(listArts)
                            .filter(x -> x.getChecked().get())
                            .findFirstOrElse(null);

                    if (selectedItem != null)
                        this.onConfirm.run(selectedItem);
                })
                .setNegativeButton(R.string.abort, (dialog, which) -> {
                    if (this.onAbort != null) this.onAbort.run();
                })
                .create();

        alertDialog.setCanceledOnTouchOutside(isCancelable());
        alertDialog.setOnShowListener(this);
        return alertDialog;
    }

    @Override
    public void onShow(DialogInterface dialogInterface) {
        super.onShow(dialogInterface);

        this.listArts.get(0).getChecked().set(true);
        this.initRecyclerView();
    }


    private void initRecyclerView() {
        var itemType = new Type<DialogSelectArtToOrderItemListModel, DialogSelectArtToOrderItemModelBinding>(R.layout.dialog_select_art_to_order__item_model, BR.row);
//        itemType.areItemSame((oldItem, newItem) -> Objects.equals(oldItem.getId(), newItem.getId()));
        itemType.areContentsTheSame(DialogSelectArtToOrderItemListModel::equals);
        itemType.onClick(x -> {
            resetChecks();
            x.getBinding().getRow().getChecked().set(true);

            return null;
        });

        new LiveAdapter(listArts)
                .map(DialogSelectArtToOrderItemListModel.class, itemType)
                .into(this.mBindings.itemList);
    }

    private void resetChecks() {
        for (var item : listArts) {
            item.getChecked().set(false);
        }
    }

}