/*
 *   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.velocitypowered.api.event.connection.PluginMessageEvent;
import com.velocitypowered.api.proxy.Player;
import de.steamwar.velocitycore.listeners.PluginMessage;
import org.msgpack.core.MessagePack;
import org.msgpack.core.MessagePacker;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

public class Alpine {
    //https://github.com/alpine-client/alpine-client-api

    public static final String HANDSHAKE = "ac:handshake";
    public static final String PLAY = "ac:play";

    private final byte[] magicHandshakeResponse = new byte[] {0x3A, 0x3D}; //https://github.com/alpine-client/alpine-client-api/blob/master/src/main/java/com/alpineclient/plugin/listener/plugin/HandshakeListener.java#L19
    private final byte[] modulesPacket;

    public Alpine() {
        //https://github.com/alpine-client/alpine-client-api/blob/master/MODULES.md
        String[] disabledModules = new String[] {
                "armor_status", "cannon_playback", "cannon_view", "clear_water", "explosion_boxes", "fullbright",
                "hit_color", "inventory_tweaks", "low_hp_tint", "minimap", "perspective_mod", "potion_status",
                "reach_display", "schematica", "tnt_timer", "toggle_sneak_sprint"
        };

        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        try (MessagePacker packer = MessagePack.newDefaultPacker(stream)) {
            //https://github.com/alpine-client/alpine-client-api/blob/master/src/main/java/com/alpineclient/plugin/network/Packet.java#L103
            packer.packInt(0xB0);

            //https://github.com/alpine-client/alpine-client-api/blob/master/src/main/java/com/alpineclient/plugin/network/packet/PacketModules.java
            packer.packArrayHeader(disabledModules.length);
            for (String module : disabledModules) {
                packer.packString(module);
                packer.packBoolean(false); //https://github.com/alpine-client/alpine-client-api/blob/master/src/main/java/com/alpineclient/plugin/config/impl/GeneralConfig.java
            }
        } catch (IOException e) {
            throw new SecurityException(e);
        }

        modulesPacket = stream.toByteArray();
    }

    public void handleHandshakeMessage(PluginMessageEvent event) {
        Player player = (Player) event.getSource();
        PluginMessage.send(player, HANDSHAKE, magicHandshakeResponse);
        PluginMessage.send(player, PLAY, modulesPacket);
    }
}
