package com.bsphp.login;

import com.google.gson.JsonElement;

public final class BSPHPAPIResult {
    public final Object data;
    public final Integer code;

    public BSPHPAPIResult(Object data, Integer code) {
        this.data = data;
        this.code = code;
    }

    public String message() {
        if (data == null) return "";
        if (data instanceof String s) return s;
        if (data instanceof JsonElement je) {
            if (je.isJsonPrimitive() && je.getAsJsonPrimitive().isString()) {
                return je.getAsString();
            }
            if (je.isJsonArray()) {
                StringBuilder sb = new StringBuilder();
                var arr = je.getAsJsonArray();
                for (int i = 0; i < arr.size(); i++) {
                    if (i > 0) sb.append('\n');
                    sb.append(arr.get(i));
                }
                return sb.toString();
            }
            return je.toString();
        }
        return String.valueOf(data);
    }
}
