//
//  QSEventHandle.swift
//  QuickSearchLocation
//
//  Created by Destiny on 2025/10/11.
//

import UIKit
import GravityEngineSDK

class QSEventHandle: NSObject {
    static let isDebug = false
    static func gravityPush(eventName: String, eventProps: [String:Any] = [:]){
#if targetEnvironment(simulator)
        return;
#else

#endif
        if(isDebug){
            do {
                let jsonData = try JSONSerialization.data(withJSONObject: eventProps, options: .prettyPrinted)
                let jsonString = String(data: jsonData, encoding: .utf8)
                DispatchQueue.main.async {
                    UIApplication.keyWindow?.toast(text: "event:\(eventName)\neventProps:\(String(describing: jsonString))")
                }
                print(jsonString ?? "转换失败")
            } catch {
                print("JSON 转换错误: \(error)")
            }
            
        }
        GravityEngineSDK.sharedInstance()?.track(eventName, properties: eventProps)
    }

    /// 上传单次事件到服务器
    ///
    /// - Parameters:
    ///   - eventType: 事件类型（如 "track"）暂时只有track 可以不传
    ///   - eventName: 事件名称（如 "vip_center_exposure"）
    ///   - eventProps: 事件属性（字典形式，如 `["is_member": false, "entry_point": "open_now"]`）
    ///
    /// - Example:
    ///   ```
    ///   eventPush(
    ///       eventType: "track",
    ///       eventName: "button_click",
    ///       eventProps: ["button_id": "submit"]
    ///   )
    ///   ```
    ///
    static func eventPush(eventType: String = "track", eventName: String, eventProps: [String:Any] = [:]){
#if targetEnvironment(simulator)
        return;
#else

#endif
        if(isDebug){
            do {
                let jsonData = try JSONSerialization.data(withJSONObject: eventProps, options: .prettyPrinted)
                let jsonString = String(data: jsonData, encoding: .utf8)
                DispatchQueue.main.async {
                    UIApplication.keyWindow?.toast(text: "event:\(eventName)\neventProps:\(String(describing: jsonString))")
                }
                
                print(jsonString ?? "转换失败")
            } catch {
                print("JSON 转换错误: \(error)")
            }
            
        }
        GravityEngineSDK.sharedInstance()?.track(eventName, properties: eventProps)
        QSLCentralEventManager.shared.uploadEvent(eventType: "track", eventName: eventName, eventProps: eventProps)
    }
    
    /// 上传多次事件到服务器
    ///
    /// - Parameters:
    ///   - eventType: 事件类型（如 "track"）暂时只有track 可以不传
    ///   - eventName: 事件名称（如 "vip_center_exposure"）
    ///   - eventProps: 事件属性（字典数组形式，如 `[["is_member": false, "entry_point": "open_now"],...]`）
    ///
    /// - Example:
    ///   ```
    ///   eventPush(
    ///       eventType: "track",
    ///       eventName: "button_click",
    ///       eventProps: ["button_id": "submit"]
    ///   )
    ///   ```
    ///
    static func eventArrPush(eventType: String = "track", eventName: String, eventPropsArr: [[String:Any]] = []){
        QSLCentralEventManager.shared.uploadEvent(eventType: "track", eventName: eventName, eventPropsArr: eventPropsArr)
    }
    
    
}
