//
//  SoundPlayer.swift
//  LeCountdown
//
//  Created by Laurent Morvillier on 30/01/2023.
//

import Foundation
import AVFoundation

struct SoundFile {
    
    var filename: String
    var fileExtension: String
    
    init(fullName: String) throws {
        let components = fullName.components(separatedBy: ".")
        if components.count == 2 {
            self.filename = components[0]
            self.fileExtension = components[1]
        } else {
            throw SoundPlayerError.badFileName(name: fullName)
        }
    }
    
    var url: URL? {
        return Bundle.main.url(forResource: self.filename, withExtension: self.fileExtension)
    }
}

enum SoundPlayerError : Error {
    case missingResourceError(file: SoundFile)
    case badFileName(name: String)
}

@objc class SoundPlayer: NSObject, AVAudioPlayerDelegate {
    
    fileprivate var _player: AVAudioPlayer?

    func playSound(soundFile: SoundFile, repeats: Bool) throws {
        guard let url = soundFile.url else {
            throw SoundPlayerError.missingResourceError(file: soundFile)
        }
        
//        let audioSession: AVAudioSession = AVAudioSession.sharedInstance()
//        try audioSession.setCategory(.playback)
//        try audioSession.setActive(true)

        _player = try AVAudioPlayer(contentsOf: url)
        _player?.prepareToPlay()
//        let loopCount = repeats ? Int.max : 0
//        _player?.numberOfLoops = 0 //loopCount
        _player?.volume = 1.0
        _player?.delegate = self

//        do {
//            try AVAudioSession.sharedInstance().setCategory(.playAndRecord, mode: .default, options: [.allowBluetooth, .defaultToSpeaker])
//        } catch {
//            print("audioSession error = \(error)")
//        }

        _player?.play()
        
        
    }
    
    func stop() {
        self._player?.stop()
    }
    
    // MARK: - Delegate
    
    func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
        
    }
    
}
