//
//  DevicesUtil.swift
//  Gami
//
//  Created by OneeChan on 2026/2/6.
//

import Foundation
import AVFAudio

class DevicesUtil {
    static var isBluetoothHeadsetConnected: Bool {
        let audioSession = AVAudioSession.sharedInstance()
        let currentRoute = audioSession.currentRoute
        for output in currentRoute.outputs {
            if output.portType == .bluetoothA2DP ||
                output.portType == .bluetoothLE ||
                output.portType == .bluetoothHFP {
                return true
            }
        }
        guard let availableInputs = audioSession.availableInputs else {
            return false
        }
        for input in availableInputs {
            if input.portType == .bluetoothA2DP ||
                input.portType == .bluetoothLE ||
                input.portType == .bluetoothHFP {
                return true
            }
        }
        return false
   }
    
    static var isBluetoothHeadsetActive: Bool {
        let audioSession = AVAudioSession.sharedInstance()
        let currentRoute = audioSession.currentRoute
        for output in currentRoute.outputs {
            switch output.portType {
            case .bluetoothA2DP, .bluetoothLE, .bluetoothHFP:
                return true
            default:
                continue
            }
        }
        return false
    }

    static var getCurrentOutputDeviceName: String? {
        let audioSession = AVAudioSession.sharedInstance()
        let currentRoute = audioSession.currentRoute
        if let currentOutput = currentRoute.outputs.first {
            return currentOutput.portName
        }
        return nil
    }
}
