/*
 *   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.bausystem.configplayer.serializer;

import org.bukkit.configuration.serialization.ConfigurationSerializable;
import org.bukkit.configuration.serialization.ConfigurationSerialization;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import yapion.hierarchy.api.groups.YAPIONAnyType;
import yapion.hierarchy.types.YAPIONObject;
import yapion.serializing.api.SerializerObject;
import yapion.serializing.data.DeserializeData;
import yapion.serializing.data.SerializeData;

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

import static yapion.utils.IdentifierUtils.TYPE_IDENTIFIER;

public class ConfigurationSerializableSerializer extends SerializerObject<ConfigurationSerializable> {

    @Override
    public Class<ConfigurationSerializable> type() {
        return ConfigurationSerializable.class;
    }

    @Override
    public boolean isInterface() {
        return true;
    }

    @Override
    public YAPIONObject serialize(SerializeData<ConfigurationSerializable> serializeData) {
        YAPIONObject yapionObject = new YAPIONObject();
        yapionObject.add(TYPE_IDENTIFIER, serializeData.object.getClass().getTypeName());
        if (serializeData.object instanceof ItemStack) {
            yapionObject.add(TYPE_IDENTIFIER, ItemStack.class.getTypeName());
        }
        if (serializeData.object instanceof ItemMeta) {
            yapionObject.add(TYPE_IDENTIFIER, ItemMeta.class.getTypeName());
        }
        Map<String, Object> serializeDataMap = serializeData.object.serialize();
        serializeDataMap.forEach((s, o) -> {
            YAPIONAnyType yapionAnyType = serializeData.serialize(o);
            if (yapionAnyType instanceof YAPIONObject) {
                YAPIONObject object = (YAPIONObject) yapionAnyType;
                if (object.containsKey(TYPE_IDENTIFIER) && object.getPlainValue(TYPE_IDENTIFIER).equals("com.google.common.collect.RegularImmutableList")) {
                    object.put(TYPE_IDENTIFIER, ArrayList.class.getTypeName());
                }
            }
            yapionObject.add(s, yapionAnyType);
        });
        return yapionObject;
    }

    @Override
    public ConfigurationSerializable deserialize(DeserializeData<YAPIONObject> deserializeData) {
        Map<String, Object> deserializeDataMap = new HashMap<>();
        deserializeData.object.forEach((s, yapionAnyType) -> {
            if (s.equals(TYPE_IDENTIFIER)) {
                if (yapionAnyType.toString().equals("(org.bukkit.inventory.meta.ItemMeta)")) {
                    deserializeDataMap.put("==", "ItemMeta");
                } else {
                    deserializeDataMap.put("==", deserializeData.deserialize(yapionAnyType));
                }
                return;
            }
            deserializeDataMap.put(s, deserializeData.deserialize(yapionAnyType));
        });
        return ConfigurationSerialization.deserializeObject(deserializeDataMap);
    }
}