 //
 
 import AVFoundation
+import ObjectiveC
 
 // Current Playback Time monitoring
 
 public extension AVPlayer {
+    public var isPlaying: Bool {
+        return rate != 0 && error == nil
+    }
+
     public func currentTime(block: (seconds: Int, formattedTime: String) -> Void) -> AnyObject {
         let interval = CMTimeMake(1, 1)
         return addPeriodicTimeObserverForInterval(interval, queue: dispatch_get_main_queue()) {[weak self] time in

            self.init(fileName: remoteOrLocalName)
        }
    }

    public var hasValidDuration: Bool {
        return status == .ReadyToPlay && duration.isValid
    }
}

public extension CMTime {
    public var isValid: Bool { return flags.contains(.Valid) }
}


        return String(format: "%02d:%02d:%02d", hrs, min, sec)
    }
}

public extension AVPlayer {
    private struct AssociatedKey {
        static var Repeat = "XcoreAVPlayerRepeat"
    }

    /// Indicates whether to repeat playback of the current item.
    public var `repeat`: Bool {
        get { return objc_getAssociatedObject(self, &AssociatedKey.Repeat) as? Bool ?? false }
        set {
            guard newValue != `repeat` else { return }
            objc_setAssociatedObject(self, &AssociatedKey.Repeat, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)

            if newValue {
                NSNotificationCenter.defaultCenter().addObserverForName(AVPlayerItemDidPlayToEndTimeNotification, object: currentItem, queue: nil) {[weak self] notification in
                    if let currentItem = notification.object as? AVPlayerItem {
                        self?.actionAtItemEnd = .None
                        currentItem.seekToTime(kCMTimeZero)
                        self?.play()
                    }
                }
            } else {
                NSNotificationCenter.defaultCenter().removeObserver(self, name: AVPlayerItemDidPlayToEndTimeNotification, object: currentItem)
            }
        }
    }
}

public extension AVPlayerItem {
    public var hasValidDuration: Bool {
        return status == .ReadyToPlay && duration.isValid
    }
}

public extension CMTime {
    public var isValid: Bool { return flags.contains(.Valid) }

    public func timeWithOffset(offset: NSTimeInterval) -> CMTime {
        let seconds = CMTimeGetSeconds(self)
        let secondsWithOffset = seconds + offset
        return CMTimeMakeWithSeconds(secondsWithOffset, timescale)
    }
}

// MARK: RemoteOrLocalInstantiable

// Convenience methods for initializing videos

public extension AVPlayer {
    /// Initializes an AVPlayer that automatically detect and load the asset from local or a remote url.
    ///
    /// Implicitly creates an AVPlayerItem. Clients can obtain the AVPlayerItem as it becomes the player's currentItem.
    ///
    /// - parameter remoteOrLocalName: The local file name from `NSBundle.mainBundle()` or remote url
    ///
    /// - returns:            An instance of AVPlayer
    public convenience init?(remoteOrLocalName: String) {
        if let playerItem = AVPlayerItem(remoteOrLocalName: remoteOrLocalName) {
            self.init(playerItem: playerItem)
        } else {
            return nil
