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

package com.hypixel.hytale.server.npc.corecomponents.world;

import com.hypixel.hytale.common.util.StringUtil;
import com.hypixel.hytale.server.npc.sensorinfo.InfoProvider;
import com.hypixel.hytale.server.core.asset.type.weather.config.Weather;
import com.hypixel.hytale.component.ComponentAccessor;
import com.hypixel.hytale.component.Store;
import com.hypixel.hytale.server.npc.role.Role;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import com.hypixel.hytale.component.Ref;
import com.hypixel.hytale.server.npc.corecomponents.builders.BuilderSensorBase;
import com.hypixel.hytale.server.npc.asset.builder.BuilderSupport;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.npc.corecomponents.world.builders.BuilderSensorWeather;
import javax.annotation.Nullable;
import com.hypixel.hytale.server.npc.corecomponents.SensorBase;

public class SensorWeather extends SensorBase
{
    @Nullable
    protected final String[] weathers;
    protected int prevWeatherIndex;
    protected boolean cachedResult;
    
    public SensorWeather(@Nonnull final BuilderSensorWeather builder, @Nonnull final BuilderSupport support) {
        super(builder);
        this.weathers = builder.getWeathers(support);
    }
    
    @Override
    public boolean matches(@Nonnull final Ref<EntityStore> ref, @Nonnull final Role role, final double dt, @Nonnull final Store<EntityStore> store) {
        if (!super.matches(ref, role, dt, store)) {
            return false;
        }
        final int weatherIndex = role.getWorldSupport().getCurrentWeatherIndex(store);
        if (weatherIndex == 0) {
            return false;
        }
        if (weatherIndex == this.prevWeatherIndex) {
            return this.cachedResult;
        }
        final String weatherAssetId = Weather.getAssetMap().getAsset(weatherIndex).getId();
        this.prevWeatherIndex = weatherIndex;
        return this.cachedResult = this.matchesWeather(weatherAssetId);
    }
    
    @Override
    public InfoProvider getSensorInfo() {
        return null;
    }
    
    protected boolean matchesWeather(@Nullable final String weather) {
        if (weather == null) {
            return false;
        }
        for (final String weatherMatcher : this.weathers) {
            if (StringUtil.isGlobMatching(weatherMatcher, weather)) {
                return true;
            }
        }
        return false;
    }
}
