//
//  JWAdsBuilder.swift
//  WOKA
//
//  Created by MacBook Pro on 25/09/24.
//
import JWPlayerKit

struct AdBreakItemStruct{
    let adUrl : URL
    let offset : JWAdOffset
}

class JWAdsBuilder{
    
    static let shareInstace = JWAdsBuilder()
    
    private init(){}
    
    func createAds(adsData : [AdBreakItemStruct]) -> JWAdvertisingConfig?{
        var adBreaks = [JWAdBreak]()
        
        for adTags in adsData{
            let adBreakBuilder = JWAdBreakBuilder()
                .offset(adTags.offset)
                .tags([adTags.adUrl])
            
            if let adBreak = try? adBreakBuilder.build() {
                adBreaks.append(adBreak)
            }
        }
        
        let adConfigBuilder = JWImaAdvertisingConfigBuilder()
            .schedule(adBreaks)
        guard let adConfig = try? adConfigBuilder.build() else {
            // Handle build error
            return nil
        }
        
        return adConfig
    }
    
    func generateAdTagUrl(url : String) -> String {
        let correlatorValue = Int(Date().timeIntervalSince1970 * 1000)
        return "\(url)\(correlatorValue)"
    }

}
