package com.alterdekim.game.message;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;

public class SerializerUtils {

    public static List<Byte> primitiveToBytes(byte[] bytes) {
        List<Byte> result = new ArrayList<>();
        for( byte b : bytes ) {
            result.add(b);
        }
        return result;
    }

    public static byte[] bytesToPrimitive(List<Byte> bytes) {
        byte[] b = new byte[bytes.size()];
        for( int i = 0; i < bytes.size(); i++ ) {
            b[i] = bytes.get(i);
        }
        return b;
    }

    public static <T> String convertToString(T obj) {
        return switch (obj.getClass().getSimpleName()) {
            case "String" -> (String) obj;
            case "Integer", "Double", "Short", "Float", "Long", "Boolean", "Byte" -> String.valueOf(obj);
            default -> obj.toString();
        };
    }

    public static List<Field> getPrivateFields(Class<?> theClass) {
        List<Field> privateFields = new ArrayList<>();
        Field[] fields = theClass.getDeclaredFields();
        for(Field field : fields){
            if(Modifier.isPrivate(field.getModifiers())) {
                field.setAccessible(true);
                privateFields.add(field);
            }
        }
        return privateFields;
    }
}
