package it.integry.integrywmsnative.gest.ordini_uscita_elenco.filters;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.MutableLiveData;

import com.annimon.stream.ComparatorCompat;
import com.annimon.stream.Stream;
import com.ravikoradiya.liveadapter.LiveAdapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Objects;

import it.integry.integrywmsnative.BR;
import it.integry.integrywmsnative.R;
import it.integry.integrywmsnative.core.di.BindableBoolean;
import it.integry.integrywmsnative.core.expansion.RunnableArgs;
import it.integry.integrywmsnative.core.model.MtbDepo;
import it.integry.integrywmsnative.core.utility.UtilityBoolean;
import it.integry.integrywmsnative.databinding.LayoutFilterDepositoBinding;
import it.integry.integrywmsnative.ui.filter_chips.FilterLayoutView;

public class FilterDepositoLayoutView extends FilterLayoutView {

    protected LayoutFilterDepositoBinding mBindings;

    private List<MtbDepo> allCodMdeps;
    private List<MtbDepo> availableCodMdeps;
    private List<MtbDepo> preSelectedCodMdeps = new ArrayList<>();
    private List<MtbDepo> hiddenDepos = new ArrayList<>();
    private final MutableLiveData<List<ListModel>> listModel = new MutableLiveData<>();

    private RunnableArgs<List<MtbDepo>> onFilterApplied;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBindings = DataBindingUtil.inflate(inflater, R.layout.layout_filter_deposito, container, false);
        this.mBindings.setView(this);

        refreshList();

        this.setSearchView(this.mBindings.searchView, newFilter -> {
            hiddenDepos = Stream.of(allCodMdeps)
                    .filter(x -> !x.getCodMdep().toUpperCase(Locale.ROOT).contains(newFilter.toUpperCase(Locale.ROOT)) &&
                            !x.getDescrizione().toUpperCase(Locale.ROOT).contains(newFilter.toUpperCase(Locale.ROOT)))
                    .toList();

            refreshList();
        });


        new LiveAdapter(listModel, getViewLifecycleOwner(), BR.item)
                .map(ListModel.class, R.layout.layout_filter_deposito__list_item)
                .into(this.mBindings.recyclerviewDepositi);

        return mBindings.getRoot();
    }


    public void onConfirm() {
        if (onFilterApplied == null) return;

        this.onFilterApplied.run(Stream.of(Objects.requireNonNull(listModel.getValue()))
                .filter(x -> x.selected.get())
                .map(x -> x.originalModel)
                .toList());

        dismiss();
    }

    private void refreshList() {
        ComparatorCompat<ListModel> c =
                ComparatorCompat
                        .chain(new ComparatorCompat<ListModel>((x, y) -> UtilityBoolean.toInt(x.getSelected().get()) - UtilityBoolean.toInt(y.getSelected().get())))
                        .thenComparing(new ComparatorCompat<>((x, y) -> UtilityBoolean.toInt(x.getEnabled().get()) - UtilityBoolean.toInt(y.getEnabled().get())))
                        .thenComparing(new ComparatorCompat<>((x, y) -> y.getOriginalModel().getCodMdep().compareToIgnoreCase(x.getOriginalModel().getCodMdep())))
                        .reversed();


        listModel.setValue(Stream.of(getAllCodMdeps())
                .filter(x -> !hiddenDepos.contains(x))
                .map(x -> {
                    var selectedBoolean = new BindableBoolean(preSelectedCodMdeps.contains(x));
                    selectedBoolean.addOnPropertyChangedCallback(() -> {
                        if (selectedBoolean.get()) preSelectedCodMdeps.add(x);
                        else preSelectedCodMdeps.remove(x);
                    });

                    return new ListModel()
                            .setSelected(selectedBoolean)
                            .setEnabled(new BindableBoolean(availableCodMdeps.contains(x)))
                            .setOriginalModel(x);
                })
                .sorted(c)
                .toList());
    }

    public List<MtbDepo> getAllCodMdeps() {
        return allCodMdeps;
    }

    public FilterDepositoLayoutView setAllCodMdeps(List<MtbDepo> allCodMdeps) {
        this.allCodMdeps = allCodMdeps;
        return this;
    }

    public List<MtbDepo> getAvailableCodMdeps() {
        return availableCodMdeps;
    }

    public FilterDepositoLayoutView setAvailableCodMdeps(List<MtbDepo> availableCodMdeps) {
        this.availableCodMdeps = availableCodMdeps;
        return this;
    }

    public List<MtbDepo> getPreSelectedCodMdeps() {
        return preSelectedCodMdeps;
    }

    public FilterDepositoLayoutView setPreSelectedCodMdeps(List<MtbDepo> preSelectedCodMdeps) {
        this.preSelectedCodMdeps = preSelectedCodMdeps;
        return this;
    }

    public RunnableArgs<List<MtbDepo>> getOnFilterApplied() {
        return onFilterApplied;
    }

    public FilterDepositoLayoutView setOnFilterApplied(RunnableArgs<List<MtbDepo>> onFilterApplied) {
        this.onFilterApplied = onFilterApplied;
        return this;
    }

    public static class ListModel {
        private BindableBoolean selected = new BindableBoolean();
        private BindableBoolean enabled = new BindableBoolean();
        private MtbDepo originalModel;

        public BindableBoolean getSelected() {
            return selected;
        }

        public ListModel setSelected(BindableBoolean selected) {
            this.selected = selected;
            return this;
        }

        public BindableBoolean getEnabled() {
            return enabled;
        }

        public ListModel setEnabled(BindableBoolean enabled) {
            this.enabled = enabled;
            return this;
        }

        public MtbDepo getOriginalModel() {
            return originalModel;
        }

        public ListModel setOriginalModel(MtbDepo originalModel) {
            this.originalModel = originalModel;
            return this;
        }
    }
}
