/*
 * Copyright 2017 ZeroMemes
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package me.zero.client.load.mixin;

import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;
import me.zero.client.api.ClientAPI;
import me.zero.client.api.event.defaults.PacketEvent;
import net.minecraft.network.INetHandler;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.Packet;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.*;

import javax.annotation.Nullable;

/**
 * @author Brady
 * @since 4/27/2017 12:00 PM
 */
@Mixin(NetworkManager.class)
public abstract class MixinNetworkManager {

    @Shadow public abstract void dispatchPacket(final Packet<?> inPacket, @Nullable final GenericFutureListener<? extends Future<?super Void>>[] futureListeners);

    @Redirect(method = "channelRead0", at = @At(value = "INVOKE", target = "net/minecraft/network/Packet.processPacket(Lnet/minecraft/network/INetHandler;)V"))
    @SuppressWarnings("unchecked")
    public void processPacket(Packet<?> packetIn, INetHandler handler) {
        PacketEvent event = new PacketEvent.Send(packetIn);
        ClientAPI.EVENT_BUS.post(event);
        if (event.isCancelled())
            return;

        ((Packet<INetHandler>) event.getPacket()).processPacket(handler);
    }

    @Redirect(method = "sendPacket", at = @At(value = "INVOKE", target = "net/minecraft/network/NetworkManager.dispatchPacket(Lnet/minecraft/network/Packet;[Lio/netty/util/concurrent/GenericFutureListener;)V"))
    public void sendPacket(NetworkManager networkManager, Packet<?> packetIn, @Nullable final GenericFutureListener<? extends Future<?super Void>>[] futureListeners) {
        PacketEvent event = new PacketEvent.Receive(packetIn);
        ClientAPI.EVENT_BUS.post(event);
        if (event.isCancelled())
            return;

        this.dispatchPacket(event.getPacket(), null);
    }
}

@@ -1707,12 +1707,15 @@
 ent.
-Send
+Receive
 (pac
@@ -2313,39 +2313,36 @@
 new PacketEvent.
-Receive
+Send
 (packetIn);%0A    

Fix packet events