/*
 *   This file is a part of the SteamWar software.
 *
 *   Copyright (C) 2024  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.protobuf.Any;
import com.google.protobuf.InvalidProtocolBufferException;
import com.google.protobuf.Value;
import com.lunarclient.apollo.configurable.v1.ConfigurableSettings;
import com.lunarclient.apollo.configurable.v1.OverrideConfigurableSettingsMessage;
import com.lunarclient.apollo.player.v1.ModMessage;
import com.lunarclient.apollo.player.v1.PlayerHandshakeMessage;
import com.velocitypowered.api.event.connection.PluginMessageEvent;
import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.messages.MinecraftChannelIdentifier;
import de.steamwar.sql.Mod;
import de.steamwar.velocitycore.VelocityCore;

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

public class Lunar {
    // https://lunarclient.dev/apollo/introduction
    // https://github.com/LunarClient/Apollo

    public static final String CHANNEL = "lunar:apollo";

    private final byte[] packet;

    public Lunar() {
        ConfigurableSettings.Builder builder = ConfigurableSettings.newBuilder()
                .setApolloModule("mod_setting")
                .setEnable(true);

        for(String property : List.of(
                "freelook.enabled", "hypixel-mod.enabled", "minimap.enabled", "nametag.enabled", "replaymod.enabled",
                "team-view.enabled", "tnt-countdown.enabled", "toggle-sneak.toggle-sneak-container"
        ))
            builder.putProperties(property, Value.newBuilder().setBoolValue(false).build());

        packet = Any.pack(OverrideConfigurableSettingsMessage.newBuilder().addConfigurableSettings(builder).build()).toByteArray();
    }

    public void sendRestrictions(Player player) {
        player.sendPluginMessage(MinecraftChannelIdentifier.from(CHANNEL), packet);
    }

    public void handlePluginMessage(PluginMessageEvent event) {
        try {
            Any packet = Any.parseFrom(event.getData());
            if(packet.is(PlayerHandshakeMessage.class))
                handle((Player) event.getSource(), packet.unpack(PlayerHandshakeMessage.class));
        } catch (InvalidProtocolBufferException e) {
            throw new SecurityException(e);
        }
    }

    private void handle(Player player, PlayerHandshakeMessage packet) {
        List<Mod> mods = new ArrayList<>();

        for(ModMessage mod : packet.getInstalledModsList()) {
            switch(mod.getType()) {
                case TYPE_FABRIC_INTERNAL, TYPE_FORGE_INTERNAL:
                    // Controlled with ModSettings
                    break;
                case TYPE_FABRIC_EXTERNAL:
                    mods.add(Mod.getOrCreate(mod.getName(), Mod.Platform.FABRIC));
                    break;
                case TYPE_FORGE_EXTERNAL:
                    mods.add(Mod.getOrCreate(mod.getName(), Mod.Platform.FORGE));
                    break;
                default:
                    VelocityCore.getLogger().log(Level.INFO, () -> player.getUsername() + " uses Lunar mod with unknown type " + mod);
                    break;
            }
        }

        ModUtils.handleMods(player, mods);
    }
}
