// ViewController.swift
// Tencent is pleased to support the open source community by making vap available.

import UIKit
import QGVAPlayer

final class ViewController: UIViewController, LNVAPPlaybackDelegate {
    private let vapButton = UIButton()
    private let failButton = UIButton()
    private var playerView: LNVAPPlayerView?

    override func viewDidLoad() {
        super.viewDidLoad()

        vapButton.frame = CGRect(x: 0, y: 100, width: view.frame.width, height: 90)
        vapButton.backgroundColor = .lightGray
        vapButton.setTitle("LN Swift API", for: .normal)
        vapButton.addTarget(self, action: #selector(playVap), for: .touchUpInside)
        view.addSubview(vapButton)

        failButton.frame = CGRect(x: 0, y: 220, width: view.frame.width, height: 90)
        failButton.backgroundColor = .lightGray
        failButton.setTitle("LN Swift Fail Path", for: .normal)
        failButton.addTarget(self, action: #selector(playInvalidPath), for: .touchUpInside)
        view.addSubview(failButton)
    }

    @objc private func playVap() {
        playerView?.removeFromSuperview()
        let player = LNVAPFacade.lnMakePlayerView(frame: view.bounds)
        player.center = view.center
        player.delegate = self
        player.enterBackgroundOperation = .stop
        LNVAPFacade.lnSetMutePlayer(player, mute: true)
        player.lnEnableOldVersion(true)
        player.lnAddTapGesture(target: self, action: #selector(onTap(_:)))
        view.addSubview(player)
        playerView = player

        let mp4Path = "\(Bundle.main.resourcePath ?? "")/Resource/vap.mp4"
        LNVAPFacade.lnPlayPlayer(player, filePath: mp4Path, repeatCount: -1, delegate: self)
    }

    @objc private func onTap(_ gesture: UIGestureRecognizer) {
        if let playerView {
            LNVAPFacade.lnStopPlayer(playerView)
        }
        gesture.view?.removeFromSuperview()
    }

    @objc private func playInvalidPath() {
        playerView?.removeFromSuperview()
        let player = LNVAPFacade.lnMakePlayerView(frame: view.bounds)
        player.center = view.center
        player.delegate = self
        view.addSubview(player)
        playerView = player
        LNVAPFacade.lnPlayPlayer(player, filePath: "/tmp/not_exist_vap.mp4", repeatCount: 0, delegate: self)
    }

    func lnPlayerDidStart(_ playerView: LNVAPPlayerView) {
        print("[LN Demo Swift] player did start")
    }

    func lnPlayerDidPlay(_ playerView: LNVAPPlayerView, frame: LNMP4AnimatedImageFrame) {
        print("[LN Demo Swift] player did play frame=\(frame.frameIndex)")
    }

    func lnPlayerDidFinish(_ playerView: LNVAPPlayerView, totalFrameCount: Int) {
        print("[LN Demo Swift] player did finish total=\(totalFrameCount)")
    }

    func lnPlayerDidStop(_ playerView: LNVAPPlayerView) {
        print("[LN Demo Swift] player did stop")
        DispatchQueue.main.async {
            playerView.removeFromSuperview()
        }
    }

    func lnPlayerDidFail(_ playerView: LNVAPPlayerView, error: NSError) {
        print("[LN Demo Swift] player did fail error=\(error)")
    }
}
