package it.integry.integrywmsnative.gest.accettazione_picking.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.utility.UtilityBoolean;
import it.integry.integrywmsnative.databinding.LayoutFilterCodArtBinding;
import it.integry.integrywmsnative.ui.filter_chips.FilterLayoutView;

public class FilterCodArtLayoutView extends FilterLayoutView {

    protected LayoutFilterCodArtBinding mBindings;

    private List<String> all;
    private List<String> available;
    private List<String> preselected = new ArrayList<>();
    private List<String> hidden = new ArrayList<>();
    private final MutableLiveData<List<ListModel>> listModel = new MutableLiveData<>();

    private RunnableArgs<List<String>> onFilterApplied;

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

        refreshList();

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

            refreshList();
        });


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

        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().compareToIgnoreCase(x.getOriginalModel())))
                        .reversed();


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

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

    public List<String> getAll() {
        return all;
    }

    public FilterCodArtLayoutView setAll(List<String> all) {
        this.all = all;
        return this;
    }

    public List<String> getAvailable() {
        return available;
    }

    public FilterCodArtLayoutView setAvailable(List<String> available) {
        this.available = available;
        return this;
    }

    public List<String> getPreselected() {
        return preselected;
    }

    public FilterCodArtLayoutView setPreselected(List<String> preselected) {
        this.preselected = preselected;
        return this;
    }

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

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

    public List<String> getHidden() {
        return hidden;
    }

    public FilterCodArtLayoutView setHidden(List<String> hidden) {
        this.hidden = hidden;
        return this;
    }

    public static class ListModel {
        private BindableBoolean selected = new BindableBoolean();
        private BindableBoolean enabled = new BindableBoolean();
        private String 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 String getOriginalModel() {
            return originalModel;
        }

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

}
