<script lang="ts">
    import {
        anilistModal,
        aniListLoggedIn,
        simklLoggedIn,
        malLoggedIn,
        malAnime,
        simklAnime,
        aniListAnime,
    } from "./GlobalVariablesAndHelperFunctions.svelte";
    import {Button} from "flowbite-svelte";
    // @ts-ignore
    import StarRatting from "@ernane/svelte-star-rating"
    import moment from 'moment'
    import { Table, TableBody, TableBodyCell, TableBodyRow, TableHead, TableHeadCell } from 'flowbite-svelte';
    import { writable } from 'svelte/store';
    import type {SimklAnime} from "./simkl/types/simklTypes";
    import {
        AniListUpdateEntry, MyAnimeListUpdate,
        SimklSyncEpisodes,
        SimklSyncRating,
        SimklSyncStatus
    } from "../wailsjs/go/main/App";
    import type {MALAnime, MALUploadStatus, MyListStatus} from "./mal/types/MALTypes";
    import type {AniListUpdateVariables} from "./anilist/types/AniListTypes";
    import type {AniListGetSingleAnime} from "./anilist/types/AniListCurrentUserWatchListType";

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

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

    type statusOption = {
        id: number,
        aniList: string,
        mal: string,
        simkl: string,
    }

    const statusOptions: statusOption[] = [
        { 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

    startingAnilistStatusOption = statusOptions.filter(option => currentAniListAnime.data.MediaList.status === option.aniList)[0]

    type TableItems = {
        id: number
        service: string
        progress: number
        status: string
        startedAt: string
        completedAt: string
        score: number
        repeat: number
        notes: string
    }[]

    let items = writable([] as TableItems);

    let tableItems: TableItems
    items.subscribe(value => tableItems = value)

    if(isAniListLoggedIn) {
        items.update(item => {
            item.push({
                id: currentAniListAnime.data.MediaList.id,
                service: "AniList",
                progress: currentAniListAnime.data.MediaList.progress,
                status: currentAniListAnime.data.MediaList.status,
                startedAt: `${currentAniListAnime.data.MediaList.startedAt.month}-${currentAniListAnime.data.MediaList.startedAt.day}-${currentAniListAnime.data.MediaList.startedAt.year}`,
                completedAt: `${currentAniListAnime.data.MediaList.completedAt.month}-${currentAniListAnime.data.MediaList.completedAt.day}-${currentAniListAnime.data.MediaList.completedAt.year}`,
                score: currentAniListAnime.data.MediaList.score,
                repeat: currentAniListAnime.data.MediaList.repeat,
                notes: currentAniListAnime.data.MediaList.notes
            })
            return item
        })
    }

    if(isMalLoggedIn) {
        items.update(item => {
            item.push({
                id: currentMalAnime.id,
                service: "MyAnimeList",
                progress: currentMalAnime.my_list_status.num_episodes_watched,
                status: currentMalAnime.my_list_status.status,
                startedAt: currentMalAnime.my_list_status.start_date,
                completedAt: currentMalAnime.my_list_status.finish_date,
                score: currentMalAnime.my_list_status.score,
                repeat: currentMalAnime.my_list_status.num_times_rewatched,
                notes: currentMalAnime.my_list_status.comments
            })
            return item
        })
    }

    if(isSimklLoggedIn && Object.keys(currentSimklAnime).length > 0) {
        items.update(item => {
            item.push({
                id: currentSimklAnime.show.ids.simkl,
                service: "Simkl",
                progress: currentSimklAnime.watched_episodes_count,
                status: currentSimklAnime.status,
                startedAt: "",
                completedAt: "",
                score: currentSimklAnime.user_rating,
                repeat: 0,
                notes: ""
            })
            return item
        })
    }

    const sortKey = writable('service'); // default sort key
    const sortDirection = writable(1); // default sort direction (ascending)
    const sortItems = writable(tableItems.slice()); // make a copy of the items array
    console.log(tableItems[0])

    // Define a function to sort the items
    const sortTable = (key: any) => {
        // If the same key is clicked, reverse the sort direction
        if ($sortKey === key) {
            sortDirection.update((val) => -val);
        } else {
            sortKey.set(key);
            sortDirection.set(1);
        }
    };

    $: {
        const key = $sortKey;
        const direction = $sortDirection;
        const sorted = [...$sortItems].sort((a, b) => {
            const aVal = a[key];
            const bVal = b[key];
            if (aVal < bVal) {
                return -direction;
            } else if (aVal > bVal) {
                return direction;
            }
            return 0;
        });
        sortItems.set(sorted);
    }

    const ratingInWords = {
        0: "Not Reviewed",
        1: "Appalling",
        2: "Horrible",
        3: "Very Bad",
        4: "Bad",
        5: "Average",
        6: "Fine",
        7: "Good",
        8: "Very Good",
        9: "Great",
        10: "Masterpiece",
    }

    const hide = (e) => {
        e.preventDefault();
        anilistModal.set(false)
    };

    const title = currentAniListAnime.data.MediaList.media.title.english !== "" ?
        currentAniListAnime.data.MediaList.media.title.english :
        currentAniListAnime.data.MediaList.media.title.romaji

    let config = {
        readOnly: false,
        countStars: 5,
        range: {
            min: 0,
            max: 5,
            step: 0.5
        },
        score: currentAniListAnime.data.MediaList.score / 2,
        showScore: false,
        scoreFormat: function () {
            return `(${this.score.toFixed(1)}/${this.countStars})`
        },
        name: "",
        starConfig: {
            size: 32,
            fillColor: '#F9ED4F',
            strokeColor: "#e2c714",
            unfilledColor: '#FFF',
            strokeUnfilledColor: '#000'
        }
    }

    let values = {
        progress: currentAniListAnime.data.MediaList.progress,
        status: startingAnilistStatusOption,
        startedAt: {
            year: currentAniListAnime.data.MediaList.startedAt.year,
            month: currentAniListAnime.data.MediaList.startedAt.month,
            day: currentAniListAnime.data.MediaList.startedAt.day
        },
        completedAt: {
            year: currentAniListAnime.data.MediaList.completedAt.year,
            month: currentAniListAnime.data.MediaList.completedAt.month,
            day: currentAniListAnime.data.MediaList.completedAt.day
        },
        repeat: currentAniListAnime.data.MediaList.repeat,
        score: currentAniListAnime.data.MediaList.score,
        notes: currentAniListAnime.data.MediaList.notes
    }
    let startedAtDate: string
    let completedAtDate: string

    const changeRating = (e) => {
        config.score = e.target.valueAsNumber
        values.score = e.target.valueAsNumber * 2
    }


    if (values.startedAt.year > 0) {
        let startedAtISODate = new Date(values.startedAt.year, values.startedAt.month - 1, values.startedAt.day)
        let startedAtMoment = moment(startedAtISODate)
        startedAtDate = startedAtMoment.format('YYYY-MM-DD')
    }

    const transformStartedAtDate = (e) => {
        const re = /^([0-9]{4})-([0-9]{2})-([0-9]{2})/
        const date = re.exec(e.target.value)
        values.startedAt.year = Number(date[1])
        values.startedAt.month = Number(date[2])
        values.startedAt.day = Number(date[3])
    }

    if (values.completedAt.year > 0) {
        let completedAtISODate = new Date(values.completedAt.year, values.completedAt.month - 1, values.completedAt.day)
        let completedAtMoment = moment(completedAtISODate)
        completedAtDate = completedAtMoment.format('YYYY-MM-DD')
    }

    const transformCompletedAtDate = (e) => {
        const re = /^([0-9]{4})-([0-9]{2})-([0-9]{2})/
        const date = re.exec(e.target.value)
        values.completedAt.year = Number(date[1])
        values.completedAt.month = Number(date[2])
        values.completedAt.day = Number(date[3])
    }

    const submitData = async () => {
        submitting.set(true)
        let body: AniListUpdateVariables = {
            mediaId: currentAniListAnime.data.MediaList.mediaId,
            progress: values.progress,
            status: values.status.aniList,
            score: values.score,
            repeat: values.repeat,
            notes: values.notes,
            startedAt: {
                year: values.startedAt.year,
                month: values.startedAt.month,
                day: values.startedAt.day
            },
            completedAt: {
                year: values.completedAt.year,
                month: values.completedAt.month,
                day: values.completedAt.day
            }
        }
       await AniListUpdateEntry(body).then((value) => {
           aniListAnime.update(anime => {
               anime.data.MediaList.status = value.data.SaveMediaListEntry.status
               anime.data.MediaList.progress = value.data.SaveMediaListEntry.progress
               anime.data.MediaList.repeat = value.data.SaveMediaListEntry.repeat
               anime.data.MediaList.score = value.data.SaveMediaListEntry.score
               anime.data.MediaList.startedAt.day = value.data.SaveMediaListEntry.startedAt.day
               anime.data.MediaList.startedAt.month = value.data.SaveMediaListEntry.startedAt.month
               anime.data.MediaList.startedAt.year = value.data.SaveMediaListEntry.startedAt.year
               if (value.data.SaveMediaListEntry.notes === null) {
                   anime.data.MediaList.notes = ""
               } else {
                anime.data.MediaList.notes = value.data.SaveMediaListEntry.notes
               }
               if (value.data.SaveMediaListEntry.completedAt.year === null) {
                   anime.data.MediaList.completedAt.year = 0
               } else {
                   anime.data.MediaList.completedAt.year = value.data.SaveMediaListEntry.completedAt.year
               }
               if (value.data.SaveMediaListEntry.completedAt.month === null) {
                   anime.data.MediaList.completedAt.month = 0
               } else {
                   anime.data.MediaList.completedAt.month = value.data.SaveMediaListEntry.completedAt.month
               }
               if (value.data.SaveMediaListEntry.completedAt.day === null) {
                   anime.data.MediaList.completedAt.day = 0
               } else {
                   anime.data.MediaList.completedAt.day = value.data.SaveMediaListEntry.completedAt.day
               }

               for (let [index, item] of tableItems.entries()) {
                   if (item.service === "AniList") {
                       items.update(value => {
                           value[index].id = anime.data.MediaList.id
                           value[index].progress = anime.data.MediaList.progress
                           value[index].status = anime.data.MediaList.status
                           value[index].startedAt = `${anime.data.MediaList.startedAt.month}-${anime.data.MediaList.startedAt.day}-${anime.data.MediaList.startedAt.year}`
                           value[index].completedAt = `${anime.data.MediaList.completedAt.month}-${anime.data.MediaList.completedAt.day}-${anime.data.MediaList.completedAt.year}`
                           value[index].score = anime.data.MediaList.score
                           value[index].repeat = anime.data.MediaList.repeat
                           value[index].notes = anime.data.MediaList.notes
                           return value
                       })
                   }
               }
               return anime
           })
       })

        if(malLoggedIn) {
            let body: MALUploadStatus = {
                status: values.status.mal,
                is_rewatching: false,
                score: values.score,
                num_watched_episodes: values.progress,
                num_times_rewatched: values.repeat,
                comments: values.notes
            }

            await MyAnimeListUpdate(currentMalAnime, body).then((malAnimeReturn: MyListStatus) => {
                currentMalAnime.my_list_status.status = malAnimeReturn.status
                currentMalAnime.my_list_status.is_rewatching = malAnimeReturn.is_rewatching
                currentMalAnime.my_list_status.score = malAnimeReturn.score
                currentMalAnime.my_list_status.num_episodes_watched = malAnimeReturn.num_episodes_watched
                currentMalAnime.my_list_status.num_times_rewatched = malAnimeReturn.num_times_rewatched
                currentMalAnime.my_list_status.comments = malAnimeReturn.comments
                for (let [index, item] of tableItems.entries()) {
                    if (item.service === "MyAnimeList") {
                        items.update(value => {
                            value[index].id = currentMalAnime.id
                            value[index].progress = malAnimeReturn.num_episodes_watched
                            value[index].status = malAnimeReturn.status
                            value[index].startedAt = malAnimeReturn.start_date
                            value[index].completedAt = malAnimeReturn.finish_date
                            value[index].score = malAnimeReturn.score
                            value[index].repeat = malAnimeReturn.num_times_rewatched
                            value[index].notes = malAnimeReturn.comments
                            return value
                        })
                    }
                }
            })
        }

        if (simklLoggedIn) {
            if (currentSimklAnime.watched_episodes_count !== values.progress) {
                await SimklSyncEpisodes(currentSimklAnime, values.progress).then((simklAnimeReturn) => {
                    simklAnime.set(simklAnimeReturn)
                    for (let [index, item] of tableItems.entries()) {
                        if (item.service === "Simkl") {
                            items.update(value => {
                                value[index].progress = simklAnimeReturn.watched_episodes_count
                                return value
                            })
                        }
                    }
                })
            }

            if (currentSimklAnime.user_rating !== values.score) {
                await SimklSyncRating(currentSimklAnime, values.score).then((simklAnimeReturn) => {
                    simklAnime.set(simklAnimeReturn)
                    for (let [index, item] of tableItems.entries()) {
                        if (item.service === "Simkl") {
                            items.update(value => {
                                value[index].score = simklAnimeReturn.user_rating
                                return value
                            })
                        }
                    }
                })
            }

            if (currentSimklAnime.status !== values.status.simkl) {
                await SimklSyncStatus(currentSimklAnime, values.status.simkl).then((simklAnimeReturn) => {
                    simklAnime.set(simklAnimeReturn)
                    for (let [index, item] of tableItems.entries()) {
                        if (item.service === "Simkl") {
                            items.update(value => {
                                value[index].status = simklAnimeReturn.status
                                return value
                            })
                        }
                    }
                })
            }
        }
        submitting.set(false)
        isSubmitSuccess = true
        setTimeout(() => isSubmitSuccess = false, 2000)
    }
</script>

<div id="inapp-data">
    <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">
            <StarRatting bind:config on:change={changeRating}/>
            <p>Rating:  {config.score * 2}</p>
            <p>{ratingInWords[config.score * 2]}</p>
        </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-gray-900 dark:text-white">Episode
                        Progress</label>
                    <input
                            type="number"
                            name="episodes"
                            min="0"
                            id="episodes"
                            class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-primary-600
                            focus:border-primary-600 block w-24 p-2.5 dark:bg-gray-600 dark:border-gray-500 dark:placeholder-gray-400
                            dark:text-white dark:focus:ring-primary-500 dark:focus:border-primary-500"
                            bind:value={values.progress}
                            required>
                </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"
                            class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500
                            focus:border-blue-500 block p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400
                            dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500"
                            bind:value={values.status}
                    >
                        {#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-gray-900 dark:text-white">Date
                        Started</label>
                    <div class="relative max-w-sm">
                        <div class="absolute inset-y-0 start-0 flex items-center ps-3.5 pointer-events-none">
                            <svg class="w-4 h-4 text-gray-500 dark:text-gray-400" aria-hidden="true"
                                 xmlns="http://www.w3.org/2000/svg" fill="currentColor" viewBox="0 0 20 20">
                                <path d="M20 4a2 2 0 0 0-2-2h-2V1a1 1 0 0 0-2 0v1h-3V1a1 1 0 0 0-2 0v1H6V1a1 1 0 0 0-2 0v1H2a2 2 0 0 0-2 2v2h20V4ZM0 18a2 2 0 0 0 2 2h16a2 2 0 0 0 2-2V8H0v10Zm5-8h10a1 1 0 0 1 0 2H5a1 1 0 0 1 0-2Z"/>
                            </svg>
                        </div>
                        <input
                                id="startedAt"
                                type="date"
                                name="startedAt"
                                class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500
                            focus:border-blue-500 block w-full ps-10 p-2.5 dark:bg-gray-700 dark:border-gray-600
                            dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500"
                                value={startedAtDate}
                                placeholder="Date Started"
                                on:change={transformStartedAtDate}
                        >
                    </div>
                </div>
                <div>

                    <label for="completedAt" class="text-left block mb-2 text-sm font-medium text-gray-900 dark:text-white">Date
                        Completed</label>
                    <div class="relative max-w-sm">
                        <div class="absolute inset-y-0 start-0 flex items-center ps-3.5 pointer-events-none">
                            <svg class="w-4 h-4 text-gray-500 dark:text-gray-400" aria-hidden="true"
                                 xmlns="http://www.w3.org/2000/svg" fill="currentColor" viewBox="0 0 20 20">
                                <path d="M20 4a2 2 0 0 0-2-2h-2V1a1 1 0 0 0-2 0v1h-3V1a1 1 0 0 0-2 0v1H6V1a1 1 0 0 0-2 0v1H2a2 2 0 0 0-2 2v2h20V4ZM0 18a2 2 0 0 0 2 2h16a2 2 0 0 0 2-2V8H0v10Zm5-8h10a1 1 0 0 1 0 2H5a1 1 0 0 1 0-2Z"/>
                            </svg>
                        </div>
                        <input
                                id="completedAt"
                                type="date"
                                name="completedAt"
                                class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500
                            focus:border-blue-500 block w-full ps-10 p-2.5 dark:bg-gray-700 dark:border-gray-600
                            dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500"
                                value={completedAtDate}
                                placeholder="Date Completed"
                                on:change={transformCompletedAtDate}
                        >
                    </div>
                </div>
                <div>
                    <label for="repeat"
                           class="text-left block mb-2 text-sm font-medium text-gray-900 dark:text-white">Rewatched</label>
                    <input
                            type="number"
                            name="repeat"
                            min="0"
                            id="repeat"
                            class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-primary-600
                            focus:border-primary-600 block w-24 p-2.5 dark:bg-gray-600 dark:border-gray-500 dark:placeholder-gray-400
                            dark:text-white dark:focus:ring-primary-500 dark:focus:border-primary-500"
                            bind:value={values.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-gray-900 dark:text-white">Your
                        notes</label>
                    <textarea id="notes" rows="3"
                              class="block p-2.5 w-full text-sm text-gray-900 bg-gray-50 rounded-lg border border-gray-300 focus:ring-blue-500 focus:border-blue-500 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500"
                              placeholder="Write your thoughts here..."
                              bind:value={values.notes}></textarea>
                </div>
            </div>
        </div>
    </div>
    <div id="external-data">
        <div id="anilist-data" 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 group">
            <h2 class="text-left mb-1 text-base font-semibold text-gray-900 dark:text-white">AniList</h2>
        </div>
    </div>

    <Table hoverable={true}>
        <TableHead>
            <TableHeadCell class="cursor-pointer" on:click={() => sortTable('id')}>ID</TableHeadCell>
            <TableHeadCell class="cursor-pointer" on:click={() => sortTable('service')}>Service</TableHeadCell>
            <TableHeadCell class="cursor-pointer" on:click={() => sortTable('progress')}>Episode Progress</TableHeadCell>
            <TableHeadCell class="cursor-pointer" on:click={() => sortTable('status')}>Status</TableHeadCell>
            <TableHeadCell class="cursor-pointer" on:click={() => sortTable('startedAt')}>Date Started</TableHeadCell>
            <TableHeadCell class="cursor-pointer" on:click={() => sortTable('completedAt')}>Date Completed</TableHeadCell>
            <TableHeadCell class="cursor-pointer" on:click={() => sortTable('score')}>Rating</TableHeadCell>
            <TableHeadCell class="cursor-pointer" on:click={() => sortTable('repeat')}>Rewatches</TableHeadCell>
            <TableHeadCell>Notes</TableHeadCell>
        </TableHead>
        <TableBody tableBodyClass="divide-y">
            {#each $sortItems as item}
                <TableBodyRow>
                    <TableBodyCell class="overflow-x-auto">{item.id}</TableBodyCell>
                    <TableBodyCell class="overflow-x-auto">{item.service}</TableBodyCell>
                    <TableBodyCell class="overflow-x-auto" >{item.progress}</TableBodyCell>
                    <TableBodyCell class="overflow-x-auto">{item.status}</TableBodyCell>
                    <TableBodyCell class="overflow-x-auto">{item.startedAt}</TableBodyCell>
                    <TableBodyCell class="overflow-x-auto">{item.completedAt}</TableBodyCell>
                    <TableBodyCell class="overflow-x-auto">{item.score}</TableBodyCell>
                    <TableBodyCell class="overflow-x-auto">{item.repeat}</TableBodyCell>
                    <TableBodyCell class="overflow-x-auto">{item.notes}</TableBodyCell>
                </TableBodyRow>
            {/each}
        </TableBody>
    </Table>


    <div class="bg-white rounded-lg shadow max-w-4-4 dark: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 bg-blue-700 {isSubmitSuccess ?
                    'dark:bg-green-600 hover:bg-green-800 dark:hover:bg-green-700 focus:ring-4 focus:ring-green-300 dark:focus:ring-green-800' :
                    'dark:bg-blue-600 hover:bg-blue-800 dark:hover:bg-blue-700 focus:ring-4 focus:ring-blue-300 dark:focus:ring-blue-800'
                    } font-medium
                    rounded-lg text-sm px-5 py-2.5 me-2 mb-2 focus:outline-none"
                    on:click={submitData}>
                <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-gray-900 bg-white border border-gray-300 focus:outline-none hover:bg-gray-100 focus:ring-4
                    focus:ring-gray-100 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={hide}>
                Cancel
            </Button>
        </div>
    </div>

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