<script lang="ts">

    import {AniListSearch} from "../wailsjs/go/main/App";
    import type {AniSearchList} from "./anilist/types/AniListTypes";
    import {GetAniListSingleItemAndOpenModal} from "./GlobalVariablesAndHelperFunctions.svelte";

    let aniSearch = ""
    let aniListSearch: AniSearchList
    let aniListSearchActive = false

    function runAniListSearch(): void {
        AniListSearch(aniSearch).then(result => {
            aniListSearch = result
            aniListSearchActive = true
        })
    }

    function searchDropdown(): void {
        let dropdown = document.querySelector("#aniListSearchDropdown")
        dropdown.classList.toggle("hidden")
    }

</script>


<div id="searchDropdown" class="relative w-64 md:w-48">
        <div class="flex">
            <label for="anime-search" class="mb-2 text-sm font-medium text-gray-900 sr-only dark:text-white">Find
                Anime</label>
            <div class="relative w-full">
                <input type="search" id="anime-search" bind:value={aniSearch}
                       class="rounded-s-lg block p-2.5 w-full z-20 text-sm text-gray-900 bg-gray-50 rounded-e-lg border-s-gray-50 border-s-2 border border-gray-300 focus:ring-blue-500 focus:border-blue-500 dark:bg-gray-700 dark:border-s-gray-700  dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:border-blue-500"
                       placeholder="Search for Anime" required/>
                <button id="aniListSearchButton"
                        class="absolute top-0 end-0 h-full p-2.5 text-sm font-medium text-white bg-blue-700 rounded-e-lg border border-blue-700 hover:bg-blue-800 focus:ring-4 focus:outline-none focus:ring-blue-300 dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800"
                        on:click={() => {
                            searchDropdown()
                            if(aniSearch.length > 0) runAniListSearch()
                        }}>
                    <svg class="w-4 h-4" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="none"
                         viewBox="0 0 20 20">
                        <path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                              d="m19 19-4-4m0-7A7 7 0 1 1 1 8a7 7 0 0 1 14 0Z"/>
                    </svg>
                    <span class="sr-only">Search</span>
                </button>
            </div>
        </div>

    <div id="aniListSearchDropdown" class="z-10 absolute left-0 hidden bg-white rounded-lg shadow w-60 2xl:w-80 dark:bg-gray-700">
        {#if aniListSearchActive}
            <ul class="h-56 w-full py-2 overflow-y-auto text-gray-700 dark:text-gray-200"
                aria-labelledby="aniListSearchButton">
                {#each aniListSearch.data.Page.media as media}
                    <li class="w-full">
                        <div class="flex w-full items-start p-1 hover:bg-gray-100 dark:hover:bg-gray-600 dark:hover:text-white rounded-lg">
                            <button on:click={() => {
                                let spinner = document.querySelector("#spinner")
                                spinner.classList.toggle("hidden", false)
                                GetAniListSingleItemAndOpenModal(media.id, false).then(() => spinner.classList.toggle("hidden", true))
                            }}
                                    >
                                <img class="rounded-bl-lg rounded-tl-lg max-w-24 max-h-24" src={media.coverImage.large}
                                     alt="{media.title.english === '' || media.title.english === null ?  media.title.romaji : media.title.english} Cover">
                            </button>
                                <button class="rounded-bl-lg rounded-tl-lg w-full h-24" on:click={() => {
                                let spinner = document.querySelector("#spinner")
                                spinner.classList.toggle("hidden", false)
                                GetAniListSingleItemAndOpenModal(media.id, false).then(() => spinner.classList.toggle("hidden", true))
                            }} >{media.title.english === '' || media.title.english === null ? media.title.romaji : media.title.english }</button>
                        </div>
                    </li>
                {/each}
            </ul>
        {:else if aniSearch.length === 0}
            <div class="m-4">Please enter a search term...</div>
        {/if}
    </div>
</div>