import { addToast } from "@heroui/react"
import { FolderFocusOne } from "@icon-park/react"
import { Card, CardBody, CardHeader, CardIcon } from "../window/Card"
import { invoke } from "@tauri-apps/api/core"
import { useSteamStore } from "@/store/steam"
import path from "path"

interface RoundedButtonProps {
  children?: React.ReactNode
}

const RoundedButton = ({
  children,
  ...props
}: RoundedButtonProps & React.ButtonHTMLAttributes<HTMLButtonElement>) => {
  return (
    <button
      type="button"
      className="flex items-center justify-center px-3 py-1 transition rounded-full select-none min-w-fit active:scale-95 hover:bg-black/10 text-zinc-700 dark:text-zinc-100 bg-black/5 dark:bg-white/5"
      {...props}
    >
      {children}
    </button>
  )
}

const CommonDir = () => {
  const steam = useSteamStore()

  return (
    <Card>
      <CardHeader>
        <CardIcon>
          <FolderFocusOne /> 常用目录
        </CardIcon>
      </CardHeader>
      <CardBody>
        <div className="flex gap-1.5">
          <RoundedButton
            onClick={async () => {
              await invoke("open_path", {
                path: steam.state.steamDir,
              })
              addToast({ title: "Steam安装目录" })
            }}
          >
            Steam安装目录
          </RoundedButton>
          <RoundedButton
            onClick={async () => {
              await invoke("open_path", {
                path: steam.cs2BaseDir(),
              })
              addToast({ title: "CS2游戏目录" })
            }}
          >
            CS2游戏目录
          </RoundedButton>
          <RoundedButton
            onClick={async () => {
              await invoke("open_path", {
                path: path.resolve(steam.cs2BaseDir(), "game", "csgo", "maps"),
              })
              addToast({ title: "地图目录" })
            }}
          >
            地图目录
          </RoundedButton>
          <RoundedButton
            onClick={async () => {
              await invoke("open_path", {
                path: path.resolve(steam.cs2BaseDir(), "game", "csgo", "cfg"),
              })
              addToast({ title: "游戏CFG" })
            }}
          >
            游戏CFG
          </RoundedButton>
          <RoundedButton
            onClick={async () => {
              const user = steam.currentUser()
              if (!user) {
                addToast({ title: "请先选择用户", color: "danger" })
                return
              }
              await invoke("open_path", {
                path: path.resolve(steam.state.steamDir, "userdata", user.steam_id32.toString(), "730", "local", "cfg"),
              })
              addToast({ title: "个人CFG" })
            }}
          >
            个人CFG
          </RoundedButton>
          <RoundedButton
            onClick={async () => {
              await invoke("open_path", {
                path: path.resolve(steam.cs2BaseDir(), "game", "csgo", "replays"),
              })
              addToast({ title: "官匹录像" })
            }}
          >
            官匹录像
          </RoundedButton>
          <RoundedButton
            onClick={async () => {
              await invoke("open_path", { path: `%appdata%/Wmpvp/demo` })
              addToast({ title: "完美平台录像" })
            }}
          >
            完美平台录像
          </RoundedButton>
          <RoundedButton
            onClick={async () => {
              await invoke("open_path", { path: `%appdata%/5E对战平台/demo` })
              addToast({ title: "5E平台录像" })
            }}
          >
            5E平台录像
          </RoundedButton>
        </div>
      </CardBody>
    </Card>
  )
}

export default CommonDir
