package net.miarma.hystuff;

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.common.HomeManager;
import net.miarma.hystuff.common.LastPositionManager;
import net.miarma.hystuff.common.Location;
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 HomeManager homeManager;
    private LastPositionManager lastPositionManager;
    private PlayerDeathListener playerDeathListener;
    private static final HytaleLogger LOGGER = HytaleLogger.forEnclosingClass();

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

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

    @Override
    protected void start() {
        LOGGER.atInfo().log("HyStuff plugin starting...");
        this.getCommandRegistry().registerCommand(new HomeCommand(this.homeManager));
        this.getCommandRegistry().registerCommand(new SetHomeCommand(this.homeManager));
        this.getCommandRegistry().registerCommand(new BackCommand(this.lastPositionManager));
        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.homeManager != null) {
            this.homeManager.save();
            LOGGER.atInfo().log("Total homes saved: %d", this.homeManager.getHomeCount());
        }

        if (this.playerDeathListener != null) {
            EntityStore.REGISTRY.unregisterSystem(PlayerDeathListener.class);
        }
    }
}