//
//  RCTConvert+FWVideoFeed.swift
//  react-native-firework-sdk
//
//  Created by Jeff Zheng on 2021/12/22.
//

import Foundation
import FireworkVideo

extension VideFeedSourceType {
    static var sourceTypeMapper: [String: VideFeedSourceType] {
        [
            "discover": .discover,
            "channel": .channel,
            "playlist": .playlist,
            "playlistGroup": .playlistGroup,
            "dynamicContent": .dynamicContent,
            "hashtagPlaylist": .hashtagPlaylist,
            "sku": .sku,
            "singleContent": .singleContent
        ]
    }
}

extension VideoFeedMode {
    static var feedModeMapper: [String: VideoFeedMode] {
        [
            "row": .row,
            "column": .column,
            "grid": .grid
        ]
    }
}

@objc
extension RCTConvert {
    public static func videoFeedSourceType(_ type: String?) -> VideFeedSourceType {
        guard let rType = type else {
            return .discover
        }

        return VideFeedSourceType.sourceTypeMapper[rType] ?? .discover
    }

    public static func videoFeedMode(_ type: String?) -> VideoFeedMode {
        guard let rType = type else {
            return .row
        }

        return VideoFeedMode.feedModeMapper[rType] ?? .row
    }

    public static func videoFeedConfiguration(_ config: NSDictionary?)
        -> VideoFeedConfiguration? {
            guard let rConfig = config as? [String: Any] else {
            return nil
        }

        let jsonData = try? JSONSerialization.data(withJSONObject: rConfig, options: .prettyPrinted)
        guard let rJsonData = jsonData else {
            return nil
        }

        return try? JSONDecoder().decode(VideoFeedConfiguration.self, from: rJsonData)
    }

    public static func videoPlayerConfiguration(_ config: [String: Any]?)
        -> VideoPlayerConfiguration? {
        guard let rConfig = config else {
            return nil
        }

        let jsonData = try? JSONSerialization.data(withJSONObject: rConfig, options: .prettyPrinted)
        guard let rJsonData = jsonData else {
            return nil
        }

        return try? JSONDecoder().decode(VideoPlayerConfiguration.self, from: rJsonData)
    }

    public static func adConfiguration(_ config: NSDictionary?) -> AdConfiguration? {
        guard let rConfig = config as? [String: Any] else {
            return nil
        }

        let jsonData = try? JSONSerialization.data(withJSONObject: rConfig, options: .prettyPrinted)
        guard let rJsonData = jsonData else {
            return nil
        }

        return try? JSONDecoder().decode(AdConfiguration.self, from: rJsonData)
    }

    @nonobjc
    public static func getFireworkVideoAdConfiguration(
        _ adConfiguration: AdConfiguration?
    ) -> FireworkVideo.AdConfiguration? {
        guard let feedAdConfiguration = adConfiguration else {
            return nil
        }

        var resultAdConfiguration = FireworkVideo.AdConfiguration()
        if let requiresAds = feedAdConfiguration.requiresAds {
            resultAdConfiguration.requiresAds = requiresAds
        }

        if let adsFetchTimeout = feedAdConfiguration.adsFetchTimeout {
            resultAdConfiguration.adsFetchTimeout = adsFetchTimeout
        }

        if let vastAttributes = feedAdConfiguration.vastAttributes {
            resultAdConfiguration.vastAttributes = vastAttributes.map({ attribute in
                return URLQueryItem(name: attribute.name ?? "", value: attribute.value ?? "")
            })
        }

        return resultAdConfiguration
    }
}
