import { LocalNotifications } from "@capacitor/local-notifications";
import { App } from "@capacitor/app";
import { Howl } from "howler";
import txtNotify from "@/assets/mp3/im.mp3"
import voiceNotify from "@/assets/mp3/voice.mp3"
import { useWebSocketStore } from "@/stores/modules/webSocketStore";

const soundIm = new Howl({
  src: [txtNotify],
  html5: false, // 强制使用HTML5 Audio
  format: ["mp3"], // 指定格式
  preload: true, // 预加载
  volume: 0.6, // 音量
  pool: 16, // 可以同时播放3个实例
  onloaderror: (id, error) => {
    console.error("加载失败:", error);
  },
});

export const soundVoice = new Howl({
  src: [voiceNotify],
  html5: false, // 强制使用HTML5 Audio
  format: ["mp3"], // 指定格式
  preload: true, // 预加载
  volume: 0.6, // 音量
  pool: 3, // 可以同时播放3个实例
  loop: true,
  onloaderror: (id, error) => {
    console.error("加载失败:", error);
  },
});
let id = 1
// 初始化通知监听
export async function setupNotifications() {
  // 检查权限
  const granted = await LocalNotifications.requestPermissions();
  
  if (!granted) return;
  id += 1
  console.log('id:', id);
  await LocalNotifications.schedule({
    notifications: [
      {
        title: "新消息",
        body: "您收到一条新消息",
        id,
        extra: { messageId: "123" },
      },
    ],
  }); 
  soundIm.play();
}


// App 状态变化时处理
App.addListener("appStateChange", ({ isActive }) => {
  const wsStore = useWebSocketStore();
  wsStore.changeAppState(isActive);
  console.log("App active:", isActive);
});
