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

package com.hypixel.hytale.server.core.update.command;

import java.util.concurrent.TimeUnit;
import com.hypixel.hytale.common.util.FormatUtil;
import com.hypixel.hytale.server.core.Message;
import com.hypixel.hytale.server.core.update.UpdateModule;
import com.hypixel.hytale.server.core.update.UpdateService;
import com.hypixel.hytale.common.util.java.ManifestUtil;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.core.command.system.CommandContext;
import com.hypixel.hytale.server.core.command.system.basecommands.CommandBase;

public class UpdateStatusCommand extends CommandBase
{
    public UpdateStatusCommand() {
        super("status", "server.commands.update.status.desc");
    }
    
    @Override
    protected void executeSync(@Nonnull final CommandContext context) {
        final String currentVersion = ManifestUtil.getImplementationVersion();
        final String patchline = UpdateService.getEffectivePatchline();
        final String stagedVersion = UpdateService.getStagedVersion();
        final UpdateModule updateModule = UpdateModule.get();
        final Message msg = Message.translation("server.commands.update.status").param("version", (currentVersion != null) ? currentVersion : "unknown").param("patchline", patchline).param("staged", (stagedVersion != null) ? stagedVersion : "none").param("latest", this.getLatestStatus(currentVersion, updateModule)).param("downloading", this.getDownloadStatus(updateModule));
        context.sendMessage(msg);
    }
    
    private String getLatestStatus(final String currentVersion, final UpdateModule updateModule) {
        if (updateModule == null) {
            return "unknown";
        }
        final UpdateService.VersionManifest latestKnown = updateModule.getLatestKnownVersion();
        if (latestKnown == null) {
            return "not checked";
        }
        if (currentVersion != null && currentVersion.equals(latestKnown.version)) {
            return "up to date";
        }
        return latestKnown.version + " available";
    }
    
    private String getDownloadStatus(final UpdateModule updateModule) {
        if (updateModule == null || !updateModule.isDownloadInProgress()) {
            return "no";
        }
        final UpdateModule.DownloadProgress progress = updateModule.getDownloadProgress();
        if (progress == null) {
            return "starting...";
        }
        final StringBuilder sb = new StringBuilder();
        sb.append(progress.percent()).append("% (");
        sb.append(FormatUtil.bytesToString(progress.downloadedBytes()));
        sb.append("/");
        sb.append(FormatUtil.bytesToString(progress.totalBytes()));
        sb.append(")");
        if (progress.etaSeconds() >= 0L) {
            sb.append(" ETA: ").append(FormatUtil.timeUnitToString(progress.etaSeconds(), TimeUnit.SECONDS));
        }
        return sb.toString();
    }
}
