package net.minecraft.network.protocol.game;

import net.minecraft.network.PacketListener;
import java.io.IOException;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.world.BossEvent;
import net.minecraft.network.chat.Component;
import java.util.UUID;
import net.minecraft.network.protocol.Packet;

public class ClientboundBossEventPacket implements Packet<ClientGamePacketListener> {
    private UUID id;
    private Operation operation;
    private Component name;
    private float pct;
    private BossEvent.BossBarColor color;
    private BossEvent.BossBarOverlay overlay;
    private boolean darkenScreen;
    private boolean playMusic;
    private boolean createWorldFog;
    
    public ClientboundBossEventPacket() {
    }
    
    public ClientboundBossEventPacket(final Operation a, final BossEvent ajb) {
        this.operation = a;
        this.id = ajb.getId();
        this.name = ajb.getName();
        this.pct = ajb.getPercent();
        this.color = ajb.getColor();
        this.overlay = ajb.getOverlay();
        this.darkenScreen = ajb.shouldDarkenScreen();
        this.playMusic = ajb.shouldPlayBossMusic();
        this.createWorldFog = ajb.shouldCreateWorldFog();
    }
    
    @Override
    public void read(final FriendlyByteBuf kv) throws IOException {
        this.id = kv.readUUID();
        this.operation = kv.<Operation>readEnum(Operation.class);
        switch (this.operation) {
            case ADD: {
                this.name = kv.readComponent();
                this.pct = kv.readFloat();
                this.color = kv.<BossEvent.BossBarColor>readEnum(BossEvent.BossBarColor.class);
                this.overlay = kv.<BossEvent.BossBarOverlay>readEnum(BossEvent.BossBarOverlay.class);
                this.decodeProperties(kv.readUnsignedByte());
            }
            case UPDATE_PCT: {
                this.pct = kv.readFloat();
                break;
            }
            case UPDATE_NAME: {
                this.name = kv.readComponent();
                break;
            }
            case UPDATE_STYLE: {
                this.color = kv.<BossEvent.BossBarColor>readEnum(BossEvent.BossBarColor.class);
                this.overlay = kv.<BossEvent.BossBarOverlay>readEnum(BossEvent.BossBarOverlay.class);
                break;
            }
            case UPDATE_PROPERTIES: {
                this.decodeProperties(kv.readUnsignedByte());
                break;
            }
        }
    }
    
    private void decodeProperties(final int integer) {
        this.darkenScreen = ((integer & 0x1) > 0);
        this.playMusic = ((integer & 0x2) > 0);
        this.createWorldFog = ((integer & 0x4) > 0);
    }
    
    @Override
    public void write(final FriendlyByteBuf kv) throws IOException {
        kv.writeUUID(this.id);
        kv.writeEnum(this.operation);
        switch (this.operation) {
            case ADD: {
                kv.writeComponent(this.name);
                kv.writeFloat(this.pct);
                kv.writeEnum(this.color);
                kv.writeEnum(this.overlay);
                kv.writeByte(this.encodeProperties());
            }
            case UPDATE_PCT: {
                kv.writeFloat(this.pct);
                break;
            }
            case UPDATE_NAME: {
                kv.writeComponent(this.name);
                break;
            }
            case UPDATE_STYLE: {
                kv.writeEnum(this.color);
                kv.writeEnum(this.overlay);
                break;
            }
            case UPDATE_PROPERTIES: {
                kv.writeByte(this.encodeProperties());
                break;
            }
        }
    }
    
    private int encodeProperties() {
        int integer2 = 0;
        if (this.darkenScreen) {
            integer2 |= 0x1;
        }
        if (this.playMusic) {
            integer2 |= 0x2;
        }
        if (this.createWorldFog) {
            integer2 |= 0x4;
        }
        return integer2;
    }
    
    @Override
    public void handle(final ClientGamePacketListener lw) {
        lw.handleBossUpdate(this);
    }
    
    public UUID getId() {
        return this.id;
    }
    
    public Operation getOperation() {
        return this.operation;
    }
    
    public Component getName() {
        return this.name;
    }
    
    public float getPercent() {
        return this.pct;
    }
    
    public BossEvent.BossBarColor getColor() {
        return this.color;
    }
    
    public BossEvent.BossBarOverlay getOverlay() {
        return this.overlay;
    }
    
    public boolean shouldDarkenScreen() {
        return this.darkenScreen;
    }
    
    public boolean shouldPlayMusic() {
        return this.playMusic;
    }
    
    public boolean shouldCreateWorldFog() {
        return this.createWorldFog;
    }
    
    public enum Operation {
        ADD, 
        REMOVE, 
        UPDATE_PCT, 
        UPDATE_NAME, 
        UPDATE_STYLE, 
        UPDATE_PROPERTIES;
    }
}
