declare const jsb: any;
const classJavaName = "org/cocos2dx/javascript/cocosjs/ATJSBridge";
const classJavaParamName = "com/cocos/service/SDKWrapper";

const classJavaParamNameForApp = "com/cocos/game/AppActivity";

const Cocos2dxHelper = "org/cocos2dx/lib/Cocos2dxHelper";
/**
 * 正式的
 */
const placementID = "n1g03bq8qkeckn";
/**
 * 测试专用n6729e95f588aa    n66f7c61b71d4c   
 */
const testPlacementID = "n6729e95f588aa";
/**
 * 打砖块的
 */
const testPlacementIDForDzk = "n67122e8932a3d";

interface ATAndroidJS {
    getDeviceModel(): string;
    getSystemVersion(): string;
    getDeviceManufacturer(): string;
    getCurrentLocale(): string;
    getTimeZone(): string;
    getCountry(): string;
    getMediaType(): string;
    getDeviceId(): string;
    getVersionCode(): string;
    getVersionName(): string;
    getPackageName(): string;
    getAppInstall(): string;
    getAppInstandId(): string;
    printJsLog(msg: string): void;
    initSDK(appid: string, appkey: string): void;
    initCustomMap(customMap: string): void;
    setPlacementCustomMap(placementId: string, customMap: string): void;
    setGDPRLevel(level: number): void;
    getGDPRLevel(): number;
    getUserLocation(callbackMethod: string): void;
    showGDPRAuth(): void;
    setLogDebug(debug: boolean): void;
    deniedUploadDeviceInfo(deniedInfo: string): void;
    allInfo(): string;
    makePostRequestWithXhr: (data: string) => void;
    getLanguage(): string;
    getInstanceId(): string;
    getInfomation(): string;
    getAdId(): string;
    getDeviceUserId(): string;
    getPlacementId(): string;

    getTestFormApp(): string;
    getTestFormSdk(): string;
}



const ATAndroidJS: ATAndroidJS = {
    getDeviceModel: function () {
        return jsb.reflection.callStaticMethod(classJavaParamName, "getDeviceModel", "()Ljava/lang/String;");
    },
    getSystemVersion: function () {
        return jsb.reflection.callStaticMethod(classJavaParamName, "getOSVersion", "()Ljava/lang/String;");
    },
    getDeviceManufacturer: function () {
        return jsb.reflection.callStaticMethod(classJavaParamName, "getDeviceManufacturer", "()Ljava/lang/String;");
    },
    getCurrentLocale: function () {
        return jsb.reflection.callStaticMethod(classJavaParamName, "getCurrentLocale", "()Ljava/lang/String;");
    },
    getTimeZone: function () {
        return jsb.reflection.callStaticMethod(classJavaParamName, "getTimeZone", "()Ljava/lang/String;");
    },
    getCountry: function () {
        return jsb.reflection.callStaticMethod(classJavaParamName, "getCountry", "()Ljava/lang/String;");
    },
    getMediaType: function () {
        return jsb.reflection.callStaticMethod(classJavaParamName, "getMediaType", "()Ljava/lang/String;");
    },
    getDeviceId: function () {
        return jsb.reflection.callStaticMethod(classJavaParamName, "getAndroidId", "()Ljava/lang/String;");
    },
    getVersionCode: function () {
        return jsb.reflection.callStaticMethod(classJavaParamName, "getVersionCode", "()Ljava/lang/String;");
    },
    getVersionName: function () {
        return jsb.reflection.callStaticMethod(classJavaParamName, "getVersionName", "()Ljava/lang/String;");
    },
    getPackageName: function () {
        return jsb.reflection.callStaticMethod(classJavaParamName, "getPackageName", "()Ljava/lang/String;");
    },
    getAppInstall: function () {
        return jsb.reflection.callStaticMethod(classJavaParamName, "getAppInstall", "()Ljava/lang/String;");
    },
    getAppInstandId: function () {
        // return jsb.reflection.callStaticMethod(classJavaParamName, "getInstanceId", "()Ljava/lang/String;");
        return jsb.reflection.callStaticMethod(classJavaParamName, "getInstanceIds", "()Ljava/lang/String;");
    },
    printJsLog: function (msg: string) {
        if (msg !== undefined && msg !== null) {
            jsb.reflection.callStaticMethod("android/util/Log", "i", "(Ljava/lang/String;Ljava/lang/String;)I", "AT-Cocos-JS", msg);
        }
    },
    initSDK: function (appid: string, appkey: string) {
        console.log('zh:jsb=' + jsb)
        jsb.reflection.callStaticMethod(classJavaParamName, "initSDK", "(Ljava/lang/String;Ljava/lang/String;)V", appid, appkey);
    },
    initCustomMap: function (customMap: string) {
        jsb.reflection.callStaticMethod(classJavaParamName, "initCustomMap", "(Ljava/lang/String;)V", customMap);
    },
    setPlacementCustomMap: function (placementId: string, customMap: string) {
        jsb.reflection.callStaticMethod(classJavaParamName, "setPlacementCustomMap", "(Ljava/lang/String;Ljava/lang/String;)V", placementId, customMap);
    },
    setGDPRLevel: function (level: number) {
        jsb.reflection.callStaticMethod(classJavaParamName, "setGDPRLevel", "(I)V", level);
    },
    getGDPRLevel: function () {
        return jsb.reflection.callStaticMethod(classJavaParamName, "getGDPRLevel", "()I");
    },
    getUserLocation: function (callbackMethod: string) {
        jsb.reflection.callStaticMethod(classJavaParamName, "getUserLocation", "(Ljava/lang/String;)V", callbackMethod);
    },
    showGDPRAuth: function () {
        jsb.reflection.callStaticMethod(classJavaParamName, "showGDPRAuth", "()V");
    },
    setLogDebug: function (debug: boolean) {
        jsb.reflection.callStaticMethod(classJavaParamName, "setLogDebug", "(Z)V", debug);
    },
    deniedUploadDeviceInfo: function (deniedInfo: string) {
        jsb.reflection.callStaticMethod(classJavaParamName, "deniedUploadDeviceInfo", "(Ljava/lang/String;)V", deniedInfo);
    },
    getLanguage: function () {
        return jsb.reflection.callStaticMethod(classJavaParamName, "getLanguage", "()Ljava/lang/String;");
    },
    getInstanceId: function () {
        return jsb.reflection.callStaticMethod(classJavaParamName, "getInstanceIds", "()Ljava/lang/String;");
    },
    getInfomation: function () {
        return jsb.reflection.callStaticMethod(classJavaParamName, "getInfos", "()Ljava/lang/String;");
    },

    getAdId: function () {
        return jsb.reflection.callStaticMethod(classJavaParamName, "getAdId", "()Ljava/lang/String;");
    },
    getPlacementId: function () {
        // return "n66f7c61b71d4c";
        return testPlacementIDForDzk;
    },

    getTestFormApp: function () {
        return jsb.reflection.callStaticMethod(classJavaParamNameForApp, "getTestFormApp", "()Ljava/lang/String;");
    },

    getTestFormSdk: function () {
        return jsb.reflection.callStaticMethod(classJavaParamName, "getTestFormSdk", "()Ljava/lang/String;");
    },

    /**
     * 本类封装了获取设备所有信息的方法，返回一个json字符串
     * @returns json string
     */
    allInfo: function () {
        const info = {
            systemVersion: this.getSystemVersion(),
            deviceModel: this.getDeviceModel(),
            deviceManufacturer: this.getDeviceManufacturer(),
            country: this.getCountry(),
            timeZone: this.getTimeZone(),
            device: this.getDeviceId(),
            platform: 'Android',
            language: this.getLanguage(),
            versionCode: this.getVersionCode(),
            versionName: this.getVersionName(),
            packageName: this.getPackageName(),
            adId: this.getAdId(),
            information: this.getInfomation(),
            appInstantsId: this.getAppInstandId(),
            mediaType: this.getMediaType(),
            userIp: '',
            gameId: "22"
        };

        return JSON.stringify(info);
    },
    getDeviceUserId: function () {
        return this.getDeviceId()
    },


    /**
     * 发送HTTP数据到服务器
     * @param data 
     */
    makePostRequestWithXhr: function (data: string) {
        var url = "https://screwapi.gamefunserver.com/api/game/postData";
        var xhr = new XMLHttpRequest();
        xhr.open('POST', url, true); // 第三个参数表示异步执行
        xhr.setRequestHeader('Content-Type', 'application/json');
        // 定义请求成功时的回调函数
        xhr.onload = function () {
            if (xhr.status >= 200 && xhr.status < 300) {
                console.log('请求成功:', xhr.responseText);
                //ATSDK.printLog("zhangsan:请求成功="+xhr.responseText);
            } else {
                console.error('请求失败: ' + xhr.status);
                // ATSDK.printLog("zhangsan:请求失败="+xhr.status);
            }
        };
        xhr.onerror = function () {
            console.error('网络请求发生错误');
            // ATSDK.printLog("zhangsan:网络请求发生错误");    
        };
        // var data = JSON.stringify({ key1: 'value1', key2: 'value2' });
        xhr.send(data);
    }

};

export default ATAndroidJS;
