import Adjust from '@adjustcom/adjust-web-sdk';

import { registerPlugin } from '@capacitor/core';
const AdjustPlugin = registerPlugin('AdjustPlugin');

var app_token = "7hj7gvqiun40";
const environment = 1;//0为sandbox，1为product

// 所有事件配置
var events = {
    // "7hj7gvqiun40":{
    //     click_register: { token: "n3i01g", desc: "点击注册弹窗" },
    //     click_register_count: { token: "ydct95", desc: "点击注册弹窗次数" },
    //     click_enter_game: { token: "o7bl4h", desc: "点击引导进入游戏弹窗" },
    //     click_enter_game_count: { token: "y3l5ub", desc: "点击引导进入游戏弹窗次数" },
    //     click_progress_bar: { token: "rnkku8", desc: "点击首页诱导进度条或withdraw按钮" },
    //     click_progress_bar_count: { token: "fdeq8x", desc: "点击首页诱导进度条或withdraw按钮次数" },
    //     click_bind_phone: { token: "1bjmpk", desc: "点击绑定手机" },
    //     click_bind_phone_count: { token: "3dgrxb", desc: "点击绑定手机次数" },
    //     click_first_pay: { token: "esu37i", desc: "点击首充礼包" },
    //     click_first_pay_count: { token: "d1ryc7", desc: "点击首充礼包次数" },
    //     click_withdraw_lock: { token: "ysqm7g", desc: "点击游戏内提款进度图标未解锁" },
    //     click_withdraw_lock_count: { token: "4rfz5i", desc: "点击游戏内提款进度图标未解锁次数" },
    //     click_withdraw_unlock: { token: "rhjdhz", desc: "点击游戏内提款进度图标已解锁" },
    //     click_withdraw_unlock_count: { token: "sj4ssz", desc: "点击游戏内提款进度图标已解锁次数" },
    //     click_banner1: { token: "w9aye8", desc: "点击大厅banner1图片" },
    //     click_banner1_count: { token: "wsgqty", desc: "点击大厅banner1图片次数" },
    //     click_banner2: { token: "hz8thw", desc: "点击大厅banner2图片" },
    //     click_banner2_count: { token: "q3b06y", desc: "点击大厅banner2图片次数" },
    //     click_banner3: { token: "np3gaf", desc: "点击大厅banner3图片" },
    //     click_banner3_count: { token: "nr67h8", desc: "点击大厅banner3图片次数" },
    //     click_recharge: { token: "q9q14p", desc: "点击充值" },
    //     click_recharge_count: { token: "pkxutd", desc: "点击充值次数" },
    //     click_recharge_first_pay: { token: "v2qcd7", desc: "点击充值首充礼包进入的" },
    //     click_recharge_first_pay_count: { token: "qeae0p", desc: "点击充值首充礼包进入的次数" },
    //     click_recharge_vip_hint: { token: "tn1o8n", desc: "点击充值VIP权益进来的" },
    //     click_recharge_vip_hint_count: { token: "qvgawg", desc: "点击充值VIP权益进来的次数" },
    //     click_recharge_in_control: { token: "xfdoto", desc: "点击收银台的充值按钮" },
    //     click_recharge_in_control_count: { token: "dbrfti", desc: "点击收银台的充值按钮次数" },
    //     click_download: { token: "ty8uyb", desc: "点击下载按钮" },
    //     click_withdraw_page_any: { token: "6kwfkh", desc: "点击大厅提款页任意按钮" },
    //     click_deposit: { token: "cs79ck", desc: "点击VIP权益弹窗按钮" },
    //     click_deposit_count: { token: "van1i2", desc: "点击VIP权益弹窗按钮次数" },
    //     click_withdraw_panel: { token: "ke7grf", desc: "点击游戏内引导提款弹窗按钮" },
    //     click_withdraw_panel_count: { token: "a30z6z", desc: "点击游戏内引导提款弹窗按钮次数" },
        
    //     enter_register_panel: { token: "pdv7uk", desc: "曝光注册弹窗" },
    //     enter_game: { token: "lfv0nq", desc: "曝光引导进入游戏弹窗" },
    //     enter_first_pay: { token: "ycyw9w", desc: "曝光首充礼包" },
    //     enter_first_pay_count: { token: "d3udj2", desc: "曝光首充礼包次数" },
    //     enter_bind_phone: { token: "t5vtiw", desc: "曝光绑定手机" },
    //     enter_bind_phone_count: { token: "prc7ym", desc: "曝光绑定手机次数" },
    //     enter_recharge_page: { token: "ikn9xr", desc: "曝光充值弹窗" },
    //     enter_recharge_page_count: { token: "oviqft", desc: "曝光充值弹窗次数" },
    //     enter_recharge_page_first_pay: { token: "h3d9rv", desc: "曝光充值弹窗" },
    //     enter_recharge_page_first_pay_count: { token: "ogrfqp", desc: "曝光充值弹窗次数" },
    //     enter_recharge_page_vip_hint: { token: "3oeumy", desc: "曝光充值弹窗" },
    //     enter_recharge_page_vip_hint_count: { token: "pyxhkz", desc: "曝光充值弹窗次数" },
    //     enter_recharge_control: { token: "bf9ehj", desc: "曝光支付控制台" },
    //     enter_recharge_control_count: { token: "lcdkok", desc: "曝光支付控制台次数" },
    //     enter_withdraw_panel: { token: "o4qi0j", desc: "曝光游戏内引导提款弹窗" },
    //     enter_withdraw_panel_count: { token: "d7eoz5", desc: "曝光游戏内引导提款弹窗次数" },
    //     enter_withdraw_page: { token: "jvdfi5", desc: "曝光大厅提款页" },
    //     enter_withdraw_page_count: { token: "qy75yn", desc: "曝光大厅提款页次数" },
    //     enter_deposit_panel: { token: "7okxxm", desc: "曝光VIP权益弹窗" },
    //     enter_deposit_panel_count: { token: "xk55bq", desc: "曝光VIP权益弹窗次数" },

    //     register_success: { token: "s301b6", desc: "注册成功人数" },
    //     register_success_count: { token: "qfnsj4", desc: "注册成功次数" },
    //     purchase_success: { token: "f9fqhg", desc: "充值成功人数" },
    //     purchase_success_count: { token: "ph7efy", desc: "充值成功次数" },
    //     first_pay_D0: { token: "w595dx", desc: "D0首充次数" },
    //     first_pay_D1: { token: "rhi4ng", desc: "D1首充次数" },
    // }
};

// 全局 tracker 对象 —— 你在任何地方都能这样调用：tracker.clickEnterGame()
const tracker = {
    trackEvent(eventKey, params = []) {
        // 如果是原生平台走androdSDK事件
        if (window.Capacitor?.isNativePlatform?.()) {
            console.log("native trackEvent to android sdk", window.AndroidAdjust, window.AndroidAdjust?.trackEvent);
            let token = events[app_token][eventKey]?.token;
            let revenue = params?.revenue || 0;
            let currency = global.currency || "USD";
            let orderId = params.orderId || "";
            window.AndroidAdjust?.trackEvent?.(token, revenue, currency, orderId);
            console.log("native trackEvent success");
            return;
        }

        if (events[app_token] && events[app_token][eventKey]) {
            if (eventKey == "purchase_success" || eventKey == "first_pay_D0" || eventKey == "first_pay_D1") {
                let revenue = params?.revenue || 0;
                let currency = global.currency || "USD";
                let orderId = params.orderId || "";
                if (revenue > 0){
                    Adjust.trackEvent({
                        eventToken: events[app_token][eventKey].token,
                        revenue: revenue,
                        currency: currency,
                        deduplicationId: orderId,
                    });
                } else {
                    console.log("tracker purchase fail revenue:" + revenue);
                }

            } else {
                Adjust.trackEvent({
                    eventToken: events[app_token][eventKey].token,
                    callbackParams: params
                });
            }

            // console.log("trackEvent success name: " + events[app_token][eventKey].desc + ",token:" + events[app_token][eventKey].token + ", params: " + JSON.stringify(params));
            console.log("trackEvent success name: " + events[app_token][eventKey].desc + ", params: " + JSON.stringify(params));
        } else {
            console.log("trackEvent key is not have! eventKey: " + eventKey);
        }
    },

    trackPurchaseEvent(params) {
        let data = params;
        if (data.first) {
            if (!!data.isd0) {
                // adjust 发送首充D0成功事件
                this.trackEvent("first_pay_D0", { revenue: data.golds, transaction_id: data.order_sn });
            }
            // adjust 发送首充D1成功事件
            this.trackEvent("first_pay_D1", { revenue: data.golds, transaction_id: data.order_sn });
        }
        // adjust 发送充值成功事件
        this.trackEvent("purchase_success", {revenue: data.golds, transaction_id: data.order_sn});
        this.trackEvent("purchase_success_count");
        console.log("trackPurchaseEvent success ");
    },

    initAdjust(appToken, adjustConfig) {
        // console.log(adjustConfig);
        app_token = appToken;
        events[appToken] = JSON.parse(adjustConfig);
        // 如果是原生平台走androdSDK事件
        if (window.Capacitor?.isNativePlatform?.()) {
            console.log("JS Adjust initAdjust success");
            global.adjustId = window.AndroidAdjust?.getAdid?.();
            app_token = window.AndroidAdjust?.getAppToken?.() || "7hj7gvqiun40";
            return;
        }

        Adjust.initSdk({
            appToken: app_token,
            environment: environment ? 'production' : 'sandbox',
            mustWaitForWebUUID: true,
            // logLevel: 'verbose'  // 添加这一行
        });
        console.log("Adjust is init success");
        this.getWebUUID();
    },

    getWebUUID() {
        Adjust.waitForWebUUID().then((uuid)=>{
            global.adjustId = uuid;
            // console.log("uuid: " + uuid);
        });
    }
};

export default tracker;