import type { SteamUser } from "@/types/steam"
import { store } from "@tauri-store/valtio"
import { DEFAULT_STORE_CONFIG } from "./config"
import { useSnapshot } from "valtio"
import { invoke } from "@tauri-apps/api/core"
import path from "path"

const defaultValue = {
  steamDir: "C:\\Program Files (x86)\\Steam",
  cs2Dir: "",
  steamDirValid: false,
  cs2DirValid: false,
  steamDirChecking: false,
  cs2DirChecking: false,
  users: [] as SteamUser[],
}

export const steamStore = store(
  "steam",
  { ...defaultValue },
  DEFAULT_STORE_CONFIG,
)

export const useSteamStore = () => {
  void steamStore.start
  const state = useSnapshot(steamStore.state)

  return {
    state,
    store: steamStore,
    cs2BaseDir,
    setDir,
    setCsDir,
    setUsers,
    setSteamDirValid,
    setCs2DirValid,
    checkSteamDirValid,
    checkCs2DirValid,
    currentUser,
    getUsers,
    selectUser,
    switchLoginUser,
    resetSteamStore,
  }
}

const cs2BaseDir = () => {
  return path.normalize(`${steamStore.state.cs2Dir.replaceAll("\\", "/")}/../../..`)
}

const setDir = (dir: string) => {
  steamStore.state.steamDir = dir
}
const setCsDir = (dir: string) => {
  steamStore.state.cs2Dir = dir
}
const setUsers = (users: SteamUser[]) => {
  steamStore.state.users = users
}
const setSteamDirValid = (valid: boolean) => {
  steamStore.state.steamDirValid = valid
}
const setCs2DirValid = (valid: boolean) => {
  steamStore.state.cs2DirValid = valid
}

const setSteamDirChecking = (checking: boolean) => {
  steamStore.state.steamDirChecking = checking
}

const setCs2DirChecking = (checking: boolean) => {
  steamStore.state.cs2DirChecking = checking
}

const checkSteamDirValid = async () => {
  setSteamDirChecking(true)
  const pathExist = await invoke<boolean>("check_path", { path: steamStore.state.steamDir })
  setSteamDirValid(pathExist)
  setTimeout(() => {
    setSteamDirChecking(false)
  }, 500)
}

const checkCs2DirValid = async () => {
  setCs2DirChecking(true)
  const pathExist = await invoke<boolean>("check_path", { path: steamStore.state.cs2Dir })
  setCs2DirValid(pathExist)
  setTimeout(() => {
    setCs2DirChecking(false)
  }, 500)
}

const currentUser = () => {
  return steamStore.state.users.at(0) || undefined
}

const getUsers = async () => {
  const users = await invoke<SteamUser[]>("get_steam_users", { steamDir: steamStore.state.steamDir })
  setUsers(users)
}

const selectUser = (index: number) => {
  const user = steamStore.state.users.at(index)
  if (user) {
    setUsers([
      user,
      ...steamStore.state.users.slice(0, index),
      ...steamStore.state.users.slice(index + 1),
    ])
  }
}

const switchLoginUser = async (index: number) => {
  const user = steamStore.state.users.at(index)
  if (user) {
    await invoke<SteamUser[]>("set_auto_login_user", { user: user.account_name })
  }
}

const resetSteamStore = () => {
  setDir(defaultValue.steamDir)
  setCsDir(defaultValue.cs2Dir)
  setUsers(defaultValue.users)
  setSteamDirValid(defaultValue.steamDirValid)
  setCs2DirValid(defaultValue.cs2DirValid)
  setSteamDirChecking(defaultValue.steamDirChecking)
  setCs2DirChecking(defaultValue.cs2DirChecking)
}