/*
 * This file is part of Sponge, licensed under the MIT License (MIT).
 *
 * Copyright (c) SpongePowered <https://www.spongepowered.org>
 * Copyright (c) contributors
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.spongepowered.common.item.util;

import org.spongepowered.api.data.Key;
import org.spongepowered.api.data.value.Value;
import org.spongepowered.api.item.inventory.ItemStack;
import org.spongepowered.api.item.inventory.ItemStackComparators;
import org.spongepowered.api.registry.RegistryTypes;

import java.util.Comparator;
import java.util.Set;
import java.util.function.Supplier;

public class SpongeItemStackComparatorFactory implements ItemStackComparators.Factory {

    private Comparator<ItemStack> comparator;

    public SpongeItemStackComparatorFactory() {
    }

    public SpongeItemStackComparatorFactory(Comparator<ItemStack> comparator) {
        this.comparator = comparator;
    }

    @Override
    public ItemStackComparators.Factory byType() {
        final Comparator<ItemStack> comparator = Comparator.comparing(i -> i.getType().key(RegistryTypes.ITEM_TYPE));
        return new SpongeItemStackComparatorFactory(this.comparator == null ? comparator : this.comparator.thenComparing(comparator));
    }

    @Override
    public ItemStackComparators.Factory byData() {
        final Comparator<ItemStack> comparator = new ItemDataComparator();
        return new SpongeItemStackComparatorFactory(this.comparator == null ? comparator : this.comparator.thenComparing(comparator));
    }

    @Override
    public ItemStackComparators.Factory byDurability() {

        final Comparator<ItemStack> comparator = new ItemDataComparator();
        return new SpongeItemStackComparatorFactory(this.comparator == null ? comparator : this.comparator.thenComparing(comparator));
    }

    @Override
    public ItemStackComparators.Factory bySize() {
        final Comparator<ItemStack> comparator = Comparator.comparing(ItemStack::getQuantity);
        return new SpongeItemStackComparatorFactory(this.comparator == null ? comparator : this.comparator.thenComparing(this.comparator));
    }

    @Override
    public Supplier<Comparator<ItemStack>> asSupplier() {
        final Comparator<ItemStack> comparator = this.build();
        return () -> comparator;
    }

    @Override
    public Comparator<ItemStack> build() {
        return this.comparator;
    }

    private static final class ItemDataComparator implements Comparator<ItemStack> {

        private final Key<? extends Value<?>>[] ignored;

        ItemDataComparator(Key<? extends Value<?>>... ignored) {
            this.ignored = ignored;
        }

        @Override
        public int compare(ItemStack o1, ItemStack o2) {
            final Set<Value.Immutable<?>> values = o2.getValues();
            for (Value.Immutable<?> value : o1.getValues()) {
                if (values.contains(value)) {
                    values.remove(value);
                } else if (!this.isIgnored(values, value)) {
                    return -1;
                }
            }
            return values.size();
        }

        private boolean isIgnored(Set<Value.Immutable<?>> list, Value.Immutable<?> toCheck) {
            for (Key<? extends Value<?>> ignore : this.ignored) {
                if (toCheck.getKey().equals(ignore)) {
                    list.removeIf(val -> ignore.equals(val.getKey()));
                    return true;
                }
            }
            return false;
        }
    }
}

@@ -1576,16 +1576,42 @@
 arator;%0A
+import java.util.HashSet;%0A
 import j
@@ -3833,16 +3833,30 @@
 alues = 
+new HashSet%3C%3E(
 o2.getVa
@@ -3861,16 +3861,17 @@
 Values()
+)
 ;%0A      

fix comparator