import { FastifyInstance } from "fastify";
import mainWindow from "../../main/windows/main-window";

/**
 * 播放控制接口
 * @param fastify Fastify 实例
 */
export const initControlAPI = async (fastify: FastifyInstance) => {
  // 播放控制路由前缀
  await fastify.register(
    async (fastify) => {
      // 播放
      fastify.get("/play", async (_request, reply) => {
        try {
          const mainWin = mainWindow.getWin();
          if (!mainWin) {
            return reply.code(500).send({
              code: 500,
              message: "主窗口未找到",
              data: null,
            });
          }

          mainWin.webContents.send("play");

          return reply.send({
            code: 200,
            message: "播放命令已发送",
            data: null,
          });
        } catch (error) {
          return reply.code(500).send({
            code: 500,
            message: "播放失败",
            data: error,
          });
        }
      });

      // 暂停
      fastify.get("/pause", async (_request, reply) => {
        try {
          const mainWin = mainWindow.getWin();
          if (!mainWin) {
            return reply.code(500).send({
              code: 500,
              message: "主窗口未找到",
              data: null,
            });
          }

          mainWin.webContents.send("pause");

          return reply.send({
            code: 200,
            message: "暂停命令已发送",
            data: null,
          });
        } catch (error) {
          return reply.code(500).send({
            code: 500,
            message: "暂停失败",
            data: error,
          });
        }
      });

      // 播放/暂停切换
      fastify.get("/toggle", async (_request, reply) => {
        try {
          const mainWin = mainWindow.getWin();
          if (!mainWin) {
            return reply.code(500).send({
              code: 500,
              message: "主窗口未找到",
              data: null,
            });
          }

          // 这里可以根据当前播放状态来决定发送 play 还是 pause
          // 暂时先发送 toggle 事件，如果渲染进程支持的话
          mainWin.webContents.send("toggle");

          return reply.send({
            code: 200,
            message: "播放/暂停切换命令已发送",
            data: null,
          });
        } catch (error) {
          return reply.code(500).send({
            code: 500,
            message: "播放/暂停切换失败",
            data: error,
          });
        }
      });

      // 下一曲
      fastify.get("/next", async (_request, reply) => {
        try {
          const mainWin = mainWindow.getWin();
          if (!mainWin) {
            return reply.code(500).send({
              code: 500,
              message: "主窗口未找到",
              data: null,
            });
          }

          mainWin.webContents.send("playNext");

          return reply.send({
            code: 200,
            message: "下一曲命令已发送",
            data: null,
          });
        } catch (error) {
          return reply.code(500).send({
            code: 500,
            message: "下一曲失败",
            data: error,
          });
        }
      });

      // 上一曲
      fastify.get("/prev", async (_request, reply) => {
        try {
          const mainWin = mainWindow.getWin();
          if (!mainWin) {
            return reply.code(500).send({
              code: 500,
              message: "主窗口未找到",
              data: null,
            });
          }

          mainWin.webContents.send("playPrev");

          return reply.send({
            code: 200,
            message: "上一曲命令已发送",
            data: null,
          });
        } catch (error) {
          return reply.code(500).send({
            code: 500,
            message: "上一曲失败",
            data: error,
          });
        }
      });

      // 获取播放状态（可选功能）
      fastify.get("/status", async (_request, reply) => {
        try {
          const mainWin = mainWindow.getWin();
          if (!mainWin) {
            return reply.code(500).send({
              code: 500,
              message: "主窗口未找到",
              data: null,
            });
          }

          // 这里可以通过 IPC 获取当前播放状态
          // 暂时返回基本信息
          return reply.send({
            code: 200,
            message: "获取状态成功",
            data: {
              connected: true,
              window: "available",
            },
          });
        } catch (error) {
          return reply.code(500).send({
            code: 500,
            message: "获取状态失败",
            data: error,
          });
        }
      });
    },
    { prefix: "/control" },
  );
};
