//
//  CircleStoryConfiguration.swift
//  react-native-firework-sdk
//

import FireworkVideo
import Foundation

@objc
public class CircleStoryConfiguration: NSObject, Codable {
    var backgroundColor: String?
    var playIcon: VideoFeedConfiguration.VideoFeedPlayIconConfiguration?
    var showAdBadge: Bool?
    var enableAutoplay: Bool?
    var replayBadge: ReplayBadgeConfiguration?
    var shadow: VideoFeedConfiguration.VideoFeedShadowConfiguration?
    var contentPadding: VideoFeedConfiguration.VideoFeedPadding?
    var itemSpacing: Double?

    func toNativeConfiguration() -> CircleStoryContentConfiguration {
        var config = CircleStoryContentConfiguration()

        if let bgColor = backgroundColor, let color = bgColor.uicolor() {
            config.backgroundColor = color
        }

        if let playIconConfig = playIcon {
            playIconConfig.apply(to: &config.itemView.playIcon)
        }

        if let showBadge = showAdBadge {
            config.itemView.sponsored.isHidden = !showBadge
        }

        if let autoplay = enableAutoplay {
            config.itemView.autoplay.isEnabled = autoplay
        }

        if let shadowConfig = shadow {
            if let opacity = shadowConfig.opacity {
                config.itemView.shadow.opacity = opacity
            }
            if let color = shadowConfig.color, let uiColor = color.uicolor() {
                config.itemView.shadow.color = uiColor
            }
            if let width = shadowConfig.width {
                config.itemView.shadow.offset = CGSize(width: width, height: config.itemView.shadow.offset.height)
            }
            if let height = shadowConfig.height {
                config.itemView.shadow.offset = CGSize(width: config.itemView.shadow.offset.width, height: height)
            }
        }

        if let replayConfig = replayBadge {
            config.replayBadge.isHidden = replayConfig.isHidden
        }

        return config
    }

    func toNativeLayout() -> CircleStoryLayout {
        let layout = CircleStoryLayout()

        if let padding = contentPadding {
            layout.contentInsets.top = 10
            layout.contentInsets.right = 10
            layout.contentInsets.bottom = 10
            layout.contentInsets.left = 10
            if let top = padding.top {
                layout.contentInsets.top = top
            }
            if let right = padding.right {
                layout.contentInsets.right = right
            }
            if let bottom = padding.bottom {
                layout.contentInsets.bottom = bottom
            }
            if let left = padding.left {
                layout.contentInsets.left = left
            }
        }

        if let spacing = itemSpacing {
            layout.itemSpacing = spacing
        }

        return layout
    }
}
