import { create } from 'zustand';
import type { VideoSource } from '../types/player';

interface PlayerStore {
  source: VideoSource | null;
  playing: boolean;
  currentTime: number;
  duration: number;
  fps: number;
  volume: number;
  muted: boolean;
  playbackRate: number;
  hlsReady: boolean;
  /** 영상 첫 프레임이 표시 가능(loadeddata)해진 후 true. 오버레이를 영상보다 먼저 그리지 않도록 게이트. */
  videoReady: boolean;
  /** 영상 원본(intrinsic) 해상도 (px). object-fit:cover 크롭 영역 계산 → 오버레이 정렬용. */
  videoWidth: number;
  videoHeight: number;
  setSource: (source: VideoSource | null) => void;
  setPlaying: (playing: boolean) => void;
  setCurrentTime: (t: number) => void;
  setDuration: (d: number) => void;
  setFps: (fps: number) => void;
  setVolume: (v: number) => void;
  setMuted: (m: boolean) => void;
  setPlaybackRate: (r: number) => void;
  setHlsReady: (ready: boolean) => void;
  setVideoReady: (ready: boolean) => void;
  setVideoSize: (w: number, h: number) => void;
}

export const usePlayerStore = create<PlayerStore>((set) => ({
  source: null,
  playing: false,
  currentTime: 0,
  duration: 0,
  fps: 30,
  volume: 1,
  muted: false,
  playbackRate: 1,
  hlsReady: false,
  videoReady: false,
  videoWidth: 0,
  videoHeight: 0,
  setSource: (source) => set({ source, hlsReady: false, videoReady: false }),
  setPlaying: (playing) => set({ playing }),
  setCurrentTime: (currentTime) => set({ currentTime }),
  setDuration: (duration) => set({ duration }),
  setFps: (fps) => set({ fps }),
  setVolume: (volume) => set({ volume }),
  setMuted: (muted) => set({ muted }),
  setPlaybackRate: (playbackRate) => set({ playbackRate }),
  setHlsReady: (hlsReady) => set({ hlsReady }),
  setVideoReady: (videoReady) => set({ videoReady }),
  setVideoSize: (videoWidth, videoHeight) => set({ videoWidth, videoHeight }),
}));
