"""
Poe2VisualTool Sound Manager (Haptic Audio Feedback)
Phát tín hiệu âm thanh tinh tế khi các sự kiện quan trọng diễn ra:
  - Gắn kết thành công vào game Path of Exile 2 (Attach Success)
  - Khôi phục khẩn cấp bộ nhớ (1-Frame Panic Restore)
  - Kích hoạt / Hủy bỏ Zoom hoặc Fog Removal
Sử dụng Win32 Multimedia API không gây gián đoạn luồng chính.
Bản quyền 2026 Poe2VisualTool.
"""

from __future__ import annotations

import os
import threading
from typing import Optional

try:
    import winsound
except ImportError:
    winsound = None


class SoundManager:
    """
    Trình quản lý âm thanh phản hồi cho Poe2VisualTool.
    """

    def __init__(self, enabled: bool = True):
        self.enabled = enabled

    def play_attach_success(self):
        """Phát âm thanh êm dịu báo hiệu đã kết nối an toàn vào Path of Exile 2."""
        if not self.enabled or winsound is None:
            return

        def _play():
            try:
                # 2 nốt nhạc đi lên êm tai: 523Hz (C5) -> 659Hz (E5)
                winsound.Beep(523, 70)
                winsound.Beep(659, 120)
            except Exception:
                try:
                    winsound.MessageBeep(winsound.MB_OK)
                except Exception:
                    pass

        threading.Thread(target=_play, daemon=True).start()

    def play_panic_restore(self):
        """Phát âm thanh cảnh báo khi kích hoạt 1-Frame Panic Restore."""
        if not self.enabled or winsound is None:
            return

        def _play():
            try:
                # 2 nốt nhạc đi xuống nhanh: 880Hz (A5) -> 440Hz (A4)
                winsound.Beep(880, 80)
                winsound.Beep(440, 160)
            except Exception:
                try:
                    winsound.MessageBeep(winsound.MB_ICONEXCLAMATION)
                except Exception:
                    pass

        threading.Thread(target=_play, daemon=True).start()

    def play_toggle(self, is_on: bool):
        """Âm thanh click nhẹ khi bật/tắt tính năng."""
        if not self.enabled or winsound is None:
            return

        def _play():
            try:
                freq = 784 if is_on else 494
                winsound.Beep(freq, 40)
            except Exception:
                pass

        threading.Thread(target=_play, daemon=True).start()


# Khởi tạo instance toàn cục mặc định
sound_manager = SoundManager(enabled=True)
