/*
 *   This file is a part of the SteamWar software.
 *
 *   Copyright (C) 2025 SteamWar.de-Serverteam
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Affero General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *   GNU Affero General Public License for more details.
 *
 *   You should have received a copy of the GNU Affero General Public License
 *   along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package de.steamwar.velocitycore.mods;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.velocitypowered.api.event.connection.PluginMessageEvent;
import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.proxy.protocol.ProtocolUtils;
import de.steamwar.velocitycore.VelocityCore;
import de.steamwar.velocitycore.listeners.PluginMessage;
import de.steamwar.sql.Mod;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;

public class LabyMod {
    // https://docs.labymod.net/pages/server/introduction/
    // https://github.com/LabyMod/labymod-server-api
    // https://dl.labymod.net/addons.json

    private final byte[] gameInfoPacket;
    public LabyMod() {
        gameInfoPacket = PluginMessage.genBufPacket(buf -> {
            ProtocolUtils.writeString(buf, "discord_rpc");

            JsonObject json = new JsonObject();
            json.addProperty("hasGame", true);
            json.addProperty("game_mode", "steamwar.de");
            json.addProperty("game_startTime", 0);
            json.addProperty("game_endTime", 0);
            ProtocolUtils.writeString(buf, json.toString());
        });
    }

    public void handlePluginMessage(PluginMessageEvent event) {
        Player player = (Player) event.getSource();
        player.sendPluginMessage(event.getIdentifier(), gameInfoPacket);

        ByteBuf buf = Unpooled.wrappedBuffer(event.getData());
        String purpose = ProtocolUtils.readString(buf);
        if(!"INFO".equals(purpose))
            return;

        JsonObject message = JsonParser.parseString(ProtocolUtils.readString(buf)).getAsJsonObject();
        List<Mod> mods = new LinkedList<>();

        if(message.has("addons")) {
            for(JsonElement element : message.getAsJsonArray("addons")) {
                JsonObject addon = element.getAsJsonObject();
                mods.add(Mod.getOrCreate(addon.get("name").getAsString(), Mod.Platform.LABYMOD));
            }
        }

        if(message.has("mods")) {
            VelocityCore.getLogger().log(Level.WARNING, () -> "LabyMod External Mods for debugging: " + message.getAsJsonArray("mods"));
            //for(JsonElement element : message.getAsJsonArray("mods")) {
            //    JsonObject addon = element.getAsJsonObject();
            //    //TODO observe: FORGE and FABRIC mods available, do they always and with .jar? (would equal new mod platform)
            //    //mods.add(Mod.getOrCreate(addon.get("name").getAsString().replace(".jar", ""), Mod.Platform.FORGE));
            //}
        }

        ModUtils.handleMods(player, mods);
    }
}
