<script lang="ts">
  import {
    aniListAnime,
    aniListLoggedIn,
    malAnime,
    malLoggedIn,
    simklAnime,
    simklLoggedIn,
  } from "../helperModules/GlobalVariablesAndHelperFunctions.svelte";
  import { push } from "svelte-spa-router";
  import { Button } from "flowbite-svelte";
  import type { AniListGetSingleAnime } from "../anilist/types/AniListCurrentUserWatchListType";
  import Rating from "./Rating.svelte";
  import {
    convertAniListDateToString,
    convertAniListDateToDate,
  } from "../helperFunctions/convertAniListDateIn";
  import AnimeTable from "./AnimeTable.svelte";
  import type {
    MALAnime,
    MalListStatus,
    MALUploadStatus,
  } from "../mal/types/MALTypes";
  import type { SimklAnime } from "../simkl/types/simklTypes";
  import { writable } from "svelte/store";
  import type { StatusOption, StatusOptions } from "../helperTypes/StatusTypes";
  import type { AniListUpdateVariables } from "../anilist/types/AniListTypes";
  import {
    convertDateStringToAniList,
    convertDateToAniList,
  } from "../helperFunctions/convertDateToAniList";
  import {
    AniListDeleteEntry,
    AniListUpdateEntry,
    DeleteMyAnimeListEntry,
    MyAnimeListUpdate,
    SimklSyncEpisodes,
    SimklSyncRating,
    SimklSyncRemove,
    SimklSyncStatus,
  } from "../../wailsjs/go/main/App";
  import { AddAnimeServiceToTable } from "../helperModules/AddAnimeServiceToTable.svelte";
  import { CheckIfAniListLoggedInAndLoadWatchList } from "../helperModules/CheckIfAniListLoggedInAndLoadWatchList.svelte";
  import Datepicker from "./Datepicker.svelte";
  const re = /^([0-9]{4})-([0-9]{2})-([0-9]{2})/;

  let isAniListLoggedIn: boolean;
  let isMalLoggedIn: boolean;
  let isSimklLoggedIn: boolean;
  let currentAniListAnime: AniListGetSingleAnime;
  let currentMalAnime: MALAnime;
  let currentSimklAnime: SimklAnime;
  let submitting = writable(false);
  let isSubmitting: boolean;
  let submitSuccess = writable(false);

  aniListLoggedIn.subscribe((value) => (isAniListLoggedIn = value));
  malLoggedIn.subscribe((value) => (isMalLoggedIn = value));
  simklLoggedIn.subscribe((value) => (isSimklLoggedIn = value));
  aniListAnime.subscribe((value) => (currentAniListAnime = value));
  malAnime.subscribe((value) => (currentMalAnime = value));
  simklAnime.subscribe((value) => (currentSimklAnime = value));
  submitting.subscribe((value) => (isSubmitting = value));

  const title =
    currentAniListAnime.data.MediaList.media.title.english !== ""
      ? currentAniListAnime.data.MediaList.media.title.english
      : currentAniListAnime.data.MediaList.media.title.romaji;
  const statusOptions: StatusOptions = [
    { id: 0, aniList: "CURRENT", mal: "watching", simkl: "watching" },
    {
      id: 1,
      aniList: "PLANNING",
      mal: "plan_to_watch",
      simkl: "plantowatch",
    },
    { id: 2, aniList: "COMPLETED", mal: "completed", simkl: "completed" },
    { id: 3, aniList: "DROPPED", mal: "dropped", simkl: "dropped" },
    { id: 4, aniList: "PAUSED", mal: "on_hold", simkl: "hold" },
    { id: 5, aniList: "REPEATING", mal: "rewatching", simkl: "watching" },
  ];
  let startingAnilistStatusOption: StatusOption = statusOptions.filter(
    (option) => currentAniListAnime.data.MediaList.status === option.aniList,
  )[0];
  let startedAtDate: Date | null = convertAniListDateToDate(
    currentAniListAnime.data.MediaList.startedAt,
  );
  let completedAtDate: Date | null = convertAniListDateToDate(
    currentAniListAnime.data.MediaList.completedAt,
  );

  if (isAniListLoggedIn)
    AddAnimeServiceToTable({
      id: `a-${currentAniListAnime.data.MediaList.mediaId}`,
      title,
      service: "AniList",
      progress: currentAniListAnime.data.MediaList.progress,
      status: currentAniListAnime.data.MediaList.status,
      startedAt: convertAniListDateToString(
        currentAniListAnime.data.MediaList.startedAt,
      ),
      completedAt: convertAniListDateToString(
        currentAniListAnime.data.MediaList.completedAt,
      ),
      score: currentAniListAnime.data.MediaList.score,
      repeat: currentAniListAnime.data.MediaList.repeat,
      notes: currentAniListAnime.data.MediaList.notes,
    });

  if (isMalLoggedIn) {
    let startDate = "";
    let finishDate = "";
    if (currentMalAnime.my_list_status.start_date !== "") {
      const startArray = re.exec(currentMalAnime.my_list_status.start_date);
      startDate = `${startArray[2]}-${startArray[3]}-${startArray[1]}`;
    }
    if (currentMalAnime.my_list_status.finish_date !== "") {
      const finishArray = re.exec(currentMalAnime.my_list_status.finish_date);
      finishDate = `${finishArray[2]}-${finishArray[3]}-${finishArray[1]}`;
    }
    AddAnimeServiceToTable({
      id: `m-${currentMalAnime.id}`,
      title: currentMalAnime.title,
      service: "MyAnimeList",
      progress: currentMalAnime.my_list_status.num_episodes_watched,
      status: currentMalAnime.my_list_status.status,
      startedAt: startDate,
      completedAt: finishDate,
      score: currentMalAnime.my_list_status.score,
      repeat: currentMalAnime.my_list_status.num_times_rewatched,
      notes: currentMalAnime.my_list_status.comments,
    });
  }

  if (isSimklLoggedIn && Object.keys(currentSimklAnime).length > 0)
    AddAnimeServiceToTable({
      id: `s-${currentSimklAnime.show.ids.simkl}`,
      title: currentSimklAnime.show.title,
      service: "Simkl",
      progress: currentSimklAnime.watched_episodes_count,
      status: currentSimklAnime.status,
      startedAt: "",
      completedAt: "",
      score: currentSimklAnime.user_rating,
      repeat: 0,
      notes: "",
    });

  const handleSubmit = async (e: any) => {
    submitting.set(true);
    let submitData: {
      rating: number;
      episodes: number;
      status: StatusOption;
      startedAt: Date | null;
      completedAt: Date | null;
      repeat: number;
      notes: string;
    } = {
      rating: 0,
      episodes: 0,
      status: {
        id: 0,
        aniList: "",
        mal: "",
        simkl: "",
      },
      startedAt: null,
      completedAt: null,
      repeat: 0,
      notes: "",
    };
    const formData = new FormData(e.target);
    for (let field of formData) {
      const [key, value] = field;
      if (key === "rating") {
        submitData.rating = Number(value) * 2;
        continue;
      }
      if (key === "episodes") {
        submitData.episodes = Number(value);
        continue;
      }
      if (key === "repeat") {
        submitData.repeat = Number(value);
        continue;
      }
      if (key === "status") {
        submitData.status = startingAnilistStatusOption;
        continue;
      }
      submitData[key] = value;
    }

    if (isAniListLoggedIn && currentAniListAnime.data.MediaList.mediaId !== 0) {
      let body: AniListUpdateVariables = {
        mediaId: currentAniListAnime.data.MediaList.mediaId,
        progress: submitData.episodes,
        status: submitData.status.aniList,
        score: submitData.rating,
        repeat: submitData.repeat,
        notes: submitData.notes,
        startedAt: convertDateToAniList(startedAtDate),
        completedAt: convertDateToAniList(completedAtDate),
      };
      await AniListUpdateEntry(body).then((value: AniListGetSingleAnime) => {
        /* TODO in future when you inevitably add tags to typescript, until Anilist fixes the api bug
                    where tags break the SaveMediaListEntry return, you'll want to use this delete line
                    delete value.data.MediaList.media.tags */
        aniListAnime.update((newValue) => {
          newValue = value;
          return newValue;
        });
        AddAnimeServiceToTable({
          id: `a-${currentAniListAnime.data.MediaList.mediaId}`,
          title,
          service: "AniList",
          progress: currentAniListAnime.data.MediaList.progress,
          status: currentAniListAnime.data.MediaList.status,
          startedAt: convertAniListDateToString(
            currentAniListAnime.data.MediaList.startedAt,
          ),
          completedAt: convertAniListDateToString(
            currentAniListAnime.data.MediaList.completedAt,
          ),
          score: currentAniListAnime.data.MediaList.score,
          repeat: currentAniListAnime.data.MediaList.repeat,
          notes: currentAniListAnime.data.MediaList.notes,
        });
      });
    }

    if (malLoggedIn && currentMalAnime.id !== 0) {
      let body: MALUploadStatus = {
        status: submitData.status.mal,
        is_rewatching: submitData.repeat > 0,
        score: submitData.rating,
        num_watched_episodes: submitData.episodes,
        num_times_rewatched: submitData.repeat,
        comments: submitData.notes,
      };

      await MyAnimeListUpdate(currentMalAnime, body).then(
        (malAnimeReturn: MalListStatus) => {
          malAnime.update((value) => {
            value.my_list_status.status = malAnimeReturn.status;
            value.my_list_status.is_rewatching = malAnimeReturn.is_rewatching;
            value.my_list_status.score = malAnimeReturn.score;
            value.my_list_status.num_episodes_watched =
              malAnimeReturn.num_episodes_watched;
            value.my_list_status.num_times_rewatched =
              malAnimeReturn.num_times_rewatched;
            value.my_list_status.comments = malAnimeReturn.comments;
            return value;
          });
          let startDate = "";
          let finishDate = "";
          if (currentMalAnime.my_list_status.start_date !== "") {
            const startArray = re.exec(
              currentMalAnime.my_list_status.start_date,
            );
            startDate = `${startArray[2]}-${startArray[3]}-${startArray[1]}`;
          }
          if (currentMalAnime.my_list_status.finish_date !== "") {
            const finishArray = re.exec(
              currentMalAnime.my_list_status.finish_date,
            );
            finishDate = `${finishArray[2]}-${finishArray[3]}-${finishArray[1]}`;
          }
          AddAnimeServiceToTable({
            id: `m-${currentMalAnime.id}`,
            title: currentMalAnime.title,
            service: "MyAnimeList",
            progress: currentMalAnime.my_list_status.num_episodes_watched,
            status: currentMalAnime.my_list_status.status,
            startedAt: startDate,
            completedAt: finishDate,
            score: currentMalAnime.my_list_status.score,
            repeat: currentMalAnime.my_list_status.num_times_rewatched,
            notes: currentMalAnime.my_list_status.comments,
          });
        },
      );
    }

    if (simklLoggedIn && currentSimklAnime.show.ids.simkl !== 0) {
      if (currentSimklAnime.watched_episodes_count !== submitData.episodes) {
        await SimklSyncEpisodes(currentSimklAnime, submitData.episodes).then(
          (value: SimklAnime) => {
            AddAnimeServiceToTable({
              id: `s-${value.show.ids.simkl}`,
              title: value.show.title,
              service: "Simkl",
              progress: value.watched_episodes_count,
              status: value.status,
              startedAt: "",
              completedAt: "",
              score: value.user_rating,
              repeat: 0,
              notes: "",
            });
            simklAnime.update((newValue) => {
              newValue = value;
              return newValue;
            });
          },
        );
      }

      if (currentSimklAnime.user_rating !== submitData.rating) {
        await SimklSyncRating(currentSimklAnime, submitData.rating).then(
          (value) => {
            AddAnimeServiceToTable({
              id: `s-${value.show.ids.simkl}`,
              title: value.show.title,
              service: "Simkl",
              progress: value.watched_episodes_count,
              status: value.status,
              startedAt: "",
              completedAt: "",
              score: value.user_rating,
              repeat: 0,
              notes: "",
            });
            simklAnime.update((newValue) => {
              newValue = value;
              return newValue;
            });
          },
        );
      }

      if (currentSimklAnime.status !== submitData.status.simkl) {
        await SimklSyncStatus(currentSimklAnime, submitData.status.simkl).then(
          (value) => {
            AddAnimeServiceToTable({
              id: `s-${value.show.ids.simkl}`,
              title: value.show.title,
              service: "Simkl",
              progress: value.watched_episodes_count,
              status: value.status,
              startedAt: "",
              completedAt: "",
              score: value.user_rating,
              repeat: 0,
              notes: "",
            });
            simklAnime.update((newValue) => {
              newValue = value;
              return newValue;
            });
          },
        );
      }
    }

    submitting.set(false);
    submitSuccess.set(true);
    setTimeout(() => submitSuccess.set(false), 2000);
  };

  const deleteEntries = async () => {
    submitting.set(true);
    if (isAniListLoggedIn && currentAniListAnime.data.MediaList.mediaId !== 0) {
      await AniListDeleteEntry(currentAniListAnime.data.MediaList.id);
      AddAnimeServiceToTable({
        id: `a-${currentAniListAnime.data.MediaList.mediaId}`,
        title,
        service: "AniList",
        progress: 0,
        status: "",
        startedAt: "",
        completedAt: "",
        score: 0,
        repeat: 0,
        notes: "",
      });
    }
    if (malLoggedIn && currentMalAnime.id !== 0) {
      await DeleteMyAnimeListEntry(currentMalAnime.id);
      AddAnimeServiceToTable({
        id: `m-${currentMalAnime.id}`,
        title: currentMalAnime.title,
        service: "MyAnimeList",
        progress: 0,
        status: "",
        startedAt: "",
        completedAt: "",
        score: 0,
        repeat: 0,
        notes: "",
      });
    }
    if (simklLoggedIn && currentSimklAnime.show.ids.simkl !== 0) {
      await SimklSyncRemove(currentSimklAnime);
      AddAnimeServiceToTable({
        id: `s-${currentSimklAnime.show.ids.simkl}`,
        title: currentSimklAnime.show.title,
        service: "Simkl",
        progress: 0,
        status: "",
        startedAt: "",
        completedAt: "",
        score: 0,
        repeat: 0,
        notes: "",
      });
    }
    submitting.set(false);
    submitSuccess.set(true);
    setTimeout(() => submitSuccess.set(false), 2000);
  };

  let max = 999;

  if (currentAniListAnime.data.MediaList.media.episodes !== 0) {
    max = currentAniListAnime.data.MediaList.media.episodes;
  }
  if (
    currentAniListAnime.data.MediaList.media.episodes === 0 &&
    currentAniListAnime.data.MediaList.media.nextAiringEpisode.episode !== 0
  ) {
    max =
      currentAniListAnime.data.MediaList.media.nextAiringEpisode.episode - 1;
  }
</script>

<form on:submit|preventDefault={handleSubmit} class="container pt-3 pb-10">
  <h1 class="text-white font-bold text-left text-xl pb-3">
    {title}
  </h1>
  <div class="grid grid-cols-1 md:grid-cols-10 grid-flow-col gap-4">
    <div class="md:col-span-2 space-y-3">
      <img
        class="rounded-lg"
        src={currentAniListAnime.data.MediaList.media.coverImage.large}
        alt="{title} Cover Image"
      />
      <Rating bind:score={currentAniListAnime.data.MediaList.score} />
    </div>

    <div class="md:col-span-8">
      <div
        class="flex flex-col md:flex-row md:pl-10 md:pr-10 pt-5 pb-5 justify-center md:gap-x-24 lg:gap-x-56"
      >
        <div>
          <label
            for="episodes"
            class="text-left block mb-2 text-sm font-medium text-white"
            >Episode Progress</label
          >
          <div class="relative flex items-center max-w-[8rem]">
            <button
              type="button"
              id="decrement-button"
              data-input-counter-decrement="quantity-input"
              on:click={() =>
                (currentAniListAnime.data.MediaList.progress -= 1)}
              class="bg-gray-100 dark:bg-gray-700 dark:hover:bg-gray-600 dark:border-gray-600 hover:bg-gray-200 border border-gray-300 rounded-s-lg p-3 h-11 focus:ring-gray-100 dark:focus:ring-gray-700 focus:ring-2 focus:outline-none"
            >
              <svg
                class="w-3 h-3 text-gray-900 dark:text-white"
                aria-hidden="true"
                xmlns="http://www.w3.org/2000/svg"
                fill="none"
                viewBox="0 0 18 2"
              >
                <path
                  stroke="currentColor"
                  stroke-linecap="round"
                  stroke-linejoin="round"
                  stroke-width="2"
                  d="M1 1h16"
                />
              </svg>
            </button>
            <input
              type="number"
              name="episodes"
              min="0"
              {max}
              id="episodes"
              class="border border-x-0 p-2.5 h-11 text-center text-sm block w-full placeholder-gray-400 [appearance:textfield] [&::-webkit-outer-spin-button]:appearance-none [&::-webkit-inner-spin-button]:appearance-none
                            {currentAniListAnime.data.MediaList.progress < 0 ||
              (currentAniListAnime.data.MediaList.media.episodes > 0 &&
                currentAniListAnime.data.MediaList.progress >
                  currentAniListAnime.data.MediaList.media.episodes) ||
              (currentAniListAnime.data.MediaList.media.nextAiringEpisode
                .episode > 0 &&
                currentAniListAnime.data.MediaList.progress >
                  currentAniListAnime.data.MediaList.media.nextAiringEpisode
                    .episode -
                    1)
                ? 'border-red-500 border-[2px] text-rose-300 focus:ring-red-500 focus:border-red-500'
                : 'bg-gray-700 hover:bg-gray-600 border-gray-600 text-white focus:ring-blue-500 focus:border-blue-500'} w-24"
              bind:value={currentAniListAnime.data.MediaList.progress}
              required
            />
            <button
              type="button"
              id="increment-button"
              data-input-counter-increment="quantity-input"
              on:click={() =>
                (currentAniListAnime.data.MediaList.progress += 1)}
              class="bg-gray-100 dark:bg-gray-700 dark:hover:bg-gray-600 dark:border-gray-600 hover:bg-gray-200 border border-gray-300 rounded-e-lg p-3 h-11 focus:ring-gray-100 dark:focus:ring-gray-700 focus:ring-2 focus:outline-none"
            >
              <svg
                class="w-3 h-3 text-gray-900 dark:text-white"
                aria-hidden="true"
                xmlns="http://www.w3.org/2000/svg"
                fill="none"
                viewBox="0 0 18 18"
              >
                <path
                  stroke="currentColor"
                  stroke-linecap="round"
                  stroke-linejoin="round"
                  stroke-width="2"
                  d="M9 1v16M1 9h16"
                />
              </svg>
            </button>
          </div>
          <div>
            / {currentAniListAnime.data.MediaList.media.nextAiringEpisode
              .episode !== 0
              ? currentAniListAnime.data.MediaList.media.nextAiringEpisode
                  .episode - 1
              : currentAniListAnime.data.MediaList.media.episodes}
          </div>
          {#if currentAniListAnime.data.MediaList.media.nextAiringEpisode.episode !== 0}
            <div>
              of {currentAniListAnime.data.MediaList.media.episodes}
            </div>
          {/if}
        </div>

        <div>
          <label
            for="status"
            class="text-left block mb-2 text-sm font-medium text-gray-900 dark:text-white"
            >Status</label
          >
          <select
            id="status"
            name="status"
            class="border text-sm rounded-lg
                            block p-2.5 bg-gray-700 border-gray-600 placeholder-gray-400
                            text-white focus:ring-blue-500 focus:border-blue-500"
            bind:value={startingAnilistStatusOption}
          >
            {#each statusOptions as option}
              <option value={option}>{option.aniList}</option>
            {/each}
          </select>
        </div>
      </div>
      <div
        class="flex flex-col md:flex-row md:pl-10 md:pr-10 pt-5 pb-5 justify-center md:gap-x-16 lg:gap-x-36"
      >
        <div>
          <label
            for="startedAt"
            class="text-left block mb-2 text-sm font-medium text-white"
            >Date Started</label
          >
          <Datepicker
            bind:value={startedAtDate}
            color="slate"
            dateFormat={{
              year: "numeric",
              month: "2-digit",
              day: "2-digit",
            }}
            showActionButtons
          />
        </div>
        <div>
          <label
            for="completedAt"
            class="text-left block mb-2 text-sm font-medium text-white"
            >Date Completed</label
          >
          <Datepicker
            bind:value={completedAtDate}
            color="slate"
            dateFormat={{
              year: "numeric",
              month: "2-digit",
              day: "2-digit",
            }}
            showActionButtons
          />
        </div>
        <div>
          <label
            for="repeat"
            class="text-left block mb-2 text-sm font-medium text-white"
            >Rewatched</label
          >
          <input
            type="number"
            name="repeat"
            min="0"
            id="repeat"
            class="border {currentAniListAnime.data.MediaList.repeat < 0
              ? 'border-red-500 border-[2px] text-rose-300 focus:ring-red-500 focus:border-red-500'
              : 'border-gray-500 text-white focus:ring-blue-500 focus:border-blue-500'} text-sm rounded-lg block w-24 p-2.5 bg-gray-600 placeholder-gray-400 text-white"
            bind:value={currentAniListAnime.data.MediaList.repeat}
            required
          />
        </div>
      </div>
      <div
        class="flex flex-col md:flex-row md:pl-10 md:pr-10 pt-5 pb-5 justify-center"
      >
        <div class="w-full">
          <label
            for="notes"
            class="text-left block mb-2 text-sm font-medium text-white"
            >Your notes</label
          >
          <textarea
            id="notes"
            rows="3"
            name="notes"
            class="block p-2.5 w-full text-sm rounded-lg border bg-gray-700 border-gray-600 placeholder-gray-400 text-white focus:ring-blue-500 focus:border-blue-500"
            placeholder="Write your thoughts here..."
            bind:value={currentAniListAnime.data.MediaList.notes}
          ></textarea>
        </div>
      </div>
    </div>
  </div>
  <div class="flex mb-4 rounded-lg shadow max-w-4-4 bg-gray-800">
    <div
      class="w-full mx-auto max-w-screen-xl p-4 md:flex md:items-center md:justify-end"
    >
      <Button
        disabled={isSubmitting}
        id="sync-button"
        class="text-white {$submitSuccess
          ? 'bg-green-600 dark:bg-green-600 hover:bg-green-700 dark:hover:bg-green-700 focus:ring-4 focus:ring-green-800 dark:focus:ring-green-800'
          : 'bg-blue-600 dark:bg-blue-600 hover:bg-blue-700 dark:hover:bg-blue-700 focus:ring-4 focus:ring-blue-800 dark:focus:ring-blue-800'} font-medium rounded-lg text-sm px-5 py-2.5 me-2 mb-2 focus:outline-none"
        type="submit"
      >
        <svg
          id="submit-loader"
          aria-hidden="true"
          role="status"
          class="{isSubmitting
            ? 'inline'
            : 'hidden'} w-4 h-4 me-3 text-white animate-spin"
          viewBox="0 0 100 101"
          fill="none"
          xmlns="http://www.w3.org/2000/svg"
        >
          <path
            d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z"
            fill="#E5E7EB"
          />
          <path
            d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z"
            fill="currentColor"
          />
        </svg>
        Sync Changes
      </Button>
      <Button
        class="text-white bg-gray-800 border border-gray-600 focus:outline-none hover:bg-gray-700 focus:ring-4
                    focus:ring-gray-700 font-medium rounded-lg text-sm px-5 py-2.5 me-2 mb-2 dark:bg-gray-800 dark:text-white
                    dark:border-gray-600 dark:hover:bg-gray-700 dark:hover:border-gray-600 dark:focus:ring-gray-700"
        on:click={async () => {
          await CheckIfAniListLoggedInAndLoadWatchList();
          return push("/");
        }}
      >
        Go Home
      </Button>
    </div>
  </div>
  <AnimeTable />

  <div class="flex rounded-lg shadow max-w-4-4 bg-gray-800">
    <div
      class="w-full mx-auto max-w-screen-xl p-4 md:flex md:items-center md:justify-start"
    >
      <Button
        disabled={isSubmitting}
        id="delete-button"
        class="text-white bg-red-700 {$submitSuccess
          ? 'bg-green-600 dark:bg-green-600 hover:bg-green-700 dark:hover:bg-green-700 focus:ring-4 focus:ring-green-800 dark:focus:ring-green-800'
          : 'bg-red-600 dark:bg-red-600 hover:bg-red-700 dark:hover:bg-red-700 focus:ring-4 focus:ring-red-800 dark:focus:ring-red-800'} font-medium rounded-lg text-sm px-5 py-2.5 me-2 mb-2 focus:outline-none"
        on:click={deleteEntries}
      >
        <svg
          id="submit-loader"
          aria-hidden="true"
          role="status"
          class="{isSubmitting
            ? 'inline'
            : 'hidden'} w-4 h-4 me-3 text-white animate-spin"
          viewBox="0 0 100 101"
          fill="none"
          xmlns="http://www.w3.org/2000/svg"
        >
          <path
            d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z"
            fill="#E5E7EB"
          />
          <path
            d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z"
            fill="currentColor"
          />
        </svg>
        Delete Entries
      </Button>
    </div>
    <div
      class="w-full mx-auto max-w-screen-xl p-4 md:flex md:items-center md:justify-end"
    >
      <Button
        disabled={isSubmitting}
        id="sync-button"
        class="text-white {$submitSuccess
          ? 'bg-green-600 dark:bg-green-600 hover:bg-green-700 dark:hover:bg-green-700 focus:ring-4 focus:ring-green-800 dark:focus:ring-green-800'
          : 'bg-blue-600 dark:bg-blue-600 hover:bg-blue-700 dark:hover:bg-blue-700 focus:ring-4 focus:ring-blue-800 dark:focus:ring-blue-800'} font-medium rounded-lg text-sm px-5 py-2.5 me-2 mb-2 focus:outline-none"
        type="submit"
      >
        <svg
          id="submit-loader"
          aria-hidden="true"
          role="status"
          class="{isSubmitting
            ? 'inline'
            : 'hidden'} w-4 h-4 me-3 text-white animate-spin"
          viewBox="0 0 100 101"
          fill="none"
          xmlns="http://www.w3.org/2000/svg"
        >
          <path
            d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z"
            fill="#E5E7EB"
          />
          <path
            d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z"
            fill="currentColor"
          />
        </svg>
        Sync Changes
      </Button>
      <Button
        class="text-white bg-gray-800 border border-gray-600 focus:outline-none hover:bg-gray-700 focus:ring-4
                    focus:ring-gray-700 font-medium rounded-lg text-sm px-5 py-2.5 me-2 mb-2 dark:bg-gray-800 dark:text-white
                    dark:border-gray-600 dark:hover:bg-gray-700 dark:hover:border-gray-600 dark:focus:ring-gray-700"
        on:click={async () => {
          await CheckIfAniListLoggedInAndLoadWatchList();
          return push("/");
        }}
      >
        Go Home
      </Button>
    </div>
  </div>

  <div>
    <h3 class="text-2xl">Summary</h3>
    <p>{@html currentAniListAnime.data.MediaList.media.description}</p>
  </div>
</form>
