//
//  TSThemeSetVM.swift
//  AIRingtone
//
//  Created by 100Years on 2025/3/3.
//

class TSThemeSetVM {
    var audioPlayer: TSAudioPlayer?
    
    func playRingtone(ringtone:String?) {
        if let ringtone = ringtone {
            self.audioPlayer?.stop()
            TSCommonTool.downloadAndCacheFile(from: ringtone) { [self] path, error in
                if let path = path {
                    //播放
                    if let url = URL(string: path) {
                        self.audioPlayer = TSAudioPlayer(url: url)
                        self.audioPlayer?.setLoop(true)
                        self.audioPlayer?.setVolume(1.0)
                        dePrint(self.audioPlayer?.duration)
                    }
                    
                }else{
                    //暂停
                    self.audioPlayer?.stop()
                }
            }
        }
    }

    func changeAudioState(isPlay:Bool) {
        if isPlay {
            self.audioPlayer?.play()
        }else{
            self.audioPlayer?.pause()
        }
    }
    
}




extension TSThemeSetVM{

    func floatToMinuteSecond(_ time: Float) -> String {
        // 将 Float 类型的时间转换为整数秒
        let totalSeconds = Int(time)
        // 计算分钟数
        let minutes = totalSeconds / 60
        // 计算剩余的秒数
        let seconds = totalSeconds % 60
        // 将分钟数和秒数格式化为两位数的字符串
        let minuteString = String(format: "%02d", minutes)
        let secondString = String(format: "%02d", seconds)
        // 拼接成分秒格式的字符串
        return "\(minuteString):\(secondString)"
    }

}

extension TSThemeSetVM{
    

}
