import { useMemo } from "react";
import {
  Accordion,
  AccordionContent,
  AccordionItem,
  AccordionTrigger,
} from "./ui/accordion";
import { Progress } from "./ui/progress";
import { usePinning, useUnpinMutation } from "~/hooks/usePinnning";
import { Tabs, TabsTrigger, TabsList, TabsContent } from "./ui/tabs";
import { Button } from "./ui/button";
import { Cross2Icon } from "@radix-ui/react-icons";
import { PinningStatus } from "~/data/pinning";

export const PinningNetworkBanner = () => {
  const { data} = usePinning();

  // TODO: Adapt to real API
  const itemsLeft = useMemo(
    () => data?.items.filter((item: PinningStatus) => item.status.includes("inprogress")) || [],
    [data],
  );

  const completedItems = useMemo(
    () => data?.items.filter((item: PinningStatus) => item.status.includes("completed")) || [],
    [data],
  );

  return (
    <div
      className={`border border-border rounded-lg absolute w-1/3 bottom-4 right-4 ${
        !data?.items.length ? "hidden" : "block"
      }`}>
      <Accordion type="single" defaultValue="item-1" collapsible>
        <AccordionItem value="item-1">
          <AccordionTrigger className="font-bold bg-primary px-4 rounded-tr-lg rounded-tl-lg">
            {`${completedItems.length}/${data?.items.length} items completed`}
          </AccordionTrigger>
          <AccordionContent>
            <Tabs className="w-full" defaultValue="inProgress">
              <TabsList className="rounded-none">
                <TabsTrigger value="inProgress">In Progress</TabsTrigger>
                <TabsTrigger value="completed">Completed</TabsTrigger>
              </TabsList>
              <TabsContent value="inProgress">
                {itemsLeft.length ? (
                  itemsLeft.map((item: PinningStatus) => (
                    <PinCidItem key={item.id} item={item} />
                  ))
                ) : (
                  <div className="text-primary-2 text-sm flex justify-center items-center h-10">
                    Nothing yet.
                  </div>
                )}
              </TabsContent>
              <TabsContent value="completed">
                {completedItems.length ? (
                  completedItems.map((item: PinningStatus) => (
                    <PinCidItem key={item.id} item={item} />
                  ))
                ) : (
                  <div className="text-muted text-sm flex justify-center items-center h-10">
                    Nothing yet.
                  </div>
                )}
              </TabsContent>
            </Tabs>
          </AccordionContent>
        </AccordionItem>
      </Accordion>
    </div>
  );
};

const PinCidItem = ({ item }: { item: PinningStatus }) => {
  const { mutate } = useUnpinMutation();

  return (
    <div className="px-4 mb-4">
      <div className="flex justify-between items-center rounded-lg h-10 py-2 px-4 hover:bg-primary/50 group">
        <span className="font-semibold">{item.id}</span>
        <span className="group-hover:hidden">{item.progress}%</span>
        <div className="gap-x-2 hidden group-hover:flex">
          <Button
            variant="ghost"
            className="p-2 rounded-full h-6"
            onClick={() => mutate({
              cid: item.id,
            })}>
            <Cross2Icon />
          </Button>
        </div>
      </div>
      <Progress
        value={item.progress}
        className="h-2 w-[calc(100%-1rem)] ml-2"
      />
    </div>
  );
};
