import { defineStore } from "pinia";
import type { SortType } from "@/types/main";
import type { PlayModeType, RGB, ColorScheme } from "@/types/main";

interface StatusState {
  /** 菜单折叠状态 */
  menuCollapsed: boolean;
  /** 搜索框状态 */
  searchFocus: boolean;
  /** 搜索框输入值 */
  searchInputValue: string;
  /** 播放控制条 */
  showPlayBar: boolean;
  /** 全屏播放器 */
  showFullPlayer: boolean;
  /** 全屏播放器激活状态 */
  fullPlayerActive: boolean;
  /** 播放器功能显示 */
  playerMetaShow: boolean;
  /** 播放列表状态 */
  playListShow: boolean;
  /** 播放状态 */
  playStatus: boolean;
  /** 播放加载状态 */
  playLoading: boolean;
  /** 播放速度 */
  playRate: number;
  /** 播放音量 */
  playVolume: number;
  /** 静音前音量 */
  playVolumeMute: number;
  /** 播放模式 */
  playSongMode: PlayModeType;
  /** 心动模式 */
  playHeartbeatMode: boolean;
  /** 封面主题 */
  songCoverTheme: {
    /** 封面主题颜色 */
    main?: RGB;
    /** 封面主题颜色（亮色） */
    light?: ColorScheme;
    /** 封面主题颜色（暗色） */
    dark?: ColorScheme;
  };
  /** 音乐频谱数据 */
  spectrumsData: number[];
  /** 纯净歌词模式 */
  pureLyricMode: boolean;
  /** 当前播放索引 */
  playIndex: number;
  /** 歌词播放索引 */
  lyricIndex: number;
  /** 当前播放时间 */
  currentTime: number;
  /** 歌曲总时长 */
  duration: number;
  /** 实时播放进度 */
  progress: number;
  /** 进度偏移 */
  currentTimeOffset: number;
  /** 是否为解锁歌曲 */
  playUblock: boolean;
  /** 主内容高度 */
  mainContentHeight: number;
  /** 列表排序 */
  listSort: SortType;
  /** 桌面歌词 */
  showDesktopLyric: boolean;
  /** 播放器评论 */
  showPlayerComment: boolean;
  /** 私人FM模式 */
  personalFmMode: boolean;
  /** 更新检查 */
  updateCheck: boolean;
  /** 自动关闭 */
  autoClose: {
    /** 自动关闭 */
    enable: boolean;
    /** 自动关闭时间（分钟） */
    time: number;
    /** 剩余时长（秒） */
    remainTime: number;
    /** 等待歌曲结束 */
    waitSongEnd: boolean;
  };
}

export const useStatusStore = defineStore("status", {
  state: (): StatusState => ({
    menuCollapsed: false,
    searchFocus: false,
    searchInputValue: "",
    showPlayBar: true,
    playStatus: false,
    playLoading: false,
    playUblock: false,
    playListShow: false,
    showFullPlayer: false,
    fullPlayerActive: false,
    playerMetaShow: true,
    currentTime: 0,
    duration: 0,
    progress: 0,
    currentTimeOffset: 0,
    songCoverTheme: {},
    pureLyricMode: false,
    spectrumsData: [],
    playIndex: -1,
    lyricIndex: -1,
    playRate: 1,
    playVolume: 0.7,
    playVolumeMute: 0,
    playSongMode: "repeat",
    playHeartbeatMode: false,
    personalFmMode: false,
    mainContentHeight: 0,
    listSort: "default",
    showDesktopLyric: false,
    showPlayerComment: false,
    updateCheck: false,
    autoClose: {
      enable: false,
      time: 30,
      remainTime: 0,
      waitSongEnd: true,
    },
  }),
  getters: {
    // 播放音量图标
    playVolumeIcon(state) {
      const volume = state.playVolume;
      return volume === 0
        ? "VolumeOff"
        : volume < 0.4
          ? "VolumeMute"
          : volume < 0.7
            ? "VolumeDown"
            : "VolumeUp";
    },
    // 播放模式图标
    playModeIcon(state) {
      const mode = state.playSongMode;
      return state.playHeartbeatMode
        ? "HeartBit"
        : mode === "repeat"
          ? "Repeat"
          : mode === "repeat-once"
            ? "RepeatSong"
            : "Shuffle";
    },
    // 音量百分比
    playVolumePercent(state) {
      return Math.round(state.playVolume * 100);
    },
    // 播放器主色
    mainColor(state) {
      const mainColor = state.songCoverTheme?.main;
      if (!mainColor) return "239, 239, 239";
      return `${mainColor.r}, ${mainColor.g}, ${mainColor.b}`;
    },
  },
  actions: {},
  // 持久化
  persist: {
    key: "status-store",
    storage: localStorage,
    pick: [
      "menuCollapsed",
      "currentTime",
      "duration",
      "progress",
      "pureLyricMode",
      "playIndex",
      "playRate",
      "playVolume",
      "playVolumeMute",
      "playSongType",
      "playSongMode",
      "songCoverTheme",
      "listSort",
      "showDesktopLyric",
      "playHeartbeatMode",
      "personalFmMode",
      "autoClose",
    ],
  },
});
