/**
 * 
 * @param {number} num 
 * @param {number} radix 
 * @returns 
 */
export const formatNum = (num, radix = 2) => {
    if (!num) return 0
    if (isNaN(num)) return 0
    const numStr = num.toFixed(radix)
    for (let i = numStr.length - 1; i > 0; i--) {
        if (numStr[i] !== '0') {
            if (numStr[i] === '.') {
                return numStr.slice(0, i)
            }
            return numStr.slice(0, i + 1)
        }
    }
    return numStr
}

export const versionCompare = (v1, v2) => {
    if (!v1) return false
    if (!v2) return false
    v1 = v1.split('.')
    v2 = v2.split('.')
    const len = Math.max(v1.length, v2.length)

    while (v1.length < len) {
        v1.push('0')
    }
    while (v2.length < len) {
        v2.push('0')
    }
    for (let i = 0; i < len; i++) {
        const num1 = parseInt(v1[i])
        const num2 = parseInt(v2[i])
        if (num1 > num2) {
            return 1
        } else if (num1 < num2) {
            return -1
        }
    }
}

export const formatDate = (data) => {
    const date = new Date(data)
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
    return `${year}-${month.toString().padStart(2, '0')}-${day.toString().padStart(2, '0')}`
}


// export function getGoogleAdId() {
//     return new Promise((resolve, reject) => {
//         console.log("getGoogleAdId plus:", plus.android)
//         if (typeof plus !== 'undefined' && plus.android) {
//             try {
//                 let Looper = plus.android.importClass("android.os.Looper");
//                 let Handler = plus.android.importClass("android.os.Handler");
//                 let Context = plus.android.importClass("android.content.Context");
//                 let AdvertisingIdClient = plus.android.importClass(
//                     "com.google.android.gms.ads.identifier.AdvertisingIdClient");
//                 let mainActivity = plus.android.runtimeMainActivity();
//                 console.log("getGoogleAdId AdvertisingIdClient:", AdvertisingIdClient)
//                 // 在子线程获取广告 ID，避免 UI 线程阻塞
//                 let handler = new Handler(Looper.getMainLooper());

//                 handler.post(new plus.android.implements("java.lang.Runnable", {
//                     run: function() {
//                         try {
//                             let adInfo = AdvertisingIdClient.getAdvertisingIdInfo(mainActivity);
//                             let adId = adInfo.getId();
//                             resolve(adId);
//                         } catch (e) {
//                             reject("获取 Google 广告 ID 失败：" + e.message);
//                         }
//                     }
//                 }));
//             } catch (e) {
//                 reject("初始化失败：" + e.message);
//             }
//         } else {
//             reject("当前环境不支持 Android 原生接口");
//         }
//     });
// }
export const onBack = () => {
    const pages = getCurrentPages()
    if (pages.length > 1) {
        uni.navigateBack()
    } else {
        uni.reLaunch({ url: '/pages/home/Home' })
    }
}
// #ifdef APP-PLUS
export function getAdid() {
    return new Promise((resolve, reject) => {
        // const GoogleAdHelper = uni.requireNativePlugin('GoogleAdid')
        // GoogleAdHelper.getGoogleAdId(result => {
        //     console.log("GAID Result:" + JSON.stringify(result));
        //     if (result?.adID) {
        //         resolve(result?.adID)
        //     } else {
        //         reject(null)
        //     }
        // })
        const tiduGaidUtil = uni.requireNativePlugin('Tidu-GaidUtil');
        tiduGaidUtil.getGaid({}, result => {
            console.log("result is " + JSON.stringify(result));
            if (result?.gaid) {
                if(result.gaid === '00000000-0000-0000-0000-000000000000') {
                    reject(null)
                } else {
                    resolve(result?.gaid)
                }
            } else {
                reject(null)
            }
        })
    })
}

// export function getAndroidId() {
//     return new Promise((resolve, reject) => {
//         const AndroidId = uni.requireNativePlugin('AndroidId')
//         AndroidId.getAndroidId(result => {
//             console.log("android_id is " + JSON.stringify(result));
//             if (result?.aid) {
//                 resolve(result?.aid)
//             } else {
//                 reject(null)
//             }
//         })
//     })
// }

export const loginReport = (data) => {
    const LoginReport = uni.requireNativePlugin('EarnReport')
    LoginReport.loginReport(data, (err) => {
        if (err) {
            uni.showToast({
                title: 'report error',
                icon: 'none'
            })
        }
    })
}
// #endif