import { SteamUser } from '@/types/steam'
import { tauriStore } from '@/utils/persist'
import { create } from 'zustand'
import { persist, createJSONStorage } from 'zustand/middleware'

const mock_user: SteamUser = {
  steamID64: "76561198052315353",
  steamID32: "STEAM_0:0:46157676",
  accountName: "wrr",
  personaName: "wrr",
  recent: 0,
  avatar: "",
  // "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/0d/0d1d4b9e1a9e8b2b3d4d0a7e1b3d6b1e3f8f4f7.jpg",
}

interface SteamState {
  dir: string,
  csDir: string,
  users: SteamUser[],
  isDirValid: boolean,
  isCsDirValid: boolean,
}

interface SteamAction {
  setDir: (dir: string) => void,
  setCsDir: (dir: string) => void,
  setUsers: (users: SteamUser[]) => void,
  setIsDirValid: (valid: boolean) => void,
  setIsCsDirValid: (valid: boolean) => void,
  currentUser: () => SteamUser,
}

const STORE_NAME = "steam"
const { /* store,  */ storage } = tauriStore(STORE_NAME)

const useSteamStore = create<SteamState & SteamAction>()(
  persist(
    (set, get) => ({
      dir: "C:\\Program Files (x86)\\Steam",
      csDir: "",
      users: [mock_user] as SteamUser[],
      isDirValid: false,
      isCsDirValid: false,
      setDir: (dir: string) => set(() => ({ dir: dir })),
      setCsDir: (dir: string) => set(() => ({ csDir: dir })),
      setUsers: (users: SteamUser[]) => set(() => ({ users: users })),
      setIsDirValid: (valid: boolean) => set(() => ({ isDirValid: valid })),
      setIsCsDirValid: (valid: boolean) => set(() => ({ isCsDirValid: valid })),
      currentUser: () => {
        return get().users[0] || mock_user
      },
    }),
    {
      name: STORE_NAME, // name of item in the storage (must be unique)
      storage: createJSONStorage(() => storage), // (optional) by default the 'localStorage' is used
    }
  )
)

export default useSteamStore