import React, { useEffect, useMemo } from "react";
import { Link, useLoaderData, useNavigate } from "react-router";
import { Avatar, Divider, Flex, Image, Typography } from "antd";
import { useTitle } from "ahooks";
import type { XCXProfile } from "../types/profile";
import User from "../components/User";
import { ChangeBackground } from "../components/ChangeBackground";
import UserTags from "../components/Tags";
import { GameTable } from "../components/GameTable";
import { FavButton } from "../components/FavButton";
import { useFavPlayerStore } from "../store/useFavPlayerStore";

function Honor(props: { honors?: XCXProfile['honors'] }) {
  if (!props.honors?.length) return null;
  return (
    <>
      <Divider>荣誉</Divider>
      <Flex vertical gap={12}>
        {props.honors?.map(honor => {
          return (
            <Flex key={honor.hid} gap={12} style={{ width: '100%' }}>
              <Image style={{ mixBlendMode: 'multiply' }} src={honor.honor} />
              <Link to={`/event/${honor.eventid}`}>
                <Typography.Text>
                  {honor.subject}
                </Typography.Text>
              </Link>
            </Flex>
          );
        })}
      </Flex>
    </>
  );
}

function Raket(props: { profile?: XCXProfile | null }) {
  const {
    qiupaitype = '',
    zhengshoutype = '',
    fanshoutype = '',
    qiupai = '',
    zhengshou = '',
    fanshou = '',
  } = props.profile || {};
  if ([qiupaitype, zhengshoutype, fanshoutype].every(e => !e)) {
    return null;
  }
  return (
    <>
      <Divider>装备</Divider>
      <Flex vertical gap={8}>
        <Flex gap={4} align="center">
          <Typography.Text type="secondary">底板：</Typography.Text>
          <Typography.Text>{qiupaitype}</Typography.Text>
          <Typography.Text type="secondary" style={{ marginLeft: 4 }}>({qiupai})</Typography.Text>
        </Flex>
        <Flex gap={4} align="center">
          <Typography.Text type="secondary">正手：</Typography.Text>
          <Typography.Text>{zhengshoutype}</Typography.Text>
          <Typography.Text type="secondary" style={{ marginLeft: 4 }}>({zhengshou})</Typography.Text>
        </Flex>
        <Flex gap={4} align="center">
          <Typography.Text type="secondary">反手：</Typography.Text>
          <Typography.Text>{fanshoutype}</Typography.Text>
          <Typography.Text type="secondary" style={{ marginLeft: 4 }}>({fanshou})</Typography.Text>
        </Flex>
      </Flex>
    </>
  );
}

function PlayerList(props: { title: string; names?: string[]; uids?: string[] }) {
  const { names = [], uids = [] } = props;
  if (!names.length) return null;
  return (
    <>
      <Typography.Title level={5}>{props.title}</Typography.Title>
      <Flex vertical align="center">
        {names.map((e, i) => (
          <User key={e} name={e} uid={uids?.[i] ?? ''} />
        ))}
      </Flex>
    </>
  );
}

export default function ProfilePage() {
  const { profile, uid } = useLoaderData<{ profile: XCXProfile; uid: string }>();
  console.debug('profile', profile);
  const navigate = useNavigate();
  useTitle(`${profile?.username}${profile?.realname ? ` (${profile?.realname})` : ''} 个人详情`, { restoreOnUnmount: true });
  const tags = useMemo<React.ReactNode[]>(() =>
    [profile?.province, profile?.sex, profile?.bg ?? '', ...Object.values(profile?.allCities ?? [])]
      .filter(Boolean)
  , [profile]);
  const favUser = useMemo(() => ({
    uid,
    score: profile.score,
    name: profile.username,
    realname: profile.realname,
    avatar: profile.realpic
  }), [profile]);
  useEffect(() => {
    if (!useFavPlayerStore.getState().isFav(uid)) return;
    useFavPlayerStore.getState().fav(favUser);
  }, [favUser]);
  return (
    <>
      <ChangeBackground url={profile?.realpic} />
      <Flex vertical align="center" style={{ padding: 24 }}>
        <Avatar src={profile?.realpic} size={128} />
        <Typography.Title level={2}>
          <Flex justify="center">
            <span>{profile?.username}</span>
            <FavButton
              user={{
                uid,
                score: profile.score,
                name: profile.username,
                realname: profile.realname,
                avatar: profile.realpic
              }}
            />
          </Flex>
        </Typography.Title>
        <Flex vertical gap={4}>
          <Flex gap={4}>
            <Typography.Text>姓名：</Typography.Text>
            <Typography.Text>{profile?.realname}</Typography.Text>
          </Flex>
          <Flex gap={4}>
            <Typography.Text>积分：</Typography.Text>
            <Typography.Text>{profile?.score}</Typography.Text>
          </Flex>
          <Flex gap={4}>
            <Typography.Text>排名：</Typography.Text>
            <Typography.Text>{profile?.rank}</Typography.Text>
          </Flex>
        </Flex>
        <Typography.Text style={{ textAlign: 'center', marginTop: 4 }}>
          {(tags.length > 0 && tags.reduce((a, b) => (<>{a}<Divider orientation="vertical" />{b}</>)))}
        </Typography.Text>
        <Divider>简介</Divider>
        <Typography.Paragraph>
          {profile?.description}
        </Typography.Paragraph>
        <UserTags uid={uid} />
        <Raket profile={profile} />
        <Flex wrap gap={24} justify="center">
          <Flex vertical align="center">
            <PlayerList title="交手前三名" names={profile?.TopPlayerUsernameScore} />
          </Flex>
          <Flex vertical align="center">
            <PlayerList title="战胜前三名" names={profile?.Top3OfBeatUsernameScore} />
          </Flex>
          <Flex vertical align="center">
            <PlayerList title="战胜的男子" names={profile?.Top3ManOfBeatUsernameScore} />
          </Flex>
          <Flex vertical align="center">
            <PlayerList title="战胜的女子" names={profile?.Top3WomanOfBeatUsernameScore} />
          </Flex>
        </Flex>
        <Flex gap={24}>
          <Flex vertical align="center">
            <PlayerList title="福星" names={profile?.FuXing?.names} uids={profile?.FuXing?.uids} />
          </Flex>
          <Flex vertical align="center">
            <PlayerList title="苦主" names={profile?.KuZhu?.names} uids={profile?.KuZhu?.uids} />
          </Flex>
        </Flex>
        <Honor honors={profile?.honors} />
        <GameTable uid={uid} data={profile?.games?.data} />
      </Flex>
    </>
  );
}