package it.integry.integrywmsnative.view.dialogs.choose_art_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.interfaces.IRecyclerItemClicked;
import it.integry.integrywmsnative.core.model.MtbAart;
import it.integry.integrywmsnative.databinding.DialogChooseArtFromListaArtBinding;

public class DialogChooseArtFromListaArts {


    private Context currentContext;
    private AlertDialog currentAlert;

    private DialogChooseArtFromListaArtsAdapter currentAdapter;

    private RunnableArgs<MtbAart> mOnItemChoosed;

    public static AlertDialog make(final Context context, List<MtbAart> listaArts, RunnableArgs<MtbAart> onItemChoosed) {
        return new DialogChooseArtFromListaArts(context, listaArts, onItemChoosed).currentAlert;
    }


    private DialogChooseArtFromListaArts(Context context, List<MtbAart> listaArts, RunnableArgs<MtbAart> onItemChoosed) {
        currentContext = context;
        mOnItemChoosed = onItemChoosed;

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


        final AlertDialog.Builder alertDialog = new AlertDialog.Builder(context)
                .setView(binding.getRoot())
                .setPositiveButton(context.getText(R.string.confirm), null)
                .setNegativeButton(context.getText(R.string.abort), null);

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

        initRecyclerView(binding, listaArts);
    }

    private void initRecyclerView(DialogChooseArtFromListaArtBinding binding, List<MtbAart> dataset) {
        binding.dialogChooseArtFromListaArtMainList.setNestedScrollingEnabled(false);

        binding.dialogChooseArtFromListaArtMainList.setHasFixedSize(true);

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

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

        currentAdapter = new DialogChooseArtFromListaArtsAdapter(currentContext, dataset, onItemClicked);
        binding.dialogChooseArtFromListaArtMainList.setAdapter(currentAdapter);
    }

    private IRecyclerItemClicked<MtbAart> onItemClicked = (item, position) -> {
        if(mOnItemChoosed != null) {
            mOnItemChoosed.run(item);
        }

        currentAlert.dismiss();
    };

}
