package net.miarma.hystuff.commands.misc;

import com.hypixel.hytale.component.Ref;
import com.hypixel.hytale.component.Store;
import com.hypixel.hytale.server.core.Message;
import com.hypixel.hytale.server.core.command.system.CommandContext;
import com.hypixel.hytale.server.core.command.system.basecommands.AbstractPlayerCommand;
import com.hypixel.hytale.server.core.universe.PlayerRef;
import com.hypixel.hytale.server.core.universe.world.World;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import net.miarma.hystuff.HyStuff;
import net.miarma.hystuff.component.PlaytimeComponent;
import org.checkerframework.checker.nullness.compatqual.NonNullDecl;

public class PlaytimeCommand extends AbstractPlayerCommand {
    public PlaytimeCommand() {
        super("playtime", "Tells you how long you've been playing.");
    }

    @Override
    protected void execute(@NonNullDecl CommandContext ctx,
                           @NonNullDecl Store<EntityStore> store,
                           @NonNullDecl Ref<EntityStore> ref,
                           @NonNullDecl PlayerRef playerRef,
                           @NonNullDecl World world) {
        PlaytimeComponent playtimeComp = store.getComponent(ref, HyStuff.PLAYTIME_COMPONENT_TYPE);

        long seconds = 0L;
        if (playtimeComp != null) {
            seconds = playtimeComp.getPlaytime();
        }

        long h = seconds / 3600;
        long m = (seconds % 3600) / 60;

        ctx.sendMessage(Message.raw("Playtime: " + h + "h " + m + "m"));
    }
}
