/*
 *    This file is a part of the SteamWar software.
 *
 *    Copyright (C) 2022  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.core;

import de.steamwar.Reflection;

import java.util.ArrayList;
import java.util.List;

public class ChatWrapper8 implements ChatWrapper {

    private static final Reflection.Constructor chatComponentConstructor = Reflection.getConstructor(Reflection.getClass("net.minecraft.network.chat.ChatComponentText"), String.class);
    @Override
    public Object stringToChatComponent(String text) {
        return chatComponentConstructor.invoke(text);
    }

    private static final Class<?> metadataPacket = Reflection.getClass("net.minecraft.network.protocol.game.ClientboundSetEntityDataPacket");
    private static final Reflection.Field<Integer> metadataEntity = Reflection.getField(metadataPacket, int.class, 0);
    private static final Reflection.Field<List> metadataMetadata = Reflection.getField(metadataPacket, List.class, 0);
    @Override
    public Object getDataWatcherPacket(int entityId, Object... dataWatcherKeyValues) {
        Object packet = Reflection.newInstance(metadataPacket);
        metadataEntity.set(packet, entityId);

        ArrayList<Object> nativeWatchers = new ArrayList<>(1);
        for(int i = 0; i < dataWatcherKeyValues.length; i+=2) {
            nativeWatchers.add(BountifulWrapper.impl.getDataWatcherItem(dataWatcherKeyValues[i], dataWatcherKeyValues[i+1]));
        }
        metadataMetadata.set(packet, nativeWatchers);

        return packet;
    }
}
