package com.castlabs.reactnative.utils;

import android.os.Bundle;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.bridge.WritableArray;

import java.util.ArrayList;
import java.util.Set;

public class BundleConverter {

    // Convert a Bundle into a WritableMap
    public static WritableMap fromBundle(Bundle bundle) {
        WritableMap map = Arguments.createMap();
        if (bundle == null) return map;

        Set<String> keys = bundle.keySet();
        for (String key : keys) {
            Object value = bundle.get(key);

            if (value == null) {
                map.putNull(key);
            } else if (value instanceof Boolean) {
                map.putBoolean(key, (Boolean) value);
            } else if (value instanceof Integer) {
                map.putInt(key, (Integer) value);
            } else if (value instanceof Double) {
                map.putDouble(key, (Double) value);
            } else if (value instanceof Float) {
                map.putDouble(key, ((Float) value).doubleValue());
            } else if (value instanceof Long) {
                // React Native doesn't have a 'long' type; use double
                map.putDouble(key, ((Long) value).doubleValue());
            } else if (value instanceof String) {
                map.putString(key, (String) value);
            } else if (value instanceof Bundle) {
                map.putMap(key, fromBundle((Bundle) value));
            } else if (value instanceof ArrayList) {
                map.putArray(key, fromArrayList((ArrayList<?>) value));
            } else {
                // Fallback: convert to string
                map.putString(key, value.toString());
            }
        }
        return map;
    }

    // Helper: Convert ArrayList to WritableArray
    private static WritableArray fromArrayList(ArrayList<?> list) {
        WritableArray array = Arguments.createArray();
        for (Object item : list) {
            if (item == null) {
                array.pushNull();
            } else if (item instanceof Boolean) {
                array.pushBoolean((Boolean) item);
            } else if (item instanceof Integer) {
                array.pushInt((Integer) item);
            } else if (item instanceof Double) {
                array.pushDouble((Double) item);
            } else if (item instanceof Float) {
                array.pushDouble(((Float) item).doubleValue());
            } else if (item instanceof Long) {
                array.pushDouble(((Long) item).doubleValue());
            } else if (item instanceof String) {
                array.pushString((String) item);
            } else if (item instanceof Bundle) {
                array.pushMap(fromBundle((Bundle) item));
            } else if (item instanceof ArrayList) {
                array.pushArray(fromArrayList((ArrayList<?>) item));
            } else {
                array.pushString(item.toString());
            }
        }
        return array;
    }
}
