import "@/styles/core.scss";
import { NavLink, Outlet, type LoaderFunction } from "react-router-dom";

import { useConfig } from "@/config";
import { accountStore, type User } from "@/stores";

import styles from "./index.module.scss";
import { getCookie, CookieKeys } from "@/ui/Shared";
import { Avatar } from "antd";
import { useSnapshot } from "valtio";

const NeosConfig = useConfig();
export const loader: LoaderFunction = () => {
  const user = getCookie<User>(CookieKeys.USER);
  if (user) accountStore.login(user);
  return null;
};

export const Component = () => {
  // TODO 根据是否登录，显示内容
  return (
    <>
      <nav className={styles.navbar}>
        <img
          className={styles.logo}
          src={`${NeosConfig.assetsPath}/neos-logo.svg`}
          alt="NEOS"
        />

        <NavLink to="/">主页</NavLink>
        <NavLink to="/match">匹配</NavLink>
        <NavLink to="/build">组卡</NavLink>
        <span style={{ flexGrow: 1 }} />
        <span className={styles.profile}>
          <NeosAvatar />
          <NavLink to="/profile">个人中心</NavLink>
        </span>
      </nav>
      <Outlet />
    </>
  );
};

const NeosAvatar = () => {
  const { user } = useSnapshot(accountStore);
  return <Avatar size="small" src={user?.avatar_url} />;
};
