package net.miarma.hystuff.commands.misc;

import com.hypixel.hytale.component.Ref;
import com.hypixel.hytale.component.Store;
import com.hypixel.hytale.math.vector.Transform;
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.modules.entity.component.TransformComponent;
import com.hypixel.hytale.server.core.modules.entity.teleport.Teleport;
import com.hypixel.hytale.server.core.universe.PlayerRef;
import com.hypixel.hytale.server.core.universe.Universe;
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.HomeComponent;
import net.miarma.hystuff.component.LastPositionComponent;

import javax.annotation.Nonnull;

public class HomeCommand extends AbstractPlayerCommand {
    public HomeCommand() {
        super("home", "Teleports you to your home");
    }

    @Override
    protected boolean canGeneratePermission() {
        return false;
    }

    @Override
    protected void execute(@Nonnull CommandContext ctx,
                           @Nonnull Store<EntityStore> store,
                           @Nonnull Ref<EntityStore> ref,
                           @Nonnull PlayerRef playerRef,
                           @Nonnull World world) {
        HomeComponent homeComp = store.getComponent(ref, HyStuff.HOME_COMPONENT_TYPE);

        if (homeComp == null) {
            ctx.sendMessage(Message.raw("You do not have a home set!"));
            return;
        }

        Transform homeLocation = homeComp.getTransform();
        if (homeLocation == null) {
            ctx.sendMessage(Message.raw("Your home data is corrupt."));
            return;
        }

        TransformComponent transformComp = store.getComponent(ref, TransformComponent.getComponentType());
        if (transformComp == null) {
            ctx.sendMessage(Message.raw("Could not get your current position!"));
            return;
        }

        LastPositionComponent last = new LastPositionComponent(
            transformComp.getTransform().clone(),
            world.getWorldConfig().getUuid()
        );

        world.execute(() -> {
            store.putComponent(ref, HyStuff.LAST_POSITION_COMPONENT_TYPE, last);
        });

        Teleport teleport = new Teleport(homeLocation.getPosition(), homeLocation.getRotation());
        World targetWorld = Universe.get().getWorld(homeComp.getWorldUuid());

        world.execute(() -> {
            store.addComponent(ref, Teleport.getComponentType(), teleport);
        });

        ctx.sendMessage(Message.raw(String.format(
            "Teleporting to your home at %.1f, %.1f, %.1f in %s",
            homeLocation.getPosition().getX(),
            homeLocation.getPosition().getY(),
            homeLocation.getPosition().getZ(),
            targetWorld.getName()
        )));
    }
}