// 
// Decompiled by Procyon v0.6.0
// 

package com.hypixel.hytale.server.core.entity.entities.player.hud;

import com.hypixel.hytale.protocol.packets.interface_.UpdateVisibleHudComponents;
import com.hypixel.hytale.server.core.io.PacketHandler;
import com.hypixel.hytale.protocol.packets.interface_.ResetUserInterfaceState;
import com.hypixel.hytale.protocol.Packet;
import com.hypixel.hytale.protocol.packets.interface_.CustomUICommand;
import com.hypixel.hytale.protocol.packets.interface_.CustomHud;
import com.hypixel.hytale.server.core.universe.PlayerRef;
import javax.annotation.Nonnull;
import java.util.Collection;
import java.util.Collections;
import java.util.concurrent.ConcurrentHashMap;
import javax.annotation.Nullable;
import com.hypixel.hytale.protocol.packets.interface_.HudComponent;
import java.util.Set;

public class HudManager
{
    private static final Set<HudComponent> DEFAULT_HUD_COMPONENTS;
    private final Set<HudComponent> visibleHudComponents;
    private final Set<HudComponent> unmodifiableVisibleHudComponents;
    @Nullable
    private CustomUIHud customHud;
    
    public HudManager() {
        this.visibleHudComponents = (Set<HudComponent>)ConcurrentHashMap.newKeySet();
        this.unmodifiableVisibleHudComponents = Collections.unmodifiableSet((Set<? extends HudComponent>)this.visibleHudComponents);
        this.visibleHudComponents.addAll(HudManager.DEFAULT_HUD_COMPONENTS);
    }
    
    public HudManager(@Nonnull final HudManager other) {
        this.visibleHudComponents = (Set<HudComponent>)ConcurrentHashMap.newKeySet();
        this.unmodifiableVisibleHudComponents = Collections.unmodifiableSet((Set<? extends HudComponent>)this.visibleHudComponents);
        this.customHud = other.customHud;
    }
    
    @Nullable
    public CustomUIHud getCustomHud() {
        return this.customHud;
    }
    
    @Nonnull
    public Set<HudComponent> getVisibleHudComponents() {
        return this.unmodifiableVisibleHudComponents;
    }
    
    public void setVisibleHudComponents(@Nonnull final PlayerRef ref, final HudComponent... hudComponents) {
        this.visibleHudComponents.clear();
        Collections.addAll(this.visibleHudComponents, hudComponents);
        this.sendVisibleHudComponents(ref.getPacketHandler());
    }
    
    public void setVisibleHudComponents(@Nonnull final PlayerRef ref, @Nonnull final Set<HudComponent> hudComponents) {
        this.visibleHudComponents.clear();
        this.visibleHudComponents.addAll(hudComponents);
        this.sendVisibleHudComponents(ref.getPacketHandler());
    }
    
    public void showHudComponents(@Nonnull final PlayerRef ref, final HudComponent... hudComponents) {
        Collections.addAll(this.visibleHudComponents, hudComponents);
        this.sendVisibleHudComponents(ref.getPacketHandler());
    }
    
    public void showHudComponents(@Nonnull final PlayerRef ref, @Nonnull final Set<HudComponent> hudComponents) {
        this.visibleHudComponents.addAll(hudComponents);
        this.sendVisibleHudComponents(ref.getPacketHandler());
    }
    
    public void hideHudComponents(@Nonnull final PlayerRef ref, @Nonnull final HudComponent... hudComponents) {
        for (final HudComponent hudComponent : hudComponents) {
            this.visibleHudComponents.remove(hudComponent);
        }
        this.sendVisibleHudComponents(ref.getPacketHandler());
    }
    
    public void setCustomHud(@Nonnull final PlayerRef ref, @Nullable final CustomUIHud hud) {
        final CustomUIHud oldHud = this.getCustomHud();
        if (oldHud == hud) {
            return;
        }
        if ((this.customHud = hud) == null) {
            ref.getPacketHandler().writeNoCache(new CustomHud(true, null));
        }
        else {
            hud.show();
        }
    }
    
    public void resetHud(@Nonnull final PlayerRef ref) {
        this.setVisibleHudComponents(ref, HudManager.DEFAULT_HUD_COMPONENTS);
        this.setCustomHud(ref, null);
    }
    
    public void resetUserInterface(@Nonnull final PlayerRef ref) {
        ref.getPacketHandler().writeNoCache(new ResetUserInterfaceState());
    }
    
    public void sendVisibleHudComponents(@Nonnull final PacketHandler packetHandler) {
        packetHandler.writeNoCache(new UpdateVisibleHudComponents(this.visibleHudComponents.toArray(HudComponent[]::new)));
    }
    
    @Nonnull
    @Override
    public String toString() {
        return "HudManager{visibleHudComponents=" + String.valueOf(this.visibleHudComponents) + ", unmodifiableVisibleHudComponents=" + String.valueOf(this.unmodifiableVisibleHudComponents) + ", customHud=" + String.valueOf(this.customHud);
    }
    
    static {
        DEFAULT_HUD_COMPONENTS = Set.of(new HudComponent[] { HudComponent.UtilitySlotSelector, HudComponent.BlockVariantSelector, HudComponent.StatusIcons, HudComponent.Hotbar, HudComponent.Chat, HudComponent.Notifications, HudComponent.KillFeed, HudComponent.InputBindings, HudComponent.Reticle, HudComponent.Compass, HudComponent.Speedometer, HudComponent.ObjectivePanel, HudComponent.PortalPanel, HudComponent.EventTitle, HudComponent.Stamina, HudComponent.AmmoIndicator, HudComponent.Health, HudComponent.Mana, HudComponent.Oxygen, HudComponent.BuilderToolsLegend, HudComponent.Sleep });
    }
}
