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

package com.hypixel.hytale.server.core.asset.common;

import com.hypixel.hytale.codec.KeyedCodec;
import com.hypixel.hytale.codec.Codec;
import com.hypixel.hytale.codec.builder.BuilderCodec;
import java.util.concurrent.ConcurrentHashMap;
import com.hypixel.hytale.common.util.CompletableFutureUtil;
import java.io.IOException;
import com.hypixel.hytale.sneakythrow.SneakyThrow;
import com.hypixel.hytale.codec.ExtraInfo;
import com.hypixel.hytale.codec.util.RawJsonReader;
import java.nio.charset.StandardCharsets;
import javax.annotation.Nullable;
import javax.annotation.Nonnull;
import java.util.concurrent.CompletableFuture;
import java.util.Map;
import com.hypixel.hytale.logger.HytaleLogger;

public class BlockyAnimationCache
{
    public static final HytaleLogger LOGGER;
    private static final Map<String, BlockyAnimation> animations;
    
    @Nonnull
    public static CompletableFuture<BlockyAnimation> get(final String name) {
        final BlockyAnimation animationData = BlockyAnimationCache.animations.get(name);
        if (animationData != null) {
            return CompletableFuture.completedFuture(animationData);
        }
        final CommonAsset asset = CommonAssetRegistry.getByName(name);
        if (asset == null) {
            return CompletableFuture.completedFuture((BlockyAnimation)null);
        }
        return get0(asset);
    }
    
    @Nonnull
    public static CompletableFuture<BlockyAnimation> get(@Nonnull final CommonAsset asset) {
        final BlockyAnimation animationData = BlockyAnimationCache.animations.get(asset.getName());
        if (animationData != null) {
            return CompletableFuture.completedFuture(animationData);
        }
        return get0(asset);
    }
    
    @Nullable
    public static BlockyAnimation getNow(final String name) {
        final BlockyAnimation animationData = BlockyAnimationCache.animations.get(name);
        if (animationData != null) {
            return animationData;
        }
        final CommonAsset asset = CommonAssetRegistry.getByName(name);
        if (asset == null) {
            return null;
        }
        return get0(asset).join();
    }
    
    public static BlockyAnimation getNow(@Nonnull final CommonAsset asset) {
        final BlockyAnimation animationData = BlockyAnimationCache.animations.get(asset.getName());
        if (animationData != null) {
            return animationData;
        }
        return get0(asset).join();
    }
    
    @Nonnull
    private static CompletableFuture<BlockyAnimation> get0(@Nonnull final CommonAsset asset) {
        final String name = asset.getName();
        return CompletableFutureUtil._catch(asset.getBlob().thenApply(bytes -> {
            final String str = new String(bytes, StandardCharsets.UTF_8);
            final RawJsonReader reader = RawJsonReader.fromJsonString(str);
            try {
                final ExtraInfo extraInfo = ExtraInfo.THREAD_LOCAL.get();
                final BlockyAnimation newAnimationData = BlockyAnimation.CODEC.decodeJson(reader, extraInfo);
                extraInfo.getValidationResults().logOrThrowValidatorExceptions(BlockyAnimationCache.LOGGER);
                BlockyAnimationCache.animations.put(name, newAnimationData);
                return newAnimationData;
            }
            catch (final IOException e) {
                throw SneakyThrow.sneakyThrow(e);
            }
        }));
    }
    
    public static void invalidate(final String name) {
        BlockyAnimationCache.animations.remove(name);
    }
    
    static {
        LOGGER = HytaleLogger.forEnclosingClass();
        animations = new ConcurrentHashMap<String, BlockyAnimation>();
    }
    
    public static class BlockyAnimation
    {
        public static final BuilderCodec<BlockyAnimation> CODEC;
        public static final double FRAMES_PER_SECOND = 60.0;
        private int duration;
        
        public int getDurationFrames() {
            return this.duration;
        }
        
        public double getDurationMillis() {
            return this.duration * 1000.0 / 60.0;
        }
        
        public double getDurationSeconds() {
            return this.duration / 60.0;
        }
        
        @Nonnull
        @Override
        public String toString() {
            return "BlockyAnimation{duration=" + this.duration;
        }
        
        static {
            CODEC = BuilderCodec.builder(BlockyAnimation.class, BlockyAnimation::new).addField(new KeyedCodec<Integer>("duration", Codec.INTEGER, true, true), (blockyAnimation, i) -> blockyAnimation.duration = i, blockyAnimation -> blockyAnimation.duration).build();
        }
    }
}
