/*
    This file is a part of the SteamWar software.

    Copyright (C) 2020  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 org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.SkullMeta;

import java.util.HashMap;
import java.util.Map;

public class FlatteningWrapper8 implements FlatteningWrapper.IFlatteningWrapper {

    private static final Reflection.Field<String> scoreboardName = Reflection.getField(FlatteningWrapper.scoreboardObjective, String.class, 1);
    private static final Class<?> scoreActionEnum = Reflection.getClass("net.minecraft.PacketPlayOutScoreboardScore$EnumScoreboardAction");
    private static final Reflection.Field<?> scoreAction = Reflection.getField(FlatteningWrapper.scoreboardScore, scoreActionEnum, 0);
    private static final Object scoreActionChange = scoreActionEnum.getEnumConstants()[0];

    @Override
    public void setScoreboardTitle(Object packet, String title) {
        scoreboardName.set(packet, title);
    }

    @Override
    public void setScoreAction(Object packet) {
        scoreAction.set(packet, scoreActionChange);
    }

    @Override
    public Material getMaterial(String material) {
        try{
            return Material.valueOf(material);
        }catch(IllegalArgumentException e){
            return Material.STONE;
        }
    }

    @Override
    public Material getDye(int colorCode) {
        return Material.INK_SACK;
    }

    @Override
    public ItemStack setSkullOwner(String player) {
        ItemStack head = new ItemStack(Material.SKULL_ITEM, 1, (short) 3);
        SkullMeta headmeta = (SkullMeta) head.getItemMeta();
        headmeta.setOwner(player.startsWith(".") ? player.substring(1) : player);
        headmeta.setDisplayName(player);
        head.setItemMeta(headmeta);
        return head;
    }

    @Override
    public Object getPose(FlatteningWrapper.EntityPose pose) {
        return Byte.valueOf((byte)(pose == FlatteningWrapper.EntityPose.SNEAKING ? 2 : 0));
    }

    private static final Class<?> dataWatcher = Reflection.getClass("net.minecraft.DataWatcher");
    private static final Class<?> namedSpawnPacket = Reflection.getClass("net.minecraft.network.protocol.game.ClientboundAddPlayerPacket");
    private static final Reflection.Field<?> namedSpawnDataWatcher = Reflection.getField(namedSpawnPacket, dataWatcher, 0);
    private static final Class<?> entity = Reflection.getClass("net.minecraft.Entity");
    private static final Reflection.Constructor dataWatcherConstructor = Reflection.getConstructor(dataWatcher, entity);
    @Override
    public void setNamedSpawnPacketDataWatcher(Object packet) {
        namedSpawnDataWatcher.set(packet, dataWatcherConstructor.invoke((Object) null));
    }

    @Override
    public Object formatDisplayName(String displayName) {
        return displayName != null ? displayName : "";
    }


    private static final Reflection.Field<?> spawnType = Reflection.getField(ProtocolWrapper.spawnPacket, int.class, Core.getVersion() > 8 ? 6 : 9);
    private static final Reflection.Field<Integer> spawnLivingType = Reflection.getField(ProtocolWrapper.spawnLivingPacket, int.class, 1);
    private static final Map<EntityType, Object> types = new HashMap<>();
    static {
        types.put(TrickyTrialsWrapper.impl.getTntEntityType(), 50);
        types.put(EntityType.ARMOR_STAND, 30);
        types.put(EntityType.ARROW, 60);
        types.put(EntityType.FIREBALL, 63);
        types.put(EntityType.ITEM_FRAME, 18);
        types.put(EntityType.FALLING_BLOCK, 21);
    }
    @Override
    public void setSpawnPacketType(Object packet, EntityType type) {
        (type.isAlive() ? spawnLivingType : spawnType).set(packet, types.get(type));
    }

    @Override
    public int getViewDistance(Player player) {
        return 10;
    }

    private static final Reflection.Method save = Reflection.getMethod("org.bukkit.craftbukkit.CraftWorld", "save", boolean.class);
    @Override
    public void syncSave(World world) {
        save.invoke(world, true);
    }
}
