from flask import Blueprint, request, jsonify
from windows_audio import WindowsAudioManager
from audio_io import AudioIO

device_bp = Blueprint('device', __name__)

audio_manager = WindowsAudioManager()
recorder = AudioIO()

# @device_bp.route('/device/set', methods=['POST'])
# def set_audio_device():
#     device_index = request.json.get('device_index')
#     try:
#         device_index = int(device_index)
#         print(f'HTTP: Setting audio device to index {device_index}')
#         sample_rate = audio_manager.set_default_input_device(device_index)
#         recorder.sample_rate = sample_rate
#         return jsonify({'status': 'device set', 'device_index': device_index, 'sample_rate': sample_rate})
#     except Exception as e:
#         return jsonify({'status': 'error', 'message': str(e)}), 400

# @device_bp.route('/device/get', methods=['GET'])
# def get_audio_device():
#     try:
#         device_info = audio_manager.get_default_device('input')
#         return jsonify({'status': 'success', 'device_info': device_info})
#     except Exception as e:
#         return jsonify({'status': 'error', 'message': str(e)}), 400 

@device_bp.route('/device/list', methods=['GET'])
def list_audio_devices():
    device_type = request.args.get('device_type', 'input')
    try:
        devices = audio_manager.list_audio_devices(device_type)
        return jsonify({'status': 'success', 'devices': devices})
    except Exception as e:
        return jsonify({'status': 'error', 'message': str(e)}), 400
