/*
 * 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.entity;

import com.google.common.collect.ImmutableList;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.projectile.EntityPotion;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
import org.spongepowered.api.data.DataTransactionResult;
import org.spongepowered.api.data.key.Keys;
import org.spongepowered.api.data.manipulator.immutable.ImmutablePotionEffectData;
import org.spongepowered.api.data.manipulator.mutable.PotionEffectData;
import org.spongepowered.api.data.value.ValueContainer;
import org.spongepowered.api.data.value.immutable.ImmutableValue;
import org.spongepowered.api.data.value.mutable.ListValue;
import org.spongepowered.api.effect.potion.PotionEffect;
import org.spongepowered.common.data.manipulator.mutable.SpongePotionEffectData;
import org.spongepowered.common.data.processor.common.AbstractSingleDataSingleTargetProcessor;
import org.spongepowered.common.data.value.immutable.ImmutableSpongeListValue;
import org.spongepowered.common.data.value.mutable.SpongeListValue;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;

public class PotionEntityPotionDataProcessor extends AbstractSingleDataSingleTargetProcessor<EntityPotion, List<PotionEffect>,
        ListValue<PotionEffect>, PotionEffectData, ImmutablePotionEffectData> {

    public PotionEntityPotionDataProcessor() {
        super(Keys.POTION_EFFECTS, EntityPotion.class);
    }

    @Override
    protected boolean set(EntityPotion dataHolder, List<PotionEffect> value) {
        return false;
    }

    @Override
    protected Optional<List<PotionEffect>> getVal(EntityPotion dataHolder) {
        ItemStack potionItem = dataHolder.potionDamage;
        if (potionItem == null) {
            return Optional.empty();
        }
        Collection<net.minecraft.potion.PotionEffect> effects = Items.potionitem.getEffects(potionItem);
        if (effects == null || effects.isEmpty()) {
            return Optional.empty();
        }
        List<PotionEffect> apiEffects = new ArrayList<>();
        for (net.minecraft.potion.PotionEffect potionEffect : effects) {
            apiEffects.add(((PotionEffect) potionEffect));
        }
        return Optional.of(apiEffects);
    }

    @Override
    protected ImmutableValue<List<PotionEffect>> constructImmutableValue(List<PotionEffect> value) {
        return new ImmutableSpongeListValue<>(Keys.POTION_EFFECTS, ImmutableList.copyOf(value));
    }

    @Override
    protected ListValue<PotionEffect> constructValue(List<PotionEffect> actualValue) {
        return new SpongeListValue<>(Keys.POTION_EFFECTS, actualValue);
    }

    @Override
    protected PotionEffectData createManipulator() {
        return new SpongePotionEffectData();
    }

    @Override
    public DataTransactionResult removeFrom(ValueContainer<?> container) {
        if (!(container instanceof EntityPotion)) {
            return DataTransactionResult.failNoData();
        }
        Optional<List<PotionEffect>> effects = getVal((EntityPotion) container);
        if (effects.isPresent()) {
            ((EntityPotion) container).potionDamage = new ItemStack(Items.potionitem, 1, 0);
            return DataTransactionResult.successRemove(constructImmutableValue(effects.get()));
        }
        return DataTransactionResult.successNoData();
    }
}

@@ -1520,16 +1520,57 @@
 mStack;%0A
+import net.minecraft.potion.PotionUtils;%0A
 import o
@@ -1618,16 +1618,16 @@
 Result;%0A
-
 import o
@@ -3060,28 +3060,31 @@
 aHolder.
-potionDamage
+func_184543_l()
 ;%0A      
@@ -3222,32 +3222,27 @@
 fects = 
-Items.potionitem
+PotionUtils
 .getEffe
@@ -3244,16 +3244,25 @@
 tEffects
+FromStack
 (potionI
@@ -4497,23 +4497,22 @@
 er).
-potionDamage = 
+func_184541_a(
 new 
@@ -4544,16 +4544,17 @@
 m, 1, 0)
+)
 ;%0A      

Fix potion entity data processor