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

package com.hypixel.hytale.server.worldgen.cache;

import com.hypixel.hytale.metrics.metric.AverageCollector;
import com.hypixel.hytale.server.worldgen.chunk.ZoneBiomeResult;
import it.unimi.dsi.fastutil.ints.IntArrayList;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import com.hypixel.hytale.server.worldgen.biome.Biome;
import it.unimi.dsi.fastutil.ints.IntList;
import javax.annotation.Nonnull;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;

public class InterpolatedBiomeCountList
{
    @Nonnull
    private final Int2ObjectMap<BiomeCountResult> results;
    @Nonnull
    private final IntList biomeIds;
    private Biome center;
    
    public InterpolatedBiomeCountList() {
        this.results = new Int2ObjectOpenHashMap<BiomeCountResult>();
        this.biomeIds = new IntArrayList();
    }
    
    public BiomeCountResult get(@Nonnull final Biome biome) {
        return this.get(biome.getId());
    }
    
    public BiomeCountResult get(final int index) {
        return this.results.get(index);
    }
    
    public void setCenter(@Nonnull final ZoneBiomeResult result) {
        final Biome biome = result.getBiome();
        this.center = biome;
        this.biomeIds.add(biome.getId());
        this.results.put(biome.getId(), new BiomeCountResult(biome, result.heightThresholdContext, result.heightmapNoise));
    }
    
    public void add(@Nonnull final ZoneBiomeResult result, final int distance2) {
        final Biome biome = result.getBiome();
        final int biomeId = biome.getId();
        if (this.center.getInterpolation().getBiomeRadius2(biomeId) < distance2) {
            return;
        }
        final BiomeCountResult r = this.get(biomeId);
        if (r == null) {
            this.biomeIds.add(biomeId);
            this.results.put(biomeId, new BiomeCountResult(biome, result.heightThresholdContext, result.heightmapNoise));
        }
        else {
            r.heightNoise = AverageCollector.add(r.heightNoise, result.heightmapNoise, r.count);
            final BiomeCountResult biomeCountResult = r;
            ++biomeCountResult.count;
        }
    }
    
    @Nonnull
    public IntList getBiomeIds() {
        return this.biomeIds;
    }
    
    @Nonnull
    @Override
    public String toString() {
        return "InterpolatedBiomeCountList{results=" + String.valueOf(this.results) + ", biomeIds=" + String.valueOf(this.biomeIds);
    }
    
    public static class BiomeCountResult
    {
        @Nonnull
        public final Biome biome;
        public double heightThresholdContext;
        public double heightNoise;
        public int count;
        
        public BiomeCountResult(@Nonnull final Biome biome, final double heightThresholdContext, final double heightNoise) {
            this.biome = biome;
            this.heightThresholdContext = heightThresholdContext;
            this.heightNoise = heightNoise;
            this.count = 1;
        }
        
        @Nonnull
        @Override
        public String toString() {
            return "BiomeCountResult{biome=" + String.valueOf(this.biome) + ", heightThresholdContext=" + this.heightThresholdContext + ", heightNoise=" + this.heightNoise + ", count=" + this.count;
        }
    }
}
