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

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

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.SearchView;
import androidx.core.content.ContextCompat;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableList;

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 it.integry.integrywmsnative.BR;
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.DialogAvailableIntventarioItemsItemModelBinding;
import it.integry.integrywmsnative.databinding.DialogChooseArtFromListaArtBinding;
import it.integry.integrywmsnative.ui.SimpleDividerItemDecoration;

public class DialogAvailableItemsView extends BaseDialogFragment {

    private final List<DialogAvailableItemListModel> mListaArts;
    private final ObservableList<DialogAvailableItemListModel> mFilteredListaArts = new ObservableArrayList<>();


    private final RunnableArgs<MtbAart> mOnItemSelected;


    public DialogAvailableItemsView(List<DialogAvailableItemListModel> listaArts, RunnableArgs<MtbAart> onItemSelected) {
        super();
        this.mListaArts = listaArts;
        this.mFilteredListaArts.addAll(mListaArts);

        this.mOnItemSelected = onItemSelected;
    }


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

        binding.titleText.setText(R.string.articoli_disponibili);

        for (var item : mListaArts) {
            if (mOnItemSelected != null) item.setClickable(true);
        }

        initSearchView(binding);
        initRecyclerView(binding);

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

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

    private void initSearchView(DialogChooseArtFromListaArtBinding binding) {
        binding.searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                final List<DialogAvailableItemListModel> matchItems = Stream.of(mListaArts)
                        .filter(x -> x.getCodMart().toLowerCase().contains(newText.toLowerCase())
                                || x.getDescrizioneEstesa().toLowerCase().contains(newText.toLowerCase()))
                        .toList();

                mFilteredListaArts.clear();
                mFilteredListaArts.addAll(matchItems);

                return false;
            }
        });
    }

    private void initRecyclerView(DialogChooseArtFromListaArtBinding binding) {
        binding.itemsList.setHasFixedSize(true);

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

        var itemType = new Type<DialogAvailableItemListModel, DialogAvailableIntventarioItemsItemModelBinding>(R.layout.dialog_available_intventario_items__item_model, BR.item);

        itemType.areItemSame((x, y) -> x.getOriginalModel().getCodMart().equals(y.getOriginalModel().getCodMart()));
        itemType.areContentsTheSame(DialogAvailableItemListModel::equals);

        if (mOnItemSelected != null) {
            itemType.onClick(x -> {
                mOnItemSelected.run(x.getBinding().getItem().getOriginalModel());
                dismiss();
                return null;
            });
        }

        new LiveAdapter(mFilteredListaArts)
                .map(DialogAvailableItemListModel.class, itemType)
                .into(binding.itemsList);

    }

}
