<script lang="ts">

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

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

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

</script>


<div>
    <div class="max-w-md mx-auto">
        <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" data-dropdown-toggle="aniListSearch"
                        data-dropdown-offset-distance="0"
                        data-dropdown-offset-skidding="-50"
                        data-dropdown-placement="bottom"
                        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={() => {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>

    <div id="aniListSearch" class="z-10 hidden bg-white rounded-lg shadow 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">
                        <button on:click={() => {
                            GetAniListSingleItemAndOpenModal(media.id)
                        }}
                                class="flex w-full items-start p-1 hover:bg-gray-100 dark:hover:bg-gray-600 dark:hover:text-white rounded-lg">
                            <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">
                            <p class="bg-gray-800 text-left w-full h-24 p-2 rounded-tr-lg rounded-br-lg">{media.title.english === '' || media.title.english === null ? media.title.romaji : media.title.english }</p>
                        </button>
                    </li>
                {/each}
            </ul>
        {:else if aniSearch.length === 0}
            <div class="m-4">Please enter a search term...</div>
        {/if}
        <a href="#"
           class="flex items-center p-3 text-sm font-medium text-blue-600 border-t border-gray-200 rounded-b-lg bg-gray-50 dark:border-gray-600 hover:bg-gray-100 dark:bg-gray-700 dark:hover:bg-gray-600 dark:text-blue-500 hover:underline">
            <svg class="w-4 h-4 me-2" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="currentColor"
                 viewBox="0 0 20 18">
                <path d="M6.5 9a4.5 4.5 0 1 0 0-9 4.5 4.5 0 0 0 0 9ZM8 10H5a5.006 5.006 0 0 0-5 5v2a1 1 0 0 0 1 1h11a1 1 0 0 0 1-1v-2a5.006 5.006 0 0 0-5-5Zm11-3h-2V5a1 1 0 0 0-2 0v2h-2a1 1 0 1 0 0 2h2v2a1 1 0 0 0 2 0V9h2a1 1 0 1 0 0-2Z"/>
            </svg>
            Add new user
        </a>
    </div>
</div>