//
//  TSBusinessAudioPlayer.swift
//  AIRingtone
//
//  Created by 100Years on 2025/3/7.
//


class TSBusinessAudioPlayer {
    
    static let shared = TSBusinessAudioPlayer { state in
        kExecuteOnMainThread{
            NotificationCenter.default.post(name: .kBusinessAudioStateChange, object: nil, userInfo: ["PlayerState": state])
        }
    }
    
    enum PlayerState:Equatable {
        case play
        case pause
        case stop
        case loading(Float)
        case volume(Float)
    }
    
    private var audioPlayer: TSAudioPlayer?
    
    var stateChangeBlock:(PlayerState) -> Void
    
    var duration:Double{
        if let audioPlayer = audioPlayer {
            return audioPlayer.duration
        }
        return 0.0
    }

    var isPlaying:Bool{
        if let audioPlayer = audioPlayer {
            return audioPlayer.isPlaying
        }
        return false
    }
    
    
    //播放器是否可用
    var playerUsable:Bool {
        if let audioPlayer = audioPlayer {
            return audioPlayer.playerUsable
        }
        return false
    }
    
    //加载音乐可能 2-3 秒有结果,停止加载后播放.
    private var isStopPlayingAfterLoading:Bool = false
    private var loadingLogic:(show: () -> Void, hide: () -> Void)?
    init(stateChangeBlock:@escaping (PlayerState) -> Void) {
        self.stateChangeBlock = stateChangeBlock
    }
    
    func playRingtone(ringtone:String?) {
        if let ringtone = ringtone {
            loadingLogic = kCreateLoadingLogic { [weak self]  in
                guard let self = self else { return }
                self.stateChangeBlock(.loading(0.0))
            } hideBlock: {[weak self]  in
                guard let self = self else { return }
                self.stateChangeBlock(.loading(1.0))
            }

            loadingLogic?.show()
            isStopPlayingAfterLoading = false
            TSCommonTool.downloadAndCacheFile(from: ringtone) { [weak self] path, error in
                guard let self = self else { return }
                loadingLogic?.hide()
                
                if isStopPlayingAfterLoading == true{
                    self.stop()
                    isStopPlayingAfterLoading = false
                    return
                }
                
                if let path = path {
                    //播放
                    if let url = URL(string: path) {
                        self.audioPlayer = TSAudioPlayer(url: url)
                        self.audioPlayer?.setLoop(true)
                        setVolume(volume: 1.0)
                        self.play()
                        dePrint(self.audioPlayer?.duration)
                    }
                    
                }else{
                    //暂停
                    self.stop()
                }
            }
        }
    }
    
    
    func play() {
        self.audioPlayer?.play()
        stateChangeBlock(.play)
    }
    
    func stop() {
        isStopPlayingAfterLoading = true
        loadingLogic?.hide()
        self.audioPlayer?.stop()
        stateChangeBlock(.stop)
    }
    
    func pause() {
        isStopPlayingAfterLoading = true
        loadingLogic?.hide()
        self.audioPlayer?.pause()
        stateChangeBlock(.pause)
    }
    
    func setVolume(volume:Float){
        self.audioPlayer?.setVolume(volume)
        stateChangeBlock(.volume(volume))
    }
    
    func changeAudioSwitch()->Float {
        let volume:Float = self.audioPlayer?.volume == 0.0 ? 1.0 : 0.0
        setVolume(volume: volume)
        return volume
    }
     
    deinit {
        dePrint("TSBusinessAudioPlayer deinit")
    }
}
