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 amw) {
        this.operation = a;
        this.id = amw.getId();
        this.name = amw.getName();
        this.pct = amw.getPercent();
        this.color = amw.getColor();
        this.overlay = amw.getOverlay();
        this.darkenScreen = amw.shouldDarkenScreen();
        this.playMusic = amw.shouldPlayBossMusic();
        this.createWorldFog = amw.shouldCreateWorldFog();
    }
    
    @Override
    public void read(final FriendlyByteBuf mg) throws IOException {
        this.id = mg.readUUID();
        this.operation = mg.<Operation>readEnum(Operation.class);
        switch (this.operation) {
            case ADD: {
                this.name = mg.readComponent();
                this.pct = mg.readFloat();
                this.color = mg.<BossEvent.BossBarColor>readEnum(BossEvent.BossBarColor.class);
                this.overlay = mg.<BossEvent.BossBarOverlay>readEnum(BossEvent.BossBarOverlay.class);
                this.decodeProperties(mg.readUnsignedByte());
            }
            case UPDATE_PCT: {
                this.pct = mg.readFloat();
                break;
            }
            case UPDATE_NAME: {
                this.name = mg.readComponent();
                break;
            }
            case UPDATE_STYLE: {
                this.color = mg.<BossEvent.BossBarColor>readEnum(BossEvent.BossBarColor.class);
                this.overlay = mg.<BossEvent.BossBarOverlay>readEnum(BossEvent.BossBarOverlay.class);
                break;
            }
            case UPDATE_PROPERTIES: {
                this.decodeProperties(mg.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 mg) throws IOException {
        mg.writeUUID(this.id);
        mg.writeEnum(this.operation);
        switch (this.operation) {
            case ADD: {
                mg.writeComponent(this.name);
                mg.writeFloat(this.pct);
                mg.writeEnum(this.color);
                mg.writeEnum(this.overlay);
                mg.writeByte(this.encodeProperties());
            }
            case UPDATE_PCT: {
                mg.writeFloat(this.pct);
                break;
            }
            case UPDATE_NAME: {
                mg.writeComponent(this.name);
                break;
            }
            case UPDATE_STYLE: {
                mg.writeEnum(this.color);
                mg.writeEnum(this.overlay);
                break;
            }
            case UPDATE_PROPERTIES: {
                mg.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 nl) {
        nl.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;
    }
}
