//
//  PlayerViewController.swift
//  Vlogio
//
//  Created by Alexander Popov on 2020-06-29.
//  Copyright © 2020 Alexander Popov. All rights reserved.
//

import Foundation
import AVFoundation
import AVKit
import SwiftUI

struct PlayerViewControllerRep: UIViewControllerRepresentable {
    
    let appState: AppState!
    
    init(appState: AppState) {
        self.appState = appState
    }
    
    func makeUIViewController(context: Context) -> some UIViewController {
        let pv = AVPlayerViewController()
        let playerItems = appState.playerItemsForCurrentVlog()
        if playerItems.isEmpty {
            return pv
        }
        pv.player = AVQueuePlayer(items: playerItems)
        pv.entersFullScreenWhenPlaybackBegins = true
        pv.player?.play()
        return pv
    }
    
    func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) {
        debugPrint("PlayerViewController updated")
    }
    
}
