import Capacitor
import Foundation
import GameKit
import UIKit

/// Shared base for per-feature modules. Inherits `NSObject` so subclasses can
/// adopt @objc protocols (e.g. `GKGameCenterControllerDelegate`).
internal class PgsModule: NSObject {
    unowned let plugin: PlayGamesPlugin
    init(_ plugin: PlayGamesPlugin) {
        self.plugin = plugin
        super.init()
    }

    /// Top-most presented view controller. Callers must be on the main queue.
    func topViewController() -> UIViewController? {
        guard var top = plugin.bridge?.viewController else { return nil }
        while let presented = top.presentedViewController { top = presented }
        return top
    }
}

/// Serialise a GameKit player as the JS-side `PlayerInfo`. Uses the stable
/// cross-device `gamePlayerID` (not the per-team `teamPlayerID`).
internal func playerInfo(from player: GKLocalPlayer) -> [String: Any] {
    let info: [String: Any] = [
        "playerId": player.gamePlayerID,
        "displayName": player.displayName,
    ]
    return info
}
