package common.packet;

import java.io.IOException;

import common.network.IClientPlayer;
import common.network.Packet;
import common.network.PacketBuffer;
import common.potion.Potion;
import common.potion.PotionEffect;

public class SPacketEntityEffect implements Packet<IClientPlayer>
{
    private int entityId;
    private Potion effectId;
    private int amplifier;
    private int duration;
    private int remaining;
    private boolean particles;

    public SPacketEntityEffect()
    {
    }

    public SPacketEntityEffect(int entityIdIn, PotionEffect effect)
    {
        this.entityId = entityIdIn;
        this.effectId = effect.getPotion();
        this.amplifier = /* (byte)(*/ effect.getAmplifier(); // & 255);

//        if (effect.getDuration() > 32767)
//        {
//            this.duration = 32767;
//        }
//        else
//        {
        this.duration = effect.getDuration();
        this.remaining = effect.getRemaining();
//        }

        this.particles = effect.getIsShowParticles();
    }

    /**
     * Reads the raw packet data from the data stream.
     */
    public void readPacketData(PacketBuffer buf) throws IOException
    {
        this.entityId = buf.readVarInt();
        this.effectId = buf.readEnumValue(Potion.class);
        this.amplifier = buf.readVarInt();
        this.duration = buf.readVarInt();
        this.remaining = buf.readVarInt();
        this.particles = buf.readBoolean();
    }

    /**
     * Writes the raw packet data to the data stream.
     */
    public void writePacketData(PacketBuffer buf) throws IOException
    {
        buf.writeVarInt(this.entityId);
        buf.writeEnumValue(this.effectId);
        buf.writeVarInt(this.amplifier);
        buf.writeVarInt(this.duration);
        buf.writeVarInt(this.remaining);
        buf.writeBoolean(this.particles);
    }
    
    /**
     * Passes this Packet on to the NetHandler for processing.
     */
    public void processPacket(IClientPlayer handler)
    {
        handler.handleEntityEffect(this);
    }

    public int getEntityId()
    {
        return this.entityId;
    }

    public Potion getEffectId()
    {
        return this.effectId;
    }

    public int getAmplifier()
    {
        return this.amplifier;
    }

    public int getDuration()
    {
        return this.duration;
    }

    public int getRemaining()
    {
        return this.remaining;
    }

    public boolean hasParticles()
    {
        return this.particles;
    }
}
