//
//  VehicleMonitoringVideoDetailController.swift
//  OrderScheduling
//
//  Created by 中道 on 2023/8/16.
//

import Foundation
import DDControlsKit_Private
import DDCategoryKit_Private
import DDAutoUIKit_Private
import SnapKit
import ZFPlayer

open class VehicleMonitoringVideoDetailController : DDViewController {
    private let player : ZFPlayerController
    private let controlView : ZFPlayerControlView
    private let containerView : DDImageView
    
    public init(assetURL:URL?) {
        
        controlView = ZFPlayerControlView()
        controlView.fastViewAnimated = true
        controlView.autoHiddenTimeInterval = 5
        controlView.autoFadeTimeInterval = 0.5
        controlView.prepareShowLoading = true
        controlView.prepareShowControlView = false
        controlView.fullScreenMode = .landscape
        
        containerView = DDImageView()

        let playManager = ZFIJKPlayerManager()
        player = ZFPlayerController(playerManager: playManager, containerView: containerView)
        player.controlView = controlView
        super.init(nibName: nil, bundle: nil)
        
        player.pauseWhenAppResignActive = true

        player.orientationDidChanged = { (player,isFullScreen) in
            let appdelegate = UIApplication.shared.delegate as? AppDelegate
            appdelegate?.allowOrentitaionRotation = isFullScreen
        }
        
        view.addSubview(containerView)
        containerView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        if let assetURL {
            player.assetURLs = [assetURL]
            player.playTheIndex(0)
            controlView.showTitle("", coverURLString: nil,fullScreenMode: .automatic)
        }
    }

    public required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    deinit{
        player.stop()
    }
    
    public func playAssetURL(assetURL:URL?) {
        if let assetURL {
            player.stop()
            player.assetURLs = [assetURL]
            player.playTheIndex(0)
        }
    }
    
    public func stop() {
        player.stop()
    }
    
    open override func viewDidLoad() {
        super.viewDidLoad()
        dd_navigationBarBackgroundColor = .hex("354683")
        dd_navigationBarTitleTextAttributes = [.foregroundColor : UIColor.white(alpha: 0.7),.font:UIFont.mediumFont(17)]
    }

    open override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        player.isViewControllerDisappear = false
    }
    
    open override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        player.isViewControllerDisappear = true
    }
    
    open override var preferredStatusBarStyle: UIStatusBarStyle {
        return .default
    }
    
    open override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation {
        return .none
    }
    
    open override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return .allButUpsideDown
    }
}

 
