package it.integry.integrywmsnative.gest.contab_doc_interni.edit_form.dialog.selectDocRows;

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.DialogSelectDocRowsBinding;
import it.integry.integrywmsnative.databinding.DialogSelectDocRowsItemModelBinding;

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


    @Inject
    DialogSelectDocRowsViewModel mViewModel;

    private DialogSelectDocRowsBinding mBindings;
    private Context mContext;

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

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

    private DialogSelectDocRowsView(List<DialogSelectDocRowsItemListModel<Object>> listArts,
                                    RunnableArgs<DialogSelectDocRowsItemListModel> 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 = DialogSelectDocRowsBinding.inflate(LayoutInflater.from(this.mContext), null, false);
        mBindings.setLifecycleOwner(this);

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

        setCancelable(false);

        var alertDialog = new MaterialAlertDialogBuilder(this.mContext)
                .setView(mBindings.getRoot())
                .setCancelable(isCancelable())
                .setPositiveButton(R.string.confirm, (dialog, which) -> {
                    final DialogSelectDocRowsItemListModel<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<DialogSelectDocRowsItemListModel, DialogSelectDocRowsItemModelBinding>(R.layout.dialog_select_doc_rows__item_model, BR.row);
//        itemType.areItemSame((oldItem, newItem) -> Objects.equals(oldItem.getId(), newItem.getId()));
        itemType.areContentsTheSame(DialogSelectDocRowsItemListModel::equals);
        itemType.onClick(x -> {
            resetChecks();
            x.getBinding().getRow().getChecked().set(true);

            return null;
        });

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

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

}