package net.miarma.hystuff;

import com.hypixel.hytale.component.ComponentType;
import com.hypixel.hytale.logger.HytaleLogger;
import com.hypixel.hytale.server.core.plugin.JavaPlugin;
import com.hypixel.hytale.server.core.plugin.JavaPluginInit;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import net.miarma.hystuff.commands.misc.BackCommand;
import net.miarma.hystuff.commands.misc.HomeCommand;
import net.miarma.hystuff.commands.misc.PlaytimeCommand;
import net.miarma.hystuff.commands.misc.SetHomeCommand;
import net.miarma.hystuff.commands.teleport.TpAcceptCommand;
import net.miarma.hystuff.commands.teleport.TpDenyCommand;
import net.miarma.hystuff.commands.teleport.TpaCommand;
import net.miarma.hystuff.commands.teleport.TpaHereCommand;
import net.miarma.hystuff.component.HomeComponent;
import net.miarma.hystuff.component.LastPositionComponent;
import net.miarma.hystuff.component.PlaytimeComponent;
import net.miarma.hystuff.component.TpaRequestComponent;
import net.miarma.hystuff.system.PlayerDeathSystem;
import net.miarma.hystuff.system.TpaCleanupSystem;

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 PlayerDeathSystem playerDeathSystem;
    private TpaCleanupSystem tpaCleanupSystem;
    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 static ComponentType<EntityStore, TpaRequestComponent> TPA_REQUEST_COMPONENT_TYPE;
    public static ComponentType<EntityStore, PlaytimeComponent> PLAYTIME_COMPONENT_TYPE;

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

    @Override
    protected void setup() {
        LOGGER.atInfo().log("------ HYSTUFF PLUGIN SETUP BEGIN ------");
        this.playerDeathSystem = new PlayerDeathSystem();
        this.tpaCleanupSystem = new TpaCleanupSystem();
        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);

        TPA_REQUEST_COMPONENT_TYPE = this.getEntityStoreRegistry()
                .registerComponent(TpaRequestComponent.class, "hystuff:tpa_request", TpaRequestComponent.CODEC);

        PLAYTIME_COMPONENT_TYPE = this.getEntityStoreRegistry()
                .registerComponent(PlaytimeComponent.class, "hystuff:playtime", PlaytimeComponent.CODEC);

        this.getCommandRegistry().registerCommand(new HomeCommand());
        this.getCommandRegistry().registerCommand(new SetHomeCommand());
        this.getCommandRegistry().registerCommand(new BackCommand());
        this.getCommandRegistry().registerCommand(new TpaCommand());
        this.getCommandRegistry().registerCommand(new TpaHereCommand());
        this.getCommandRegistry().registerCommand(new TpAcceptCommand());
        this.getCommandRegistry().registerCommand(new TpDenyCommand());
        this.getCommandRegistry().registerCommand(new PlaytimeCommand());

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

        if (this.tpaCleanupSystem != null) {
            try {
                EntityStore.REGISTRY.registerSystem(this.tpaCleanupSystem);
                LOGGER.atInfo().log("TPA Cleanup system enabled.");
            } catch (Exception e) {
                LOGGER.atWarning().log("Could not register TPA Cleanup system: " + e.getMessage());
            }
        }

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

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