//
//  TSBandRingTool.swift
//  AIRingtone
//
//  Created by 100Years on 2025/3/4.
//
import AVKit
import AVFoundation
class TSBandRingTool:NSObject {
    static var shared = TSBandRingTool()
    private var tutorialPlayer: AVPlayer?
    private var pictureController: AVPictureInPictureController?
    private lazy var playContentView = UIView(frame: UIScreen.main.bounds)
    private lazy var audioConvertTool = AudioTool()
    
    weak var targetVC:UIViewController?
//    init(targetVC: UIViewController) {
//        self.targetVC = targetVC
//    }
    
    static func creatBandRingTool() -> TSBandRingTool{
        TSBandRingTool.shared = TSBandRingTool()
        return TSBandRingTool.shared
    }
    
    deinit {
        dePrint("TSBandRingTool deinit")
    }
    func checkGarageBandInstallation() -> Bool {
        // GarageBand 的 URL Scheme
        let garageBandScheme = "garageband://"

        if let url = URL(string: garageBandScheme), UIApplication.shared.canOpenURL(url) {
            print("GarageBand 已安装")
            return true
        } else {
            let ac = UIAlertController(title: nil,
                                       message: "GarageBand is not installed, you need to install it.", preferredStyle: .alert)
            ac.addAction(UIAlertAction(title: "Cancel", style: .cancel))
            ac.addAction(UIAlertAction(title: "Download", style: .default, handler: { _ in
                // GarageBand 在 App Store 的链接
                let garageBandAppStoreURL = URL(string: "https://apps.apple.com/app/id408709785")!
                if UIApplication.shared.canOpenURL(garageBandAppStoreURL) {
                    UIApplication.shared.open(garageBandAppStoreURL, options: [:], completionHandler: nil)
                }
            }))
            targetVC?.present(ac, animated: true)
          
            return false
        }
    }
    
    func shareBandVC(vc:UIViewController,
        fileURLString: String,
                   fileName:String?,
                   completion: ((Bool) -> Void)? = nil) {
        
        if checkGarageBandInstallation() == false {
            completion?(false)
            return
        }
        self.targetVC = vc
        if fileURLString.contains("http") {
            TSLoadingAnimation.showLoading(in: self.targetVC?.view)
            TSCommonTool.downloadAndCacheFile(from: fileURLString) { path, error in
                TSLoadingAnimation.hideLoading()
                if let path = path,let url = URL(string: path) {
                    self.createBand(with: url, fileName: fileName) { bandURL in
                        if let url = bandURL {
                            completion?(true)
                            self.shareRing(fileUrl: url)
                        }else{
                            completion?(false)
                            dePrint("Failed to set, please try another")
                        }
                    }
                }else{
                    dePrint("downloadAndCacheFile = \(error?.localizedDescription)")
                    completion?(false)
                }
            }
        }else{
            dePrint("ringtone no http")
            completion?(false)
        }
                                   
       
    }
    
//    func shareBand(with fileURLString: String,
//                   fileName:String?,
//                   completion: ((Bool) -> Void)? = nil) {
//        
//        if checkGarageBandInstallation() == false {
//            completion?(false)
//            return
//        }
//        
//        if fileURLString.contains("http") {
//            TSLoadingAnimation.showLoading(in: self.targetVC?.view)
//            TSCommonTool.downloadAndCacheFile(from: fileURLString) { path, error in
//                TSLoadingAnimation.hideLoading()
//                if let path = path,let url = URL(string: path) {
//                    self.createBand(with: url, fileName: fileName) { bandURL in
//                        if let url = bandURL {
//                            completion?(true)
//                            self.shareRing(fileUrl: url)
//                        }else{
//                            completion?(false)
//                            dePrint("Failed to set, please try another")
//                        }
//                    }
//                }else{
//                    dePrint("downloadAndCacheFile = \(error?.localizedDescription)")
//                    completion?(false)
//                }
//            }
//        }else{
//            dePrint("ringtone no http")
//            completion?(false)
//        }
//    }
    
    // 创建用于分享库乐队的band文件
    func createBand(with fileURL: URL?, fileName: String?,
                    completion: ((URL?) -> Void)?) {
        let fileName = fileName ?? "Ringtone"
        guard let fileURL = fileURL,
              let bandPath = Self.bandPlacefolder,
              let directory = Self.ringDirectory else {
            completion?(nil)
            return
        }
        
        let bandURL = URL(fileURLWithPath: bandPath)
        let outputURL = URL(fileURLWithPath: directory).appendingPathComponent(fileName)
        
        audioConvertTool.convertToBand(from: fileURL, to: outputURL.path, bandFileURL: bandURL) { shareBandURL in
            completion?(shareBandURL)
        }
    }
    
    func shareRing(fileUrl: URL) {
        DispatchQueue.main.async {
            self.showTutorialVideo()
            
            let vc = UIActivityViewController(activityItems: [fileUrl], applicationActivities: nil)
            // 排除不需要的活动类型
            vc.excludedActivityTypes = [
                .postToFacebook,
                .postToTwitter,
                .postToWeibo,
                .message,
                .mail,
                .print,
                .copyToPasteboard,
                .assignToContact,
                .saveToCameraRoll,
                .addToReadingList,
                .postToFlickr,
                .postToVimeo,
                .postToTencentWeibo,
                .airDrop,
                .openInIBooks
            ]
            self.targetVC?.present(vc, animated: true, completion: {
                self.tryStartPictureInPicture()
            })
            
//            self.shareFileToGarageBand(fileURL: fileUrl)
        }
    }
    
    
    func shareFileToGarageBand(fileURL: URL) {
        // 假设你有一个文件的 URL
        // 创建 GarageBand 的 URL Scheme
        let garageBandURL = URL(string: "garageband://mobilegarageband203.audiobus://import?url=\(fileURL.absoluteString)")!
        
        // 检查是否可以打开 GarageBand
        if UIApplication.shared.canOpenURL(garageBandURL) {
            // 打开 GarageBand
            UIApplication.shared.open(garageBandURL, options: [:], completionHandler: nil)
        } else {
            // 如果 GarageBand 未安装，提示用户
            print("GarageBand 未安装")
        }
    }
    
}

extension TSBandRingTool : AVPlayerViewControllerDelegate, AVPictureInPictureControllerDelegate {
    
    func tryStartPictureInPicture() {
        guard let player = tutorialPlayer else {
            return
        }
        if player.status == .readyToPlay {
            self.pictureController?.startPictureInPicture()
            tutorialPlayer?.play()
            return
        }
        DispatchQueue.main.asyncAfter(deadline: .now()+0.1) {
            self.tryStartPictureInPicture()
        }
    }
    
    func showTutorialVideo() {
        guard let url = Bundle.main.url(forResource: "tutorial-ring", withExtension: "mp4") else {
            return
        }
        guard AVPictureInPictureController.isPictureInPictureSupported() else {
            return
        }
        do {
            try AVAudioSession.sharedInstance().setCategory(.playback, options: .mixWithOthers)
        } catch {
            
        }
        
        let playerItem = AVPlayerItem(asset: AVAsset(url: url))
        if tutorialPlayer == nil {
            tutorialPlayer = AVPlayer(playerItem: playerItem)
        } else {
            tutorialPlayer?.replaceCurrentItem(with: playerItem)
        }
        tutorialPlayer?.allowsExternalPlayback = true
        
        targetVC?.view.addSubview(playContentView)
        
        let layer = AVPlayerLayer(player: tutorialPlayer)
        layer.frame = CGRect(x: 0, y: 0, width: 1, height: 1)
        playContentView.layer.addSublayer(layer)

        let pictureVC = AVPictureInPictureController(playerLayer: layer)
        pictureVC?.delegate = self
        pictureController = pictureVC
        
    }
    
    func pictureInPictureControllerWillStartPictureInPicture(_ pictureInPictureController: AVPictureInPictureController) {
        playContentView.removeFromSuperview()
    }
    
    func playerViewControllerWillStartPictureInPicture(_ playerViewController: AVPlayerViewController) {
        
    }
    
    func playerViewControllerShouldAutomaticallyDismissAtPictureInPictureStart(_ playerViewController: AVPlayerViewController) -> Bool {
        return true
    }
    
    func playerViewController(_ playerViewController: AVPlayerViewController, restoreUserInterfaceForPictureInPictureStopWithCompletionHandler completionHandler: @escaping (Bool) -> Void) {
        DispatchQueue.main.async {
            if playerViewController.presentingViewController == nil {
                self.targetVC?.present(playerViewController, animated: false)
            }
            completionHandler(true)
        }
    }
    
    
}
extension TSBandRingTool {
    
    static var bandPlacefolder: String? {
        guard let cacheDirectory = ringDirectory else {
            return nil
        }
        let path = NSString(string: cacheDirectory).appendingPathComponent("placeHolderBand.band")
        
        // 步骤1
        if !FileManager.default.fileExists(atPath: path),
            let localFile = Bundle.main.path(forResource: "placeholder.band", ofType: nil) {
            try? FileManager.default.copyItem(atPath: localFile, toPath: path)
        }
        return path
    }
    
    // 铃声文件夹
    static var ringDirectory: String? {
        guard let documentUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else {
            return nil
        }
        let ringDirectory = documentUrl.appendingPathComponent("ringDirectory")
        
        // 创建文件夹
        return FileManager.default.getFolder(at: ringDirectory)?.path
    }
}


extension FileManager {
    // 获取文件夹，如果不存在，则创建, 返回空则意味着创建失败
    func getFolder(at url: URL) -> URL? {
        guard !isFolderExists(at: url) else {
            return url
        }
        do {
            try createDirectory(at: url, withIntermediateDirectories: true, attributes: nil)
            return url
        } catch {
            return nil
        }
    }
    
    func isFolderExists(at url: URL) -> Bool {
        var isDirectory: ObjCBool = false
        if fileExists(atPath: url.path, isDirectory: &isDirectory) && isDirectory.boolValue == true {
            return true // 文件夹已存在
        } else {
            return false // 文件夹不存在或者路径错误
        }
    }
}
