import ContentsquareSDK
import ContentsquareModule
import HeapSwiftCore

@objc(CSQInterfaceEventTracking)
public class CSQInterfaceEventTracking: NSObject {
    let CSQType: _CSQ.Type
    let HeapType: _Heap

    @objc
    public convenience override init() {
        self.init(CSQ.self, heap: Heap.shared)
    }

    init(_ CSQType: _CSQ.Type, heap: _Heap) {
        self.CSQType = CSQType
        self.HeapType = heap

        super.init()
    }
    
    @objc
    public func trackScreenView(name: String, cvars: [[String: Any]], sourceInfo: [String: Any]?) {
        Contentsquare.perform(
            NSSelectorFromString("_trackScreenViewWithParameters:"),
            with: ["screenName": name, "cvars": cvars, "heapSourceInfo": sourceInfo ?? [:]]
        )
    }

    @objc
    public func trackTransaction(transactionId id: String?,
                     value:Float,
                                 currency: ContentsquareSDK.Currency) {
        let transaction = ContentsquareSDK.Transaction(id: id, value: value, currency: currency)
        CSQType.trackTransaction(transaction)
    }
    
    @objc
    public func trackTransaction(transactionId id: String?,
                     value:Float,
                     stringCurrency currency: String) {
        let transaction = ContentsquareSDK.Transaction(id: id, value: value, currency: currency)
        CSQType.trackTransaction(transaction)
    }

    @objc
    public func trackEvent(name: String,
                           properties: [String: Any]?,
                           sourceInfo: [String: Any]?) {
        let processedProperties = PropertyValueConverter.processPropertyValues(properties)
        let formattedSourceInfo = PropertyValueConverter.createSourceInfo(from: sourceInfo)

        HeapType.__track(name, properties: processedProperties as! [String : any HeapSwiftCore.HeapObjcPropertyValue], sourceInfo: formattedSourceInfo)
    }
}

