<script lang="ts">
    import {
        aniListLoggedIn,
        aniListPrimary,
        aniListUser,
        aniListWatchlist,
        animePerPage,
        malLoggedIn,
        malPrimary,
        malUser,
        malWatchList,
        simklLoggedIn,
        simklUser,
        simklWatchList,
        watchListPage,
        simklPrimary,
        aniListAnime,
        GetAniListSingleItem,
    } from "./helperComponents/GlobalVariablesAndHelperFunctions.svelte";
    import {
        CheckIfAniListLoggedIn,
        CheckIfMyAnimeListLoggedIn,
        CheckIfSimklLoggedIn,
        GetAniListLoggedInUser,
        GetAniListUserWatchingList,
        GetMyAnimeList,
        GetMyAnimeListLoggedInUser,
        GetSimklLoggedInUser,
        SimklGetUserWatchlist,
    } from "../wailsjs/go/main/App";
    import {MediaListSort} from "./anilist/types/AniListTypes";
    import {onMount} from "svelte";
    import Router from "svelte-spa-router"
    import Home from "./routes/Home.svelte";
    import {wrap} from "svelte-spa-router/wrap";
    import Spinner from "./helperComponents/Spinner.svelte";
    import Header from "./helperComponents/Header.svelte";


    let isAniListPrimary: boolean
    let isMalPrimary: boolean
    let isSimklPrimary: boolean

    aniListPrimary.subscribe((value) => isAniListPrimary = value)
    malPrimary.subscribe((value) => isMalPrimary = value)
    simklPrimary.subscribe(value => isSimklPrimary = value)


    let page: number
    let perPage: number
    watchListPage.subscribe(value => page = value)
    animePerPage.subscribe(value => perPage = value)

    onMount(async () => {
        await CheckIfAniListLoggedIn().then(loggedIn => {
            if (loggedIn) {
                GetAniListLoggedInUser().then(user => {
                    aniListUser.set(user)
                    if (isAniListPrimary) {
                        GetAniListUserWatchingList(page, perPage, MediaListSort.UpdatedTimeDesc).then((watchList) => {
                            aniListWatchlist.set(watchList)
                            aniListLoggedIn.set(loggedIn)
                        })
                    } else {
                        aniListLoggedIn.set(loggedIn)
                    }
                })
            }
        })

        await CheckIfMyAnimeListLoggedIn().then(loggedIn => {
            if (loggedIn) {
                GetMyAnimeListLoggedInUser().then(user => {
                    malUser.set(user)
                    if (isMalPrimary) {
                        GetMyAnimeList(1000).then(watchList => {
                            malWatchList.set(watchList)
                            malLoggedIn.set(loggedIn)
                        })
                    } else {
                        malLoggedIn.set(loggedIn)
                    }
                })
            }
        })

        await CheckIfSimklLoggedIn().then(loggedIn => {
            if (loggedIn) {
                GetSimklLoggedInUser().then(user => {
                    if (Object.keys(user).length === 0) {
                        simklLoggedIn.set(false)
                    } else {
                        simklUser.set(user)
                        if(isSimklPrimary) {
                            SimklGetUserWatchlist().then(result => {
                                simklWatchList.set(result)
                                simklLoggedIn.set(loggedIn)
                            })
                        } else {
                            simklLoggedIn.set(loggedIn)
                        }
                    }
                })
            }
        })
    })
</script>

<Header />
<Router routes={{
    '/': Home,
    '/anime/:id': wrap({
        asyncComponent: () => import('./routes/Anime.svelte'),
        conditions: [
            () => $aniListLoggedIn,
            async (detail) => {
                await GetAniListSingleItem(Number(detail.params.id), true)
                return Object.keys($aniListAnime).length!==0
            },
        ],
        loadingComponent: Spinner
    }),
    // '*': "Not Found"
}} />
