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

import android.content.Context;

import androidx.databinding.DataBindingUtil;
import androidx.core.content.ContextCompat;
import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.LayoutInflater;

import java.util.List;

import it.integry.integrywmsnative.R;
import it.integry.integrywmsnative.core.expansion.RunnableArgs;
import it.integry.integrywmsnative.core.model.MtbColr;
import it.integry.integrywmsnative.core.utility.UtilityString;
import it.integry.integrywmsnative.databinding.DialogChooseArtsFromListaArtsLayoutBinding;

public class DialogChooseArtsFromListaArts {

    private Context currentContext;
    private AlertDialog currentAlert;

    private DialogChooseArtsFromListaArtsAdapter currentAdapter;

    private RunnableArgs<List<MtbColr>> mOnItemsChoosed;
    private Runnable mOnAbort;

    public static AlertDialog make(final Context context, List<MtbColr> listaMtbColr, RunnableArgs<List<MtbColr>> onItemsChoosed, Runnable onAbort) {
        return new DialogChooseArtsFromListaArts(context, listaMtbColr, onItemsChoosed, onAbort).currentAlert;
    }


    private DialogChooseArtsFromListaArts(Context context, List<MtbColr> listaMtbColr, RunnableArgs<List<MtbColr>> onItemsChoosed, Runnable onAbort) {
        currentContext = context;
        mOnItemsChoosed = onItemsChoosed;
        mOnAbort = onAbort;

        if(listaMtbColr != null){
            for (MtbColr mtbColr : listaMtbColr) {
                if(UtilityString.isNullOrEmpty(mtbColr.getUntMis()) && mtbColr.getMtbAart() != null){
                    mtbColr.setUntMis(mtbColr.getMtbAart().getUntMis());
                }
            }
        }

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        DialogChooseArtsFromListaArtsLayoutBinding binding = DataBindingUtil.inflate(inflater, R.layout.dialog_choose_arts_from_lista_arts_layout, null, false);


        final AlertDialog.Builder alertDialog = new AlertDialog.Builder(context)
                .setView(binding.getRoot())
                .setPositiveButton(context.getText(R.string.confirm), (dialogInterface, i) -> {
                    onPositiveClick();
                })
                .setNegativeButton(context.getText(R.string.abort), ((dialogInterface, i) -> {
                    onNegativeClick();
                }));

        currentAlert = alertDialog.create();
        currentAlert.setCanceledOnTouchOutside(false);

        initRecyclerView(binding, listaMtbColr);
    }

    private void initRecyclerView(DialogChooseArtsFromListaArtsLayoutBinding binding, List<MtbColr> dataset) {
        binding.dialogChooseArtsFromListaArtMainList.setNestedScrollingEnabled(false);

        binding.dialogChooseArtsFromListaArtMainList.setHasFixedSize(true);

        binding.dialogChooseArtsFromListaArtMainList.setLayoutManager(new LinearLayoutManager(currentContext));

        DividerItemDecoration itemDecorator = new DividerItemDecoration(currentContext, DividerItemDecoration.VERTICAL);
        itemDecorator.setDrawable(ContextCompat.getDrawable(currentContext, R.drawable.divider));
        binding.dialogChooseArtsFromListaArtMainList.addItemDecoration(itemDecorator);

        currentAdapter = new DialogChooseArtsFromListaArtsAdapter(currentContext, dataset);
        binding.dialogChooseArtsFromListaArtMainList.setAdapter(currentAdapter);
    }


    private void onPositiveClick() {

        if(mOnItemsChoosed != null) {
            mOnItemsChoosed.run(currentAdapter.getSelectedItems());
        }

    }


    private void onNegativeClick() {

        if(this.mOnAbort != null) mOnAbort.run();

    }



}
