import { Box, Stack, Text, Divider, Group, Button, Anchor } from "@mantine/core";
import { Suspense, useState, useDeferredValue } from "react";
import { Trans, useLingui } from "@lingui/react/macro";
import { Link } from "@tanstack/react-router";
import Header from "./header";
import SwipeableTabs from "@/components/swipeable-tabs";
import { usePlayer, usePlayerMatches, usePlayerStats } from "../../queries";
import TeamList from "@/features/teams/components/team-list";
import StatsOverview, { StatsSkeleton } from "@/components/stats-overview";
import MatchList from "@/features/matches/components/match-list";
import BadgeShowcase from "@/features/badges/components/badge-showcase";
import BadgeShowcaseSkeleton from "@/features/badges/components/badge-showcase-skeleton";

interface ProfileProps {
  id: string;
}

const StatsWithFilter = ({ id }: { id: string }) => {
  const [viewType, setViewType] = useState<'all' | 'mainline' | 'regional'>('all');
  const deferredViewType = useDeferredValue(viewType);
  const isStale = viewType !== deferredViewType;

  return (
    <Stack>
      <Group gap="xs" px="md" justify="space-between" align="center">
        <Text size="md" fw={700}><Trans>Statistics</Trans></Text>
        <Group gap="xs">
          <Button variant={viewType === 'all' ? 'filled' : 'light'} size="compact-xs" onClick={() => setViewType('all')}><Trans>All</Trans></Button>
          <Button variant={viewType === 'mainline' ? 'filled' : 'light'} size="compact-xs" onClick={() => setViewType('mainline')}><Trans>Mainline</Trans></Button>
          <Button variant={viewType === 'regional' ? 'filled' : 'light'} size="compact-xs" onClick={() => setViewType('regional')}><Trans>Regional</Trans></Button>
        </Group>
      </Group>
      <Box style={{ opacity: isStale ? 0.6 : 1, transition: 'opacity 150ms' }}>
        <Suspense fallback={<StatsSkeleton />}>
          <StatsContent id={id} viewType={deferredViewType} />
        </Suspense>
      </Box>
    </Stack>
  );
};

const StatsContent = ({ id, viewType }: { id: string; viewType: 'all' | 'mainline' | 'regional' }) => {
  const { data: stats } = usePlayerStats(id, viewType);
  return <StatsOverview statsData={stats} />;
};

const Profile = ({ id }: ProfileProps) => {
  const { t } = useLingui();
  const { data: player } = usePlayer(id);
  const { data: matches } = usePlayerMatches(id);

  const tabs = [
    {
      label: t`Overview`,
      value: "overview",
      content: <>
        <Stack px="md">
          <Group justify="space-between" align="center">
            <Text size="md" fw={700}><Trans>Badges</Trans></Text>
            <Anchor component={Link} to="/badges" size="sm" fw={500}>
              <Trans>View all badges</Trans>
            </Anchor>
          </Group>
          <Suspense fallback={<BadgeShowcaseSkeleton />}>
            <BadgeShowcase playerId={id} />
          </Suspense>
        </Stack>
        <Divider my="md" />
        <StatsWithFilter id={id} />
      </>,
    },
    {
      label: t`Matches`,
      value: "matches",
      content: <MatchList matches={matches || []} />,
    },
    {
      label: t`Teams`,
      value: "teams",
      content: <TeamList teams={player.teams || []} />,
    },
  ];

  return (
    <>
      <Header player={player} />
      <Box mt="lg">
        <SwipeableTabs tabs={tabs} />
      </Box>
    </>
  );
};

export default Profile;
