/*
 * 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.mixin.core.block;

import com.google.common.collect.ImmutableList;
import net.minecraft.block.BlockHopper;
import net.minecraft.block.state.IBlockState;
import org.spongepowered.api.block.BlockState;
import org.spongepowered.api.data.key.Key;
import org.spongepowered.api.data.key.Keys;
import org.spongepowered.api.data.manipulator.ImmutableDataManipulator;
import org.spongepowered.api.data.manipulator.immutable.block.ImmutableDirectionalData;
import org.spongepowered.api.data.value.BaseValue;
import org.spongepowered.api.util.Direction;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.common.data.ImmutableDataCachingUtil;
import org.spongepowered.common.data.manipulator.immutable.block.ImmutableSpongeDirectionalData;
import org.spongepowered.common.util.Constants;

import java.util.Optional;

@Mixin(BlockHopper.class)
public abstract class BlockHopperMixin extends BlockMixin {

    @SuppressWarnings("RedundantTypeArguments") // some java compilers will not calculate this generic correctly
    @Override
    public ImmutableList<ImmutableDataManipulator<?, ?>> bridge$getManipulators(final IBlockState blockState) {
        return ImmutableList.<ImmutableDataManipulator<?, ?>>of(impl$getDirectionalData(blockState));
    }

    @Override
    public boolean bridge$supports(final Class<? extends ImmutableDataManipulator<?, ?>> immutable) {
        return ImmutableDirectionalData.class.isAssignableFrom(immutable);
    }

    @Override
    public Optional<BlockState> bridge$getStateWithData(final IBlockState blockState, final ImmutableDataManipulator<?, ?> manipulator) {
        if (manipulator instanceof ImmutableDirectionalData) {
            final Direction dir = Constants.DirectionFunctions.checkDirectionNotUp(((ImmutableDirectionalData) manipulator).direction().get());
            return Optional.of((BlockState) blockState.withProperty(BlockHopper.FACING, Constants.DirectionFunctions.getFor(dir)));
        }
        return super.bridge$getStateWithData(blockState, manipulator);
    }

    @Override
    public <E> Optional<BlockState> bridge$getStateWithValue(final IBlockState blockState, final Key<? extends BaseValue<E>> key, final E value) {
        if (key.equals(Keys.DIRECTION)) {
            final Direction dir = Constants.DirectionFunctions.checkDirectionNotUp((Direction) value);
            return Optional.of((BlockState) blockState.withProperty(BlockHopper.FACING, Constants.DirectionFunctions.getFor(dir)));
        }
        return super.bridge$getStateWithValue(blockState, key, value);
    }

    private ImmutableDirectionalData impl$getDirectionalData(final IBlockState blockState) {
        return ImmutableDataCachingUtil.getManipulator(ImmutableSpongeDirectionalData.class,
                Constants.DirectionFunctions.getFor(blockState.getValue(BlockHopper.FACING)));
    }
}

@@ -1713,32 +1713,116 @@
 irectionalData;%0A
+import org.spongepowered.api.data.manipulator.immutable.block.ImmutablePoweredData;%0A
 import org.spong
@@ -1856,16 +1856,16 @@
 eValue;%0A
-
 import o
@@ -2095,32 +2095,125 @@
 irectionalData;%0A
+import org.spongepowered.common.data.manipulator.immutable.block.ImmutableSpongePoweredData;%0A
 import org.spong
@@ -2663,16 +2663,29 @@
 , ?%3E%3Eof(
+%0A            
 impl$get
@@ -2711,16 +2711,70 @@
 ckState)
+,%0A            impl$getPoweredData(blockState)%0A        
 );%0A    %7D
@@ -3992,32 +3992,190 @@
 r)));%0A        %7D%0A
+        if (key.equals(Keys.POWERED)) %7B%0A            return Optional.of((BlockState) blockState.withProperty(BlockHopper.ENABLED, (Boolean) value));%0A        %7D%0A
         return s
@@ -4422,16 +4422,16 @@
 .class,%0A
-
         
@@ -4522,11 +4522,247 @@
 );%0A    %7D
+%0A%0A    private ImmutablePoweredData impl$getPoweredData(final IBlockState blockState) %7B%0A        return ImmutableDataCachingUtil.getManipulator(ImmutableSpongePoweredData.class,%0A            blockState.getValue(BlockHopper.ENABLED));%0A    %7D
 %0A%7D%0A

Enable toggling a Hopper's blockstate to enabled/disabled