/*
 * 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.data.processor.data.item;

import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import org.spongepowered.api.data.DataTransactionResult;
import org.spongepowered.api.data.key.Keys;
import org.spongepowered.api.data.manipulator.immutable.ImmutablePotionColorData;
import org.spongepowered.api.data.manipulator.mutable.PotionColorData;
import org.spongepowered.api.data.value.ValueContainer;
import org.spongepowered.api.data.value.immutable.ImmutableValue;
import org.spongepowered.api.data.value.mutable.Value;
import org.spongepowered.api.util.Color;
import org.spongepowered.common.data.manipulator.mutable.SpongePotionColorData;
import org.spongepowered.common.data.processor.common.AbstractItemSingleDataProcessor;
import org.spongepowered.common.data.value.immutable.ImmutableSpongeValue;
import org.spongepowered.common.data.value.mutable.SpongeValue;
import org.spongepowered.common.util.Constants;

import java.util.Optional;

public class ItemPotionColorDataProcessor extends AbstractItemSingleDataProcessor<Color, Value<Color>, PotionColorData, ImmutablePotionColorData> {

    public ItemPotionColorDataProcessor() {
        super(itemStack -> itemStack.getItem() == Items.POTIONITEM || itemStack.getItem() == Items.SPLASH_POTION ||
                itemStack.getItem() == Items.LINGERING_POTION, Keys.POTION_COLOR);
    }

    @Override
    protected boolean set(ItemStack dataHolder, Color value) {
        if (!dataHolder.hasTagCompound()) {
            dataHolder.setTagCompound(new NBTTagCompound());
        }
        final NBTTagCompound mainCompound = dataHolder.getTagCompound();
        mainCompound.setInteger(Constants.Item.CUSTOM_POTION_COLOR, value.getRgb());
        return true;
    }

    @Override
    protected Optional<Color> getVal(ItemStack dataHolder) {
        if (!dataHolder.hasTagCompound()) {
            return Optional.empty();
        }

        final NBTTagCompound mainCompound = dataHolder.getTagCompound();
        return Optional.of(Color.ofRgb(mainCompound.getInteger(Constants.Item.CUSTOM_POTION_COLOR)));
    }

    @Override
    protected ImmutableValue<Color> constructImmutableValue(Color value) {
        return new ImmutableSpongeValue<>(Keys.POTION_COLOR, value);
    }

    @Override
    protected Value<Color> constructValue(Color actualValue) {
        return new SpongeValue<>(Keys.POTION_COLOR, actualValue);
    }

    @Override
    protected PotionColorData createManipulator() {
        return new SpongePotionColorData();
    }

    @Override
    public DataTransactionResult removeFrom(ValueContainer<?> container) {
        return DataTransactionResult.failNoData();
    }
}

@@ -1412,16 +1412,57 @@
 mpound;%0A
+import net.minecraft.potion.PotionUtils;%0A
 import o
@@ -1510,16 +1510,16 @@
 Result;%0A
-
 import o
@@ -2899,16 +2899,17 @@
       %7D%0A
+%0A
         
@@ -3173,262 +3173,70 @@
     
-if (!dataHolder.hasTagCompound()) %7B%0A            return Optional.empty();%0A        %7D%0A%0A        final NBTTagCompound mainCompound = dataHolder.getTagCompound();%0A        return Optional.of(Color.ofRgb(mainCompound.getInteger(Constants.Item.CUSTOM_POTION_COLOR
+return Optional.of(Color.ofRgb(PotionUtils.getColor(dataHolder
 )));
@@ -3760,24 +3760,64 @@
 ontainer) %7B%0A
+        if (!supports(container)) %7B%0A    
         retu
@@ -3851,20 +3851,688 @@
 ilNoData();%0A
+        %7D%0A%0A        ItemStack itemStack = (ItemStack) container;%0A        if (!itemStack.hasTagCompound()) %7B%0A            return DataTransactionResult.successNoData();%0A        %7D%0A%0A        NBTTagCompound mainCompound = itemStack.getTagCompound();%0A        if (!mainCompound.hasKey(Constants.Item.CUSTOM_POTION_COLOR, Constants.NBT.TAG_INT)) %7B%0A            return DataTransactionResult.successNoData();%0A        %7D%0A%0A        Color removedColor = Color.ofRgb(mainCompound.getInteger(Constants.Item.CUSTOM_POTION_COLOR));%0A        mainCompound.removeTag(Constants.Item.CUSTOM_POTION_COLOR);%0A        return DataTransactionResult.successRemove(constructImmutableValue(removedColor));%0A
     %7D%0A%7D%0A

Return the vanilla color potion if there is no custom color. Also support removing custom color.