/* eslint-disable react-hooks/set-state-in-effect */
"use client";

import { Fragment, useEffect, useState } from "react";
import {
  Box,
  Button,
  Heading,
  Icon,
  Link,
  Skeleton,
  Text,
  VStack
} from "@chakra-ui/react";
import { useQuery } from "@urql/next";
import lineChartArr from "@/lib/lineChartArray";
import LinksDeletedChart from "./LinksDeletedChart";
import { LineChartArr } from "@/types/LineChartStats";
import CommandResponsesChart from "./CommandResponsesChart";
import TimedTriggeredChart from "./TimesTriggeredChart";
import GetStatsRange from "@/graphql/queries/getStatsRange";
import StatsList from "./StatsList";
import GetTotalGroupsQuery from "@/graphql/queries/getTotalGroups";
import GetTotalStatsQuery from "@/graphql/queries/getTotalStats";
import GetTodaysStatsQuery from "@/graphql/queries/getTodaysStats";
import Image from "next/image";

export default function Home() {
  // * Total Groups * //

  const [
    {
      fetching: totalGroupsFetching,
      data: totalGroups,
      error: totalGroupsError
    }
  ] = useQuery({
    query: GetTotalGroupsQuery
  });

  // * Total Stats * //

  const [
    { fetching: totalStatsFetching, data: totalStats, error: totalStatsError }
  ] = useQuery({
    query: GetTotalStatsQuery
  });

  // * Today's Stats * //

  const [
    { fetching: todaysStatsFetching, data: todayStats, error: todaysStatsError }
  ] = useQuery({
    query: GetTodaysStatsQuery
  });

  // * 30 Day Stats * //

  // Dates
  const [currDate, setCurrDate] = useState<Date>();
  const [date30DaysAgo, setDate30DaysAgo] = useState<Date>();

  useEffect(() => {
    setCurrDate(new Date());
    setDate30DaysAgo(new Date(new Date().setDate(new Date().getDay() - 30)));
  }, []);

  const [
    {
      fetching: thirtyDayStatsFetching,
      data: thirtyDayStats,
      error: thirtyDayStatsError
    }
  ] = useQuery({
    query: GetStatsRange,
    variables: {
      stateDate: currDate,
      endDate: date30DaysAgo
    }
  });

  // Line Chart Data
  const [lineChartArrState, setLineChartArrState] = useState<LineChartArr>(
    [] as LineChartArr
  );

  useEffect(() => {
    if (!thirtyDayStatsFetching && thirtyDayStats && !thirtyDayStatsError) {
      setLineChartArrState(lineChartArr(thirtyDayStats.getStatsRange));
    }
  }, [thirtyDayStats, thirtyDayStatsError, thirtyDayStatsFetching]);

  return (
    <Fragment>
      <VStack
        bg="cyan.950"
        w="100vw"
        minH="100vh"
        h="100%"
        py="5vh"
        minW="fit-content"
      >
        <VStack w={{ base: "100%", md: "90%" }} gap={10} minW="fit-content">
          <VStack w="80%" gap={10}>
            <Icon h="7.5rem" color="whiteAlpha">
              <svg
                id="svg2"
                version="1.1"
                width="409.96661"
                height="409.98404"
                viewBox="0 0 409.96661 409.98404"
                xmlns="http://www.w3.org/2000/svg"
              >
                <g fill="currentColor">
                  <path
                    d="m 198.43447,409.88349 c -16.87402,-0.52429 -33.96148,-3.20112 -50.15064,-7.85635 -16.89988,-4.85959 -33.4194,-12.04085 -48.45,-21.06187 C 89.057412,374.49751 79.188152,367.3017 69.836023,359.09346 63.443873,353.48316 56.486693,346.52267 50.841584,340.09001 23.882655,309.37001 6.733496,271.34323 1.6401223,230.99002 -3.0267695,194.01569 2.3071672,156.75191 17.114155,122.88582 c 2.83776,-6.49044 6.63732,-14.00102 10.21694,-20.1958 C 42.991144,75.589277 64.616853,52.405411 90.533832,34.933461 108.33243,22.934523 127.85299,13.821763 148.38383,7.9274153 c 26.15516,-7.50907673 53.79077,-9.7183097 81.05,-6.4792629 50.21751,5.9670387 96.51852,30.4672236 130.00448,68.7918786 26.75943,30.626139 43.81786,68.571209 48.88922,108.749989 4.86563,38.54882 -1.15232,77.44493 -17.37657,112.31081 -8.98481,19.30835 -20.76424,36.8569 -35.46819,52.83918 -2.44277,2.65514 -8.7281,8.94409 -11.35114,11.35768 -14.70283,13.5288 -30.43046,24.37645 -48.03196,33.12859 -4.05332,2.01546 -5.5776,2.72668 -9.56585,4.46335 -27.56473,12.00295 -57.65906,17.73965 -88.09935,16.79386 z m 13.44936,-34.9928 c 25.01212,-1.07439 48.36413,-7.17806 70.34385,-18.38623 19.2632,-9.82294 36.50092,-23.26822 50.87783,-39.6843 7.86352,-8.97886 15.0508,-19.24676 20.93747,-29.91169 1.47754,-2.67687 4.04205,-7.75113 5.39976,-10.6842 8.41325,-18.17525 13.5806,-38.08598 15.08733,-58.13425 0.35814,-4.76539 0.40375,-6.24526 0.40375,-13.1 0,-7.80507 -0.13553,-10.87819 -0.75413,-17.1 -3.3773,-33.96857 -16.9036,-65.97946 -39.00164,-92.299992 -4.83753,-5.76189 -10.83367,-11.955826 -16.62603,-17.174494 -16.89903,-15.22529 -37.39974,-27.226947 -58.91819,-34.492262 -15.49468,-5.231496 -31.10132,-8.118781 -47.75,-8.833924 -2.89874,-0.124515 -10.90126,-0.124515 -13.8,0 -16.64507,0.714988 -32.25869,3.603565 -47.75,8.833924 -22.42114,7.57009 -43.37752,20.065402 -60.949998,36.341643 -2.01749,1.868667 -6.68465,6.525563 -8.64412,8.625113 -18.522729,19.846862 -31.943938,43.538062 -39.299728,69.372092 -3.13635,11.01509 -5.12484,22.15786 -6.00108,33.6279 -0.35666,4.6688 -0.40507,6.2343 -0.40507,13.1 0,6.8657 0.04841,8.4312 0.40507,13.1 1.22839,16.0797 4.67357,31.67931 10.32477,46.75 5.59407,14.91838 13.591089,29.46979 23.231919,42.27293 12.532319,16.64308 28.020458,30.89004 45.549847,41.8996 16.73723,10.51204 34.82788,17.91208 54.08839,22.12504 9.16278,2.00422 19.45487,3.32848 29.1,3.74422 2.65778,0.11456 11.5597,0.12015 14.15,0.009 z m -37.2,-46.83768 v -30.8126 l 9.28842,9.28802 9.28842,9.28802 76.72486,-76.72488 76.72486,-76.72488 12.28672,12.28673 12.28673,12.28673 -76.64997,76.64993 c -42.15747,42.15746 -76.64998,76.71734 -76.65,76.79973 -2e-5,0.0824 4.12343,4.27345 9.16323,9.31347 l 9.16327,9.16368 -30.81327,-0.001 -30.81327,-10e-4 v -30.81259 z m -61.7,-31.04633 c -0.0664,-0.0221 -0.1,-10.37788 -0.1,-30.79993 v -30.7666 l 9.2544,9.2544 9.2544,9.2544 76.72511,-76.72511 76.7251,-76.72511 12.29318,12.29349 12.29318,12.29349 -76.64567,76.64613 c -42.15513,42.15537 -76.66344,76.6994 -76.68514,76.7645 -0.0217,0.0651 4.0948,4.25282 9.14779,9.30603 5.05298,5.0532 9.16483,9.1914 9.13745,9.19599 -0.1628,0.0273 -61.31804,0.0356 -61.3998,0.008 z M 51.026084,235.09894 c -0.02324,-0.0232 -0.04225,-13.88347 -0.04225,-30.80052 v -30.75827 l 9.266339,9.26634 9.26633,9.26634 76.638487,-76.63848 c 42.15116,-42.151155 76.68658,-76.668199 76.74537,-76.704532 0.06,-0.03707 5.49452,5.321436 12.38275,12.209497 l 12.27587,12.275559 -76.68758,76.687446 c -42.17816,42.1781 -76.687569,76.72116 -76.687569,76.76237 0,0.0412 4.140059,4.21517 9.200129,9.27546 l 9.20013,9.20054 H 81.826212 c -16.916829,0 -30.776888,-0.0188 -30.800128,-0.042 z"
                    id="path3338"
                  />
                </g>
              </svg>
            </Icon>
            <VStack gap={4} maxW="30rem">
              <Heading as="h1" fontSize="5xl">
                {"Nazi Site Patrol Stats"}
              </Heading>
              <Heading
                as="h2"
                fontSize="xl"
                color="whiteAlpha.800"
                textAlign="center"
              >
                {
                  "A telegram bot that removes links and embeds to sites that align with the Fascist Right political agenda."
                }
              </Heading>
            </VStack>
            <VStack gap={0} w="min-content">
              <Link target="_blank" href={`https://t.me/NoTwitter_Bot`}>
                <Button variant="solid" bgColor={"teal.solid"} fontSize="xl">
                  {"Add the bot to your telegram group"}
                </Button>
              </Link>
              <Text textAlign="center" fontSize="md" color="whiteAlpha.800">
                {
                  "The bot requires no configuration. All it needs is admin privileges to delete messages in your group."
                }
              </Text>
            </VStack>
          </VStack>
          <VStack gap={10} w="100%">
            {totalGroups ? (
              <StatsList
                title="Total Stats"
                loading={totalStatsFetching || totalGroupsFetching}
                error={totalStatsError || totalGroupsError}
                groups={totalGroups.getTotalGroups}
                links={totalStats.getTotalStats.linksDeleted}
                commands={totalStats.getTotalStats.commandResponses}
                triggers={totalStats.getTotalStats.timesTriggered}
              />
            ) : (
              <StatsList
                title="Total Stats"
                loading={totalStatsFetching || totalGroupsFetching}
                error={totalStatsError || totalGroupsError}
                groups={0}
                links={0}
                commands={0}
                triggers={0}
              />
            )}
            {todayStats ? (
              <StatsList
                title="Today's Stats"
                loading={todaysStatsFetching}
                error={todaysStatsError}
                links={todayStats.getTodayStats.linksDeleted}
                commands={todayStats.getTodayStats.commandResponses}
                triggers={todayStats.getTodayStats.timesTriggered}
              />
            ) : (
              <StatsList
                title="Today's Stats"
                loading={todaysStatsFetching}
                error={todaysStatsError}
                links={0}
                commands={0}
                triggers={0}
              />
            )}
          </VStack>
          <VStack w="95%" gap="5vh">
            <VStack gap={1}>
              <Heading as="h1" fontSize="3xl">{`30 Day Stats`}</Heading>
              <Text textAlign="center" fontSize="sm" color="whiteAlpha.800">
                {"Updates every 24 hours"}
              </Text>
            </VStack>
            <Skeleton w="100%" h="auto" loading={thirtyDayStatsFetching}>
              <LinksDeletedChart lineChartData={lineChartArrState} />
            </Skeleton>
            <Skeleton w="100%" h="auto" loading={thirtyDayStatsFetching}>
              <CommandResponsesChart lineChartData={lineChartArrState} />
            </Skeleton>
            <Skeleton w="100%" h="auto" loading={thirtyDayStatsFetching}>
              <TimedTriggeredChart lineChartData={lineChartArrState} />
            </Skeleton>
          </VStack>
        </VStack>
      </VStack>
      <VStack
        w="100vw"
        py="5vh"
        textAlign="center"
        color="white"
        textShadow="0px 0px 5px black;"
        gap={6}
      >
        <Text fontSize="3xl" w="auto" bg="blackAlpha.400" maxW="62vw">
          {"Down with fascism! Fuck MAGA! Fuck Trump! Fuck Nazis!"}
        </Text>
        <Text fontSize="3xl" w="auto" bg="blackAlpha.400" maxW="62vw">
          {
            "Trans rights are human rights! Trans women are women! Trans men are men! Never let them take away your happiness!"
          }
        </Text>
      </VStack>
      <Box w="100vw" h="100vh" position="fixed" bottom="-510px" zIndex={-1}>
        <Image
          fill
          alt="intersex inclusive pride flag"
          src="https://upload.wikimedia.org/wikipedia/commons/6/60/Intersex-inclusive_pride_flag.svg"
        />
      </Box>
    </Fragment>
  );
}
