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

import android.app.Dialog;
import android.os.Bundle;
import android.view.LayoutInflater;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.google.android.material.dialog.MaterialAlertDialogBuilder;

import java.util.List;

import it.integry.integrywmsnative.R;
import it.integry.integrywmsnative.core.expansion.BaseDialogFragment;
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;
import it.integry.integrywmsnative.ui.SimpleDividerItemDecoration;

public class DialogChooseArtFromListaArtsView extends BaseDialogFragment {


    private final boolean mEnableSelection;
    private final List<MtbAart> mListaArts;
    private final RunnableArgs<MtbAart> mOnItemChoosed;


    public DialogChooseArtFromListaArtsView(boolean enableSelection, List<MtbAart> listaArts, RunnableArgs<MtbAart> onItemChoosed) {
        super();

        this.mEnableSelection = enableSelection;
        this.mListaArts = listaArts;
        this.mOnItemChoosed = onItemChoosed;
    }


    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        var binding = DialogChooseArtFromListaArtBinding.inflate(LayoutInflater.from(requireContext()));

        binding.titleText.setText(this.mEnableSelection ? R.string.dialog_choose_art_from_lista_art :
                R.string.articoli_disponibili);

        initRecyclerView(binding, mListaArts);

        var alertDialog = new MaterialAlertDialogBuilder(requireContext())
                .setView(binding.getRoot())
                .setCancelable(false)
                .create();

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

    private void initRecyclerView(DialogChooseArtFromListaArtBinding binding, List<MtbAart> dataset) {
        binding.itemsList.setNestedScrollingEnabled(false);
        binding.itemsList.setHasFixedSize(true);
        binding.itemsList.setLayoutManager(new LinearLayoutManager(requireContext()));

        SimpleDividerItemDecoration itemDecorator = new SimpleDividerItemDecoration(requireContext(), SimpleDividerItemDecoration.VERTICAL);
        itemDecorator.setDrawable(ContextCompat.getDrawable(requireContext(), R.drawable.divider));
        binding.itemsList.addItemDecoration(itemDecorator);

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

            dismiss();
        };

        DialogChooseArtFromListaArtsAdapter mCurrentAdapter = new DialogChooseArtFromListaArtsAdapter(
                requireContext(), dataset, this.mEnableSelection ? onItemClickListener : null);
        binding.itemsList.setAdapter(mCurrentAdapter);
    }

}
