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

package com.hypixel.hytale.builtin.adventure.objectives.config.triggercondition;

import com.hypixel.hytale.codec.validation.Validator;
import com.hypixel.hytale.codec.validation.Validators;
import com.hypixel.hytale.codec.KeyedCodec;
import com.hypixel.hytale.codec.Codec;
import com.hypixel.hytale.server.core.asset.type.weather.config.Weather;
import com.hypixel.hytale.component.Store;
import com.hypixel.hytale.server.core.universe.world.World;
import com.hypixel.hytale.server.core.universe.world.storage.ChunkStore;
import java.util.Arrays;
import com.hypixel.hytale.server.core.universe.world.chunk.BlockChunk;
import com.hypixel.hytale.builtin.adventure.objectives.markers.objectivelocation.ObjectiveLocationMarker;
import com.hypixel.hytale.component.Ref;
import com.hypixel.hytale.component.ComponentAccessor;
import com.hypixel.hytale.server.core.modules.entity.component.TransformComponent;
import com.hypixel.hytale.component.ComponentType;
import com.hypixel.hytale.builtin.weather.resources.WeatherResource;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import com.hypixel.hytale.component.ResourceType;
import javax.annotation.Nonnull;
import com.hypixel.hytale.codec.builder.BuilderCodec;

public class WeatherTriggerCondition extends ObjectiveLocationTriggerCondition
{
    @Nonnull
    public static final BuilderCodec<WeatherTriggerCondition> CODEC;
    @Nonnull
    protected static final ResourceType<EntityStore, WeatherResource> WEATHER_RESOURCE_RESOURCE_TYPE;
    @Nonnull
    protected static final ComponentType<EntityStore, TransformComponent> TRANSFORM_COMPONENT_TYPE;
    protected String[] weatherIds;
    protected int[] weatherIndexes;
    
    @Override
    public boolean isConditionMet(@Nonnull final ComponentAccessor<EntityStore> componentAccessor, @Nonnull final Ref<EntityStore> ref, final ObjectiveLocationMarker objectiveLocationMarker) {
        final WeatherResource weatherResource = componentAccessor.getResource(WeatherTriggerCondition.WEATHER_RESOURCE_RESOURCE_TYPE);
        final TransformComponent transformComponent = componentAccessor.getComponent(ref, WeatherTriggerCondition.TRANSFORM_COMPONENT_TYPE);
        assert transformComponent != null;
        final Ref<ChunkStore> chunkRef = transformComponent.getChunkRef();
        if (chunkRef == null || !chunkRef.isValid()) {
            return false;
        }
        final World world = componentAccessor.getExternalData().getWorld();
        final Store<ChunkStore> chunkStore = world.getChunkStore().getStore();
        final BlockChunk blockChunkComponent = chunkStore.getComponent(chunkRef, BlockChunk.getComponentType());
        assert blockChunkComponent != null;
        final int environmentIndex = blockChunkComponent.getEnvironment(transformComponent.getPosition());
        final int currentWeatherIndex = weatherResource.getWeatherIndexForEnvironment(environmentIndex);
        return Arrays.binarySearch(this.weatherIndexes, currentWeatherIndex) >= 0;
    }
    
    @Nonnull
    @Override
    public String toString() {
        return "WeatherTriggerCondition{weatherIds=" + Arrays.toString(this.weatherIds) + "} " + super.toString();
    }
    
    static {
        CODEC = BuilderCodec.builder(WeatherTriggerCondition.class, WeatherTriggerCondition::new).append(new KeyedCodec<String[]>("WeatherIds", Codec.STRING_ARRAY), (weatherTriggerCondition, strings) -> weatherTriggerCondition.weatherIds = strings, weatherTriggerCondition -> weatherTriggerCondition.weatherIds).addValidator(Validators.nonEmptyArray()).addValidator(Weather.VALIDATOR_CACHE.getArrayValidator()).add().afterDecode(weatherTriggerCondition -> {
            if (weatherTriggerCondition.weatherIds == null) {
                return;
            }
            else {
                weatherTriggerCondition.weatherIndexes = new int[weatherTriggerCondition.weatherIds.length];
                int i = 0;
                while (i < weatherTriggerCondition.weatherIds.length) {
                    final String key = weatherTriggerCondition.weatherIds[i];
                    final int index = Weather.getAssetMap().getIndex(key);
                    if (index == Integer.MIN_VALUE) {
                        throw new IllegalArgumentException("Unknown key! " + key);
                    }
                    else {
                        weatherTriggerCondition.weatherIndexes[i] = index;
                        ++i;
                    }
                }
                Arrays.sort(weatherTriggerCondition.weatherIndexes);
                return;
            }
        }).build();
        WEATHER_RESOURCE_RESOURCE_TYPE = WeatherResource.getResourceType();
        TRANSFORM_COMPONENT_TYPE = TransformComponent.getComponentType();
    }
}
