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

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 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.core.model.MtbAart;
import it.integry.integrywmsnative.databinding.DialogChooseArtsFromMtbAartListLayoutBinding;

public class DialogChooseArtsFromMtbAartListView extends BaseDialogFragment implements DialogInterface.OnShowListener {


    @Inject
    DialogChooseArtsFromMtbAartListViewModel mViewModel;

    private DialogChooseArtsFromMtbAartListLayoutBinding mBindings;

    private Context mContext;

    private final List<MtbAart> mtbAarts;
    private List<DialogChooseArtsFromMtbAartListItemModel> artsItemModels = null;
    private final RunnableArgs<List<MtbAart>> onConfirmed;
    private final Runnable onAbort;

    //Pass here all external parameters
    public static DialogChooseArtsFromMtbAartListView newInstance(List<MtbAart> mtbAarts, RunnableArgs<List<MtbAart>> onConfirmed, Runnable onAbort) {
        return new DialogChooseArtsFromMtbAartListView(mtbAarts, onConfirmed, onAbort);
    }

    private DialogChooseArtsFromMtbAartListView(List<MtbAart> mtbAarts, RunnableArgs<List<MtbAart>> onConfirmed, Runnable onAbort) {
        super();

        this.mtbAarts = mtbAarts;
        this.onConfirmed = onConfirmed;
        this.onAbort = onAbort;
    }


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

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

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


        setCancelable(false);

        var alertDialog = new MaterialAlertDialogBuilder(this.mContext)
                .setView(mBindings.getRoot())
                .setCancelable(isCancelable())
                .setPositiveButton(R.string.ok, (dialog, which) -> {
                    var selectedArts = Stream.of(this.artsItemModels)
                            .filter(x -> x.getSelected().get())
                            .map(DialogChooseArtsFromMtbAartListItemModel::getOriginalModel)
                            .toList();

                    if(selectedArts.isEmpty()) {
                        if (this.onAbort != null) this.onAbort.run();
                        return;
                    }

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




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

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

    private void initList() {
        this.artsItemModels = Stream.of(this.mtbAarts)
                .map(x -> new DialogChooseArtsFromMtbAartListItemModel()
                        .setCodMart(x.getCodMart())
                        .setDescrizione(x.getDescrizioneEstesa())
                        .setDiacod(x.getDiacod())
                        .setOriginalModel(x))
                .toList();

        new LiveAdapter(this.artsItemModels, BR.item)
                .map(DialogChooseArtsFromMtbAartListItemModel.class, R.layout.dialog_choose_arts_from_mtb_aart_list__item_model)
                .into(this.mBindings.listaArts);
    }
}