package net.miarma.hystuff;

import com.hypixel.hytale.component.ComponentType;
import com.hypixel.hytale.event.EventRegistry;
import com.hypixel.hytale.logger.HytaleLogger;
import com.hypixel.hytale.math.vector.Vector3d;
import com.hypixel.hytale.math.vector.Vector3f;
import com.hypixel.hytale.server.core.event.events.player.AddPlayerToWorldEvent;
import com.hypixel.hytale.server.core.event.events.player.PlayerConnectEvent;
import com.hypixel.hytale.server.core.modules.entity.component.HeadRotation;
import com.hypixel.hytale.server.core.modules.entity.component.TransformComponent;
import com.hypixel.hytale.server.core.plugin.JavaPlugin;
import com.hypixel.hytale.server.core.plugin.JavaPluginInit;
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.commands.BackCommand;
import net.miarma.hystuff.commands.HomeCommand;
import net.miarma.hystuff.commands.SetHomeCommand;
import net.miarma.hystuff.component.HomeComponent;
import net.miarma.hystuff.component.LastPositionComponent;
import net.miarma.hystuff.events.PlayerDeathListener;

import javax.annotation.Nonnull;

/**
 * This class serves as the entrypoint for your plugin. Use the setup method to register into game registries or add
 * event listeners.
 */
public class HyStuff extends JavaPlugin {
    private PlayerDeathListener playerDeathListener;
    private static final HytaleLogger LOGGER = HytaleLogger.forEnclosingClass();
    public static ComponentType<EntityStore, HomeComponent> HOME_COMPONENT_TYPE;
    public static ComponentType<EntityStore, LastPositionComponent> LAST_POSITION_COMPONENT_TYPE;

    public HyStuff(@Nonnull JavaPluginInit init) {
        super(init);
    }

    @Override
    protected void setup() {
        LOGGER.atInfo().log("------ HYSTUFF PLUGIN SETUP BEGIN ------");
        this.playerDeathListener = new PlayerDeathListener();
        LOGGER.atInfo().log("------- HYSTUFF PLUGIN SETUP END -------");
    }

    @Override
    protected void start() {
        LOGGER.atInfo().log("HyStuff plugin starting...");

        HOME_COMPONENT_TYPE = this.getEntityStoreRegistry()
                .registerComponent(HomeComponent.class, "hystuff:home", HomeComponent.CODEC);
        LAST_POSITION_COMPONENT_TYPE = this.getEntityStoreRegistry()
                .registerComponent(LastPositionComponent.class, "hystuff:last_position", LastPositionComponent.CODEC);

        this.getCommandRegistry().registerCommand(new HomeCommand());
        this.getCommandRegistry().registerCommand(new SetHomeCommand());
        this.getCommandRegistry().registerCommand(new BackCommand());

        if (this.playerDeathListener != null) {
           try {
               EntityStore.REGISTRY.registerSystem(this.playerDeathListener);
               LOGGER.atInfo().log("Death location tracking enabled.");
           } catch (Exception e) {
               LOGGER.atWarning().log("Could not register death listener: " + e.getMessage());
           }
        }

        LOGGER.atInfo().log("HyStuff plugin started succesfully!");
        LOGGER.atInfo().log("Commands registered: /home, /sethome");
    }

    @Override
    protected void shutdown() {
        LOGGER.atInfo().log("HyStuff plugin is shutting down...");
        if (this.playerDeathListener != null) {
            EntityStore.REGISTRY.unregisterSystem(PlayerDeathListener.class);
        }
    }
}