//
//  QSLGravityManager.swift
//  QuickSearchLocation
//
//  Created by Destiny on 2024/12/23.
//

import Foundation
import GravityEngineSDK

var gravityInstance: GravityEngineSDK?

class QSLGravityManager {

    func initGE() {
        
        // 引力引擎
        let config = GEConfig()
        
        config.appid = QSLGravityConst.appid
        config.accessToken = QSLGravityConst.accessToken

        GravityEngineSDK.start(with: config)
        let instance = GravityEngineSDK.sharedInstance(withAppid: config.appid)
        
        gravityInstance = instance

        // 开启自动采集
        instance?.enableAutoTrack(GravityEngineAutoTrackEventType.eventTypeAll)
        
        print("启动引力引擎")
        
        let idfa = QSLApi.params["idfa"] as? String ?? ""
        
        let idfv = QSLApi.params["idfv"] as? String ?? ""
        
        let version = QSLApi.params["appVersionCode"] as? Int ?? 100
        
        instance?.initializeGravityEngine(withAsaEnable: true, withCaid1: "", withCaid2: "", withSyncAttribution: true, withChannel: "AppStore", withSuccessCallback: { response in
            print("gravity engine initialize success, response is", response)
            
            // 检查订单
            QSLBaseManager.shared.initPayCheck()
            
            gravityInstance?.track(QSLGravityConst.launch_show)
            
            if let clientId = gravityInstance?.getCurrentClientId() {
                QSLBaseManager.shared.uploadClientId(clientId: clientId)
            }
            
        }, withErrorCallback: { error in
            print("gravity engine initialize failed, and error is", error)
            
            // 检查订单
            QSLBaseManager.shared.initPayCheck()
        })
        
        //开始统计时长
        QSLStatisticsUserTimeManager.shared.startTracking()
    }
    
}
