// Import platform-specific SDKs
import ATAndroidJS from "./Android/ATAndroidJS";
import ATiOSJS from "./iOS/ATiOSJS";

let isDebugLog = false;

// Define platform bridge initialization
const initPlatformBridge = (): any => {  
    if (cc.sys.os === cc.sys.OS_IOS) {           
        return ATiOSJS;
    } else if (cc.sys.os === cc.sys.OS_ANDROID) {
        return ATAndroidJS;
    }
};

// Initialize platform bridge
const platformBridge = initPlatformBridge();

// Define the ATSDK interface
interface IATSDK {
    kATUserLocationUnknown: number;
    kATUserLocationInEU: number;
    kATUserLocationOutOfEU: number;
    PERSONALIZED: number;
    NONPERSONALIZED: number;
    UNKNOWN: number;

    // General keys
    OS_VERSION_NAME: string;
    OS_VERSION_CODE: string;
    APP_PACKAGE_NAME: string;
    APP_VERSION_NAME: string;
    APP_VERSION_CODE: string;
    
    // Device info keys
    BRAND: string;
    MODEL: string;
    DEVICE_SCREEN_SIZE: string;
    MNC: string;
    MCC: string;

    LANGUAGE: string;
    TIMEZONE: string;
    USER_AGENT: string;
    ORIENTATION: string;
    NETWORK_TYPE: string;

    // Android-specific keys
    INSTALLER: string;
    ANDROID_ID: string;
    GAID: string;
    MAC: string;
    IMEI: string;
    OAID: string;
    
    // iOS-specific keys
    IDFA: string;
    IDFV: string;

    ATSDKListener: {
        userLocationCallback: ((userLocation: number) => void) | null;
        getUserLocationCallback: (userLocation: number) => void;
    };

    initSDK(appId: string, appKey: string): void;
    initCustomMap(customMap: Record<string, any>): void;
    setPlacementCustomMap(placementId: string, customMap: Record<string, any>): void;
    setGDPRLevel(level: number): void;
    getGDPRLevel(): number;
    getUserLocation(userLocationCallback: (userLocation: number) => void): void;
    showGDPRAuth(): void;
    setLogDebug(debug: boolean): void;
    printLog(msg: string): void;
    deniedUploadDeviceInfo(deniedInfo: string[]): void;
}

// Define ATSDK object
const ATSDK: IATSDK = {
    kATUserLocationUnknown: 0,
    kATUserLocationInEU: 1,
    kATUserLocationOutOfEU: 2,

    PERSONALIZED: 0,
    NONPERSONALIZED: 1,
    UNKNOWN: 2,

    // General keys
    OS_VERSION_NAME: "os_vn",
    OS_VERSION_CODE: "os_vc",
    APP_PACKAGE_NAME: "package_name",
    APP_VERSION_NAME: "app_vn",
    APP_VERSION_CODE: "app_vc",

    // Device info keys
    BRAND: "brand",
    MODEL: "model",
    DEVICE_SCREEN_SIZE: "screen",
    MNC: "mnc",
    MCC: "mcc",

    LANGUAGE: "language",
    TIMEZONE: "timezone",
    USER_AGENT: "ua",
    ORIENTATION: "orient",
    NETWORK_TYPE: "network_type",

    // Android-specific keys
    INSTALLER: "it_src",
    ANDROID_ID: "android_id",
    GAID: "gaid",
    MAC: "mac",
    IMEI: "imei",
    OAID: "oaid",
    
    // iOS-specific keys
    IDFA: "idfa",
    IDFV: "idfv",

    ATSDKListener: {
        userLocationCallback: null,

        getUserLocationCallback(userLocation: number) {
            this.userLocationCallback?.(userLocation);
        }
    },
    
    initSDK(appId: string, appKey: string) {
        if (platformBridge) {
            platformBridge.initSDK(appId, appKey);
        } else {
            cc.log("You must run on Android or iOS.");
        }
    },

    initCustomMap(customMap: Record<string, any>) {
        if (platformBridge) {
            if (customMap) {
                platformBridge.initCustomMap(JSON.stringify(customMap));
            }
        } else {
            cc.log("You must run on Android or iOS.");
        }
    },

    setPlacementCustomMap(placementId: string, customMap: Record<string, any>) {
        if (platformBridge) {
            if (customMap) {
                platformBridge.setPlacementCustomMap(placementId, JSON.stringify(customMap));
            }
        } else {
            cc.log("You must run on Android or iOS.");
        }
    },

    setGDPRLevel(level: number) {
        if (platformBridge) {
            platformBridge.setGDPRLevel(level);
        } else {
            cc.log("You must run on Android or iOS.");
        }
    },

    getGDPRLevel(): number {
        if (platformBridge) {
            return platformBridge.getGDPRLevel();
        } else {
            cc.log("You must run on Android or iOS.");
        }
        return this.UNKNOWN;
    },

    getUserLocation(userLocationCallback: (userLocation: number) => void) {
        this.ATSDKListener.userLocationCallback = userLocationCallback;
        if (platformBridge) {
            platformBridge.getUserLocation(GetUserLocationJsCallback);
        } else {
            cc.log("You must run on Android or iOS.");
        }
    },

    showGDPRAuth() {
        if (platformBridge) {
            platformBridge.showGDPRAuth();
        } else {
            cc.log("You must run on Android or iOS.");
        }
    },

    setLogDebug(debug: boolean) {
        isDebugLog = debug;
        if (platformBridge) {
            platformBridge.setLogDebug(debug);
        } else {
            cc.log("You must run on Android or iOS.");
        }
    },

    printLog(msg: string) {
        if (msg && isDebugLog && platformBridge) {
            platformBridge.printJsLog(msg); 
        }
    },

    deniedUploadDeviceInfo(deniedInfo: string[]) {
        if (platformBridge) {
            if (deniedInfo && deniedInfo.length > 0) {
                const deniedInfoString = deniedInfo.join(",");
                cc.log("test__" + deniedInfoString);
                platformBridge.deniedUploadDeviceInfo(deniedInfoString);
            }
        } else {
            cc.log("You must run on Android or iOS.");
        }
    }
};

// Define callback constant
const GetUserLocationJsCallback = "ATJSSDK.ATSDKListener.getUserLocationCallback";

// Export the SDK to the global window object
(window as any).ATJSSDK = ATSDK;
export default ATSDK;