//
//  LNStatisticManager.swift
//  Gami
//
//  Created by OneeChan on 2026/1/27.
//

import Foundation
import AdjustSdk


private enum LNStatisticEventToken: String {
    case call_accept = "5xzpm9"
    case call_end = "dr9456"
    case call_start = "y39skp"
    
    case first_payment = "8p9mbg"
    case register = "4vtp6f"
    case start_chat = "mhhvkj"
    
    case view_playmate = "hivhhh"
    case view_profile = "zfdf34"
    case view_session = "z9bxom"
    
    case coin_payment_page_view = "kyb5bm"
    case coin_payment_page_click = "vbkt69"
    case diamond_payment_page_view = "qlu402"
    case diamond_payment_page_click = "xa6ysh"
}


class LNStatisticManager {
    static let shared = LNStatisticManager()
    
    private init() { }
    
    func reportExposure(uids: [String], queue: DispatchQueue = .main, handler: @escaping (Bool) -> Void) {
        LNHttpManager.shared.reportExposure(uids: uids) { err in
            queue.asyncIfNotGlobal {
                handler(err == nil)
            }
        }
    }
    
    func reportVisitor(uid: String, queue: DispatchQueue = .main, handler: @escaping (Bool) -> Void) {
        LNHttpManager.shared.reportVisitor(uid: uid) { err in
            queue.asyncIfNotGlobal {
                handler(err == nil)
            }
        }
    }
}

// MARK: 视图埋点
extension LNStatisticManager {
    func reportViewPlaymate(uid: String) {
        trackEvent(.view_playmate) { event in
            event.addCallbackParameter("user_id", value: uid)
        }
    }
    
    func reportViewProfile(uid: String) {
        trackEvent(.view_profile) { event in
            event.addCallbackParameter("user_id", value: uid)
        }
    }
    
    func reportViewChat(uid: String) {
        trackEvent(.view_session) { event in
            event.addCallbackParameter("user_id", value: uid)
        }
    }
    
    func reportViewCoinPay() {
        trackEvent(.coin_payment_page_view) { event in
            event.addCallbackParameter("user_id", value: myUid)
        }
    }
    
    func reportViewDiamondPay() {
        trackEvent(.diamond_payment_page_view) { event in
            event.addCallbackParameter("user_id", value: myUid)
        }
    }
}

// MARK: 点击埋点
extension LNStatisticManager {
    func reportClickChat(uid: String) {
        trackEvent(.start_chat) { event in
            event.addCallbackParameter("user_id", value: uid)
        }
    }
    
    func reportClickCoinPay() {
        trackEvent(.coin_payment_page_click) { event in
            event.addCallbackParameter("user_id", value: myUid)
        }
    }
    
    func reportClickDiamondPay() {
        trackEvent(.diamond_payment_page_click) { event in
            event.addCallbackParameter("user_id", value: myUid)
        }
    }
}

// MARK: 通话埋点
extension LNStatisticManager {
    func reportStartCall(uid: String, success: Bool) {
        trackEvent(.call_start) { event in
            event.addCallbackParameter("user_id", value: uid)
            event.addCallbackParameter("result", value: success ? "1" : "0")
        }
    }
    
    func reportAcceptCall(uid: String, success: Bool) {
        trackEvent(.call_accept) { event in
            event.addCallbackParameter("user_id", value: uid)
            event.addCallbackParameter("result", value: success ? "1" : "0")
        }
    }
    
    func reportEndCall(uid: String, duration: Float) {
        trackEvent(.call_end) { event in
            event.addCallbackParameter("user_id", value: uid)
            event.addCallbackParameter("duration", value: "\(duration)")
        }
    }
}

// MARK: 登录埋点
enum LNRegisterMethod: String {
    case email
    case phone
    case google
    case apple
}

extension LNStatisticManager {
    func reportRegister(method: LNRegisterMethod) {
        trackEvent(.register) { event in
            event.addCallbackParameter("user_id", value: myUid)
            event.addCallbackParameter("register_method", value: method.rawValue)
            event.addCallbackParameter("register_ts", value: "\(Int(curTime))")
        }
    }
    
    func reportPayment(type: LNCurrencyType, amount: Double, currency: String, price: Double, orderId: String) {
        trackEvent(.first_payment) { event in
            event.addCallbackParameter("order_id", value: orderId)
            event.addCallbackParameter("payment_type", value: "\(type.rawValue)")
            event.addCallbackParameter("payment_amount", value: "\(amount)")
            event.addCallbackParameter("payment_currency", value: currency)
            event.addCallbackParameter("payment_price", value: "\(price)")
            event.setRevenue(price, currency: currency)
        }
    }
}

extension LNStatisticManager {
    private func trackEvent(_ token: LNStatisticEventToken, configure: (ADJEvent) -> Void) {
        guard let event = eventFor(token) else { return }
        configure(event)
        Adjust.trackEvent(event)
    }
    
    private func eventFor(_ token: LNStatisticEventToken) -> ADJEvent? {
        let event = ADJEvent(eventToken: token.rawValue)
        event?.addCallbackParameter("platform", value: "iOS")
        event?.addCallbackParameter("app", value: "gami")
        event?.addCallbackParameter("app_channel", value: "apple")
        event?.addCallbackParameter("version_code", value: curBuildVersion)
        event?.addCallbackParameter("version_name", value: curAppVersion)
        event?.addCallbackParameter("uid", value: myUid)
        event?.addCallbackParameter("device_id", value: curDeviceId)
        event?.addCallbackParameter("country_code", value: LNAppConfig.shared.countryCode)
        event?.addCallbackParameter("language_code", value: LNAppConfig.shared.curLang.languageCode)
        event?.addCallbackParameter("device_name", value: curDeviceModelName)
        event?.addCallbackParameter("os_version", value: curSystemVersion)
        event?.addCallbackParameter("network_type", value: LNNetworkMonitor.curNetworkType.desc)
        event?.addCallbackParameter("network_operator", value: LNNetworkMonitor.networkProvider)
        event?.addCallbackParameter("network_available", value: LNNetworkMonitor.curState == .available ? "true" : "false")
        event?.addCallbackParameter("background", value: LNAppConfig.shared.isForeground ? "false" : "true")
        
        return event
    }
}
