package net.miarma.hystuff.commands;

import com.hypixel.hytale.component.Ref;
import com.hypixel.hytale.component.Store;
import com.hypixel.hytale.math.vector.Vector3d;
import com.hypixel.hytale.math.vector.Vector3f;
import com.hypixel.hytale.protocol.GameMode;
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.command.system.basecommands.CommandBase;
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.common.HomeManager;
import net.miarma.hystuff.common.LastPositionManager;
import net.miarma.hystuff.common.Location;

import javax.annotation.Nonnull;
import java.util.UUID;

public class HomeCommand extends AbstractPlayerCommand {
    private final HomeManager homeManager;
    private final LastPositionManager lastPositionManager;

    public HomeCommand(HomeManager homeManager) {
        super("home", "Teleports you to your home");
        this.homeManager = homeManager;
        this.lastPositionManager = LastPositionManager.getInstance();
    }

    @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) {
        UUID playerUuid = playerRef.getUuid();
        Location home = this.homeManager.getHome(playerUuid);
        if (home == null) {
            ctx.sendMessage(Message.raw("You don't have a home set. Use /sethome first."));
        } else {
            World currentWorld = Universe.get().getWorld(playerRef.getWorldUuid());
            Vector3d pos = playerRef.getTransform().getPosition();
            Vector3f rotation = playerRef.getHeadRotation();
            Location currentLocation = new Location(currentWorld.getName(), pos.getX(), pos.getY(),
                    pos.getZ(), rotation.getYaw(), rotation.getPitch());
            this.lastPositionManager.set(playerUuid, currentLocation);

            final World homeWorld = Universe.get().getWorld(home.worldName()) != null ?
                    Universe.get().getWorld(home.worldName()) : world;

            world.execute(() -> {
                Teleport teleport = Teleport.createForPlayer(
                    homeWorld,
                    home.toPosition(),
                    home.toRotation()
                );
                store.addComponent(ref, Teleport.getComponentType(), teleport);
                ctx.sendMessage(Message.raw(String.format("Teleporting to your home at %.1f, %.1f, %.1f",
                        home.x(), home.y(), home.z())));
            });
        }
    }
}