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

package com.hypixel.hytale.builtin.asseteditor.assettypehandler;

import com.hypixel.hytale.assetstore.AssetPack;
import java.io.IOException;
import java.util.logging.Level;
import java.nio.file.Files;
import com.hypixel.hytale.server.core.asset.AssetModule;
import it.unimi.dsi.fastutil.booleans.BooleanObjectPair;
import java.util.Collections;
import com.hypixel.hytale.server.core.asset.common.CommonAssetModule;
import com.hypixel.hytale.server.core.universe.Universe;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.core.asset.common.CommonAsset;
import com.hypixel.hytale.server.core.asset.common.CommonAssetRegistry;
import com.hypixel.hytale.server.core.asset.common.asset.FileCommonAsset;
import com.hypixel.hytale.common.util.PathUtil;
import com.hypixel.hytale.builtin.asseteditor.util.AssetPathUtil;
import com.hypixel.hytale.builtin.asseteditor.EditorClient;
import com.hypixel.hytale.assetstore.AssetUpdateQuery;
import java.nio.file.Path;
import com.hypixel.hytale.builtin.asseteditor.AssetPath;
import com.hypixel.hytale.protocol.packets.asseteditor.AssetEditorAssetType;
import com.hypixel.hytale.protocol.packets.asseteditor.AssetEditorEditorType;
import com.hypixel.hytale.logger.HytaleLogger;

public class CommonAssetTypeHandler extends AssetTypeHandler
{
    private static final HytaleLogger LOGGER;
    
    public CommonAssetTypeHandler(final String id, final String icon, final String fileExtension, final AssetEditorEditorType editorType) {
        super(new AssetEditorAssetType(id, icon, true, "Common", fileExtension, editorType));
    }
    
    @Nonnull
    @Override
    public AssetLoadResult loadAsset(final AssetPath path, final Path dataPath, final byte[] data, final AssetUpdateQuery updateQuery, final EditorClient editorClient) {
        final String relativePath = PathUtil.toUnixPathString(AssetPathUtil.PATH_DIR_COMMON.relativize(path.path()));
        final FileCommonAsset newAsset = new FileCommonAsset(dataPath, relativePath, data);
        final CommonAssetRegistry.AddCommonAssetResult result = CommonAssetRegistry.addCommonAsset(path.packId(), newAsset);
        final CommonAssetRegistry.PackAsset asset = result.getNewPackAsset();
        final CommonAssetRegistry.PackAsset oldAsset = result.getPreviousNameAsset();
        if (oldAsset == null || !oldAsset.asset().getHash().equals(asset.asset().getHash())) {
            return AssetLoadResult.COMMON_ASSETS_CHANGED;
        }
        return AssetLoadResult.ASSETS_UNCHANGED;
    }
    
    @Nonnull
    @Override
    public AssetLoadResult unloadAsset(@Nonnull final AssetPath path, @Nonnull final AssetUpdateQuery updateQuery) {
        final BooleanObjectPair<CommonAssetRegistry.PackAsset> removedCommonAsset = CommonAssetRegistry.removeCommonAssetByName(path.packId(), PathUtil.toUnixPathString(AssetPathUtil.PATH_DIR_COMMON.relativize(path.path())));
        if (removedCommonAsset != null) {
            if (Universe.get().getPlayerCount() > 0) {
                if (removedCommonAsset.firstBoolean()) {
                    CommonAssetModule.get().sendAsset(removedCommonAsset.second().asset(), updateQuery.getRebuildCache().isCommonAssetsRebuild());
                }
                else {
                    CommonAssetModule.get().sendRemoveAssets(Collections.singletonList(removedCommonAsset.second()), updateQuery.getRebuildCache().isCommonAssetsRebuild());
                }
            }
            return AssetLoadResult.COMMON_ASSETS_CHANGED;
        }
        return AssetLoadResult.ASSETS_UNCHANGED;
    }
    
    @Nonnull
    @Override
    public AssetLoadResult restoreOriginalAsset(@Nonnull final AssetPath originalAssetPath, final AssetUpdateQuery updateQuery) {
        final AssetPack pack = AssetModule.get().getAssetPack(originalAssetPath.packId());
        final Path absolutePath = pack.getRoot().resolve(originalAssetPath.path()).toAbsolutePath();
        byte[] bytes = null;
        try {
            bytes = Files.readAllBytes(absolutePath);
        }
        catch (final IOException e) {
            CommonAssetTypeHandler.LOGGER.at(Level.WARNING).withCause(e).log("Failed to load file %s", absolutePath);
        }
        if (bytes == null) {
            return AssetLoadResult.ASSETS_UNCHANGED;
        }
        final String relativePath = PathUtil.toUnixPathString(AssetPathUtil.PATH_DIR_COMMON.relativize(originalAssetPath.path()));
        final FileCommonAsset commonAsset = new FileCommonAsset(absolutePath, relativePath, bytes);
        final CommonAssetRegistry.AddCommonAssetResult result = CommonAssetRegistry.addCommonAsset(originalAssetPath.packId(), commonAsset);
        final CommonAssetRegistry.PackAsset oldAsset = result.getPreviousNameAsset();
        final CommonAssetRegistry.PackAsset newAsset = result.getNewPackAsset();
        if (oldAsset == null || !oldAsset.asset().getHash().equals(newAsset.asset().getHash())) {
            return AssetLoadResult.COMMON_ASSETS_CHANGED;
        }
        return AssetLoadResult.ASSETS_UNCHANGED;
    }
    
    @Nonnull
    @Override
    public AssetUpdateQuery getDefaultUpdateQuery() {
        if (this.cachedDefaultUpdateQuery == null) {
            this.cachedDefaultUpdateQuery = new AssetUpdateQuery(new AssetUpdateQuery.RebuildCache(false, false, false, false, false, true));
        }
        return this.cachedDefaultUpdateQuery;
    }
    
    static {
        LOGGER = HytaleLogger.forEnclosingClass();
    }
}
