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

package com.hypixel.hytale.builtin.deployables.component;

import java.util.concurrent.ThreadLocalRandom;
import com.hypixel.hytale.server.core.entity.UUIDComponent;
import com.hypixel.hytale.component.CommandBuffer;
import com.hypixel.hytale.component.Store;
import com.hypixel.hytale.component.ArchetypeChunk;
import com.hypixel.hytale.builtin.deployables.DeployablesPlugin;
import com.hypixel.hytale.component.ComponentType;
import java.util.EnumMap;
import com.hypixel.hytale.protocol.Vector3f;
import java.time.Instant;
import java.util.UUID;
import com.hypixel.hytale.component.Ref;
import com.hypixel.hytale.builtin.deployables.config.DeployableConfig;
import javax.annotation.Nonnull;
import java.util.Map;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import com.hypixel.hytale.component.Component;

public class DeployableComponent implements Component<EntityStore>
{
    @Nonnull
    private final Map<DeployableFlag, Integer> flags;
    private DeployableConfig config;
    private Ref<EntityStore> owner;
    private UUID ownerUUID;
    private Instant spawnInstant;
    private float timeSinceLastAttack;
    private Vector3f debugColor;
    private boolean firstTickRan;
    private String spawnFace;
    
    public DeployableComponent() {
        this.flags = new EnumMap<DeployableFlag, Integer>(DeployableFlag.class);
        this.debugColor = null;
    }
    
    @Nonnull
    public static ComponentType<EntityStore, DeployableComponent> getComponentType() {
        return DeployablesPlugin.get().getDeployableComponentType();
    }
    
    @Override
    public Component<EntityStore> clone() {
        return this;
    }
    
    public void tick(final float dt, final int index, @Nonnull final ArchetypeChunk<EntityStore> archetypeChunk, @Nonnull final Store<EntityStore> store, @Nonnull final CommandBuffer<EntityStore> commandBuffer) {
        if (!this.firstTickRan) {
            this.config.firstTick(this, dt, index, archetypeChunk, store, commandBuffer);
            this.firstTickRan = true;
        }
        this.config.tick(this, dt, index, archetypeChunk, store, commandBuffer);
    }
    
    public void init(@Nonnull final Ref<EntityStore> deployerRef, @Nonnull final Store<EntityStore> store, @Nonnull final DeployableConfig config, @Nonnull final Instant spawnInstant, @Nonnull final String spawnFace) {
        final UUIDComponent uuidComponent = store.getComponent(deployerRef, UUIDComponent.getComponentType());
        assert uuidComponent != null;
        this.config = config;
        this.owner = deployerRef;
        this.spawnInstant = spawnInstant;
        this.spawnFace = spawnFace;
        this.ownerUUID = uuidComponent.getUuid();
    }
    
    public Ref<EntityStore> getOwner() {
        return this.owner;
    }
    
    public UUID getOwnerUUID() {
        return this.ownerUUID;
    }
    
    public DeployableConfig getConfig() {
        return this.config;
    }
    
    public Instant getSpawnInstant() {
        return this.spawnInstant;
    }
    
    public float getTimeSinceLastAttack() {
        return this.timeSinceLastAttack;
    }
    
    public void setTimeSinceLastAttack(final float time) {
        this.timeSinceLastAttack = time;
    }
    
    public float incrementTimeSinceLastAttack(final float time) {
        return this.timeSinceLastAttack += time;
    }
    
    public String getSpawnFace() {
        return this.spawnFace;
    }
    
    public int getFlag(@Nonnull final DeployableFlag key) {
        return this.flags.computeIfAbsent(key, k -> 0);
    }
    
    public void setFlag(@Nonnull final DeployableFlag key, final int value) {
        this.flags.put(key, value);
    }
    
    public Vector3f getDebugColor() {
        if (this.debugColor == null) {
            final ThreadLocalRandom random = ThreadLocalRandom.current();
            this.debugColor = new Vector3f(random.nextFloat(), random.nextFloat(), random.nextFloat());
        }
        return this.debugColor;
    }
    
    public enum DeployableFlag
    {
        STATE, 
        LIVE, 
        BURST_SHOTS, 
        TRIGGERED;
    }
}
