import { DeviceEventEmitter, NativeEventEmitter, Platform } from 'react-native';
import SmartechNudges from './specs/NativeSmartechNudges';

// TurboModule is working correctly for new architecture

// Try to use TurboModule first (new arch), fallback to legacy modules (old arch)
// React Native automatically chooses newarch/oldarch modules based on architecture
const HanselRnNative = SmartechNudges;
const HanselUserRnNative = SmartechNudges;
const HanselConfigsRnNative = SmartechNudges;
const HanselTrackerRnNative = SmartechNudges;
const HanselLogsRnNative = SmartechNudges;

// Create event emitter for cross-platform events
const HanselEventEmitter = Platform.select({
    ios: new NativeEventEmitter(SmartechNudges),
    android: DeviceEventEmitter
});

// Store event subscriptions for cleanup
const eventMap: { [key: string]: any } = {};

// ===== HanselRn Module =====
export const HanselRn = {
    getInteractionMaps: function (callback: (maps: any) => void) {
        HanselRnNative.getInteractionMaps(callback);
    },

    onBackButtonPressed: function (): boolean {
        return HanselRnNative.onBackButtonPressed();
    },

    onSetScreen: function (screenName: string) {
        HanselRnNative.onSetScreen(screenName);
    },

    onUnsetScreen: function () {
        HanselRnNative.onUnsetScreen();
    },

    showToast: function (text: string, longDuration: boolean) {
        HanselRnNative.showToast(text, longDuration);
    },

    setTypeface: function (fontFileName: string) {
        HanselRnNative.setTypeface(fontFileName);
    },

    setAppFont: function (fontName: string) {
        HanselRnNative.setAppFont(fontName);
    },

    setNativeID: function () {
        HanselRnNative.setNativeID();
    }

};

// ===== HanselUser Module =====
export const HanselUserRn = {
    putStringAttribute: function (key: string, value: string) {
        HanselUserRnNative.putStringAttribute(key, value);
    },

    putDoubleAttribute: function (key: string, value: number) {
        HanselUserRnNative.putDoubleAttribute(key, value);
    },

    putBooleanAttribute: function (key: string, value: boolean) {
        HanselUserRnNative.putBooleanAttribute(key, value);
    },

    putPrivateStringAttribute: function (key: string, value: string) {
        HanselUserRnNative.putPrivateStringAttribute(key, value);
    },

    putPrivateDoubleAttribute: function (key: string, value: number) {
        HanselUserRnNative.putPrivateDoubleAttribute(key, value);
    },

    putPrivateBooleanAttribute: function (key: string, value: boolean) {
        HanselUserRnNative.putPrivateBooleanAttribute(key, value);
    },

    setUserId: function (userId: string) {
        HanselUserRnNative.setUserId(userId);
    },

    clearAttribute: function (key: string) {
        HanselUserRnNative.clearAttribute(key);
    },

    clear: function () {
        HanselUserRnNative.clear();
    }
};

// ===== HanselConfigs Module =====
export const HanselConfigsRn = {
    getString: function (configName: string, fallbackValue: string, callback: (value: string) => void) {
        HanselConfigsRnNative.getString(configName, fallbackValue, callback);
    },

    getBoolean: function (configName: string, fallbackValue: boolean, callback: (value: boolean) => void) {
        HanselConfigsRnNative.getBoolean(configName, fallbackValue, callback);
    },

    getDouble: function (configName: string, fallbackValue: number, callback: (value: number) => void) {
        HanselConfigsRnNative.getDouble(configName, fallbackValue, callback);
    },

    getList: function (configName: string, defaultValue: any[], callback: (value: any[]) => void) {
        HanselConfigsRnNative.getList(configName, defaultValue, callback);
    },

    getJSONArray: function (configName: string, defaultValue: any[], callback: (value: any[]) => void) {
        HanselConfigsRnNative.getJSONArray(configName, defaultValue, callback);
    },

    getJSONObject: function (configName: string, defaultValue: any, callback: (value: any) => void) {
        HanselConfigsRnNative.getJSONObject(configName, defaultValue, callback);
    }
};

// ===== HanselTracker Module =====
export const HanselTrackerRn = {
    logEvent: function (eventName: string, vendor: string, hanselProperties: any, callback: (result: any) => void) {
        HanselTrackerRnNative.logEvent(eventName, vendor, hanselProperties, callback);
    },

    getHanselData: function (eventName: string, vendor: string, properties: any, callback: (result: any) => void) {
        HanselTrackerRnNative.getHanselData(eventName, vendor, properties, callback);
    },

    isUsedInMap: function (event: string, vendor: string, properties: any, callback: (result: boolean) => void) {
        HanselTrackerRnNative.isUsedInMap(event, vendor, properties, callback);
    },

    registerHanselTrackerListener: function () {
        HanselTrackerRnNative.registerHanselTrackerListener();
    },

    registerHanselActionListenerWithActionName: function (actionName: string) {
        HanselTrackerRnNative.registerHanselActionListenerWithActionName(actionName);
    },

    registerHanselDeeplinkListener: function () {
        HanselTrackerRnNative.registerHanselDeeplinkListener();
    },

    deRegisterListener: function () {
        HanselTrackerRnNative.deRegisterListener();
    },

    removeHanselActionListenerForAction: function (actionName: string) {
        HanselTrackerRnNative.removeHanselActionListenerForAction(actionName);
    },

    // Event listener methods
    addListener: function (eventName: string, handler: (data: any) => void) {
        if (HanselEventEmitter) {
            const subscription = HanselEventEmitter.addListener(eventName, handler);
            eventMap[eventName] = subscription;
            return subscription;
        }
        return undefined;
    },

    removeListener: function (eventName: string) {
        if (eventMap[eventName]) {
            eventMap[eventName].remove();
            delete eventMap[eventName];
        }
    }
};

// ===== HanselLogs Module =====
export const HanselLogs = {
    e: function (tag: string, message: string) {
        HanselLogsRnNative.e(tag, message);
    }
};

// Export everything as default for backwards compatibility
const SmartechNudgesReact = {
    HanselRn,
    HanselUserRn,
    HanselConfigsRn,
    HanselTrackerRn,
    HanselLogs
};

export default SmartechNudgesReact;