package net.miarma.hystuff.system;

import com.hypixel.hytale.component.*;
import com.hypixel.hytale.component.query.Query;
import com.hypixel.hytale.component.system.tick.EntityTickingSystem;
import com.hypixel.hytale.server.core.entity.EntityUtils;
import com.hypixel.hytale.server.core.entity.entities.Player;
import com.hypixel.hytale.server.core.universe.PlayerRef;
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;
import org.checkerframework.checker.nullness.compatqual.NullableDecl;

public class PlayerTickSystem extends EntityTickingSystem<EntityStore> {

    @NullableDecl
    @Override
    public Query<EntityStore> getQuery() {
        return PlayerRef.getComponentType();
    }

    @Override public void tick(float dt, int i,
                               @NonNullDecl ArchetypeChunk<EntityStore> chunk,
                               @NonNullDecl Store<EntityStore> store,
                               @NonNullDecl CommandBuffer<EntityStore> buffer) {

        Ref<EntityStore> entityRef = chunk.getReferenceTo(i);
        PlayerRef playerRef = store.getComponent(entityRef, PlayerRef.getComponentType());

        if (playerRef == null) return;

        PlaytimeComponent playTimeComp = store.getComponent(entityRef, HyStuff.PLAYTIME_COMPONENT_TYPE);
        if (playTimeComp == null) {
            playTimeComp = new PlaytimeComponent(0L);
            store.addComponent(entityRef, HyStuff.PLAYTIME_COMPONENT_TYPE, playTimeComp);
        }

        playTimeComp.tick(dt);
    }
}
