import UIKit

/// **容器模式** SDK 顶层入口。
///
/// **典型用法**:
/// ```swift
/// TencentQianWebView.open(
///     from: self,
///     url: signUrl,
///     config: TencentQianWebViewConfig(topBarTitle: "电子签署"),
///     callbacks: SignCallbacks(
///         onSignResult: { result in ... }
///     )
/// )
/// ```
///
/// **跨端等价**:Android `TencentQianWebView.open(...)`。
public enum TencentQianWebView {

    /// 当前 SDK 版本号。
    public static let sdkVersion: String = "1.2.0"

    /// 启动签署容器(modal present)。
    public static func open(
        from presenter: UIViewController,
        url: String,
        config: TencentQianWebViewConfig = .default,
        callbacks: SignCallbacks = SignCallbacks(),
        animated: Bool = true,
        completion: (() -> Void)? = nil
    ) {
        // 预热环境错配检测（best-effort，仅告警，不阻断）
        TencentQianWebViewPrewarmer.warnIfEnvironmentMismatch(url)
        let vc = TencentQianWebViewController(url: url, config: config, callbacks: callbacks)
        presenter.present(vc, animated: animated, completion: completion)
    }

    /// 启动「自检页」容器,验证客户 App 内 WebView 是否满足电子签 H5 兼容要求。
    /// `customUrl` 为 nil 时走 SDK 默认 URL(SDK 默认 beta 环境,兼容性测试无需切生产)。
    public static func openCompatibilityTest(
        from presenter: UIViewController,
        customUrl: String? = nil,
        config: TencentQianWebViewConfig = .default,
        callbacks: SignCallbacks = SignCallbacks(),
        animated: Bool = true
    ) {
        let finalUrl = customUrl
            ?? config.compatibilityTestUrl
            ?? TencentQianWebViewConfig.defaultCompatibilityTestUrl
        var cfg = config
        cfg.topBarTitle = L10n.compatibilityTestTitle
        open(from: presenter, url: finalUrl, config: cfg, callbacks: callbacks, animated: animated)
    }

    /// 工具方法:判断 URL 是否 SDK 应当处理。
    public static func canHandle(_ url: URL?) -> Bool {
        return url?.scheme?.lowercased() == "qianapp"
    }
}
