 
 private extension Selector {
     static let updateTime = #selector(StatusBar.updateTime)
+    static let batteryLevelChanged = #selector(StatusBar.batteryLevelChanged(notification:))
 }
+
+private let BatteryRedLevel: CGFloat = 0.2
 
 public class StatusBar: GameView {
     private var clockLabel: SKLabelNode!

    public var tintColor: SKColor = .black {
        didSet {
            clockLabel.fontColor = tintColor
        }
    }
    
    public override func loadContent() {
        clockLabel = SKLabelNode(text: "12:30")

        clockLabel.fontSize = 20
        addChild(clockLabel)
        
        batteryLevelBox = SKShapeNode(rect: CGRect(x: 0, y: 0, width: 25, height: 12), cornerRadius: 2)
        batteryLevelBox.strokeColor = .clear
        batteryLevelBox.zPosition = zPosition + 1
        addChild(batteryLevelBox)
        batteryBox = SKShapeNode()
        batteryBox.path = CGPath(roundedRect: CGRect(x: 0, y: 0, width: 25, height: 12), cornerWidth: 2, cornerHeight: 2, transform: nil)
        batteryBox.zPosition = zPosition + 2
        addChild(batteryBox)
        
        updateTime()
        updateBattery()
        
        timer?.invalidate()
        let seconds = Date.timeIntervalSinceReferenceDate.truncatingRemainder(dividingBy: 60)
        let fireAt = Date(timeIntervalSinceNow: 60 - seconds)
        timer = Timer(fireAt: fireAt, interval: 60, target: self, selector: .updateTime, userInfo: nil, repeats: true)
        RunLoop.current.add(timer!, forMode: RunLoopMode.defaultRunLoopMode)
        
        NotificationCenter.default.addObserver(self, selector: .batteryLevelChanged, name: Notification.Name.UIDeviceBatteryLevelDidChange, object: nil)
        UIDevice.current.isBatteryMonitoringEnabled = true
    }
    
    public override func unloadContent() {
        timer?.invalidate()
        NotificationCenter.default.removeObserver(self)
        UIDevice.current.isBatteryMonitoringEnabled = false
    }
    
    public override func positionContent() {
        clockLabel.position = CGPoint(x: size.width / 2, y: (size.height - clockLabel.frame.size.height) / 2)
        let batteryPoint = CGPoint(x: size.width - batteryBox.frame.size.width - 4, y: (size.height - batteryBox.frame.size.height) / 2)
        batteryBox.position = batteryPoint
        batteryLevelBox.position = batteryPoint
        super.positionContent()
    }
    
    @objc fileprivate func updateTime() {
        clockLabel.text = DateFormatter.timeFormatter.string(from: Date())
    }
    
    private func updateBattery() {
        let level = CGFloat(max(UIDevice.current.batteryLevel, 0.01))
        var levelBox = batteryBox.frame.insetBy(dx: 2, dy: 2)
        levelBox.size.width = levelBox.width * CGFloat(level)
        batteryLevelBox.fillColor = level <= BatteryRedLevel ? .red : tintColor
        batteryLevelBox.strokeColor = tintColor
        let fillPath = UIBezierPath(roundedRect: levelBox, byRoundingCorners: [UIRectCorner.bottomLeft, UIRectCorner.topLeft], cornerRadii: CGSize(width: 2, height: 2))
        batteryLevelBox.path = fillPath.cgPath
    }
    
    @objc fileprivate func batteryLevelChanged(notification: Notification) {
        updateBattery()
    }
}
