import {State} from '@hookstate/core'
import {Filters, GameList} from '../models/game'
import agent from '../api/agent'


const getWithFilters = async (
    gamesState: State<GameList>,
    loadedState: State<boolean>,
    admin: boolean,
    page: number,
    limit: number,
    search: string,
    filters: Filters,
    ratingState: string,
    steamRatingState: string,
) => {
    loadedState.set(false)
    const filterString = 'page=' + page.toString() +
        '&limit=' + limit.toString() +
        '&search=' + encodeURIComponent(search) +
        '&series=' + encodeURIComponent(filters.series) +
        '&playStatus=' + encodeURIComponent(filters.playStatus) +
        '&genre=' + encodeURIComponent(filters.genre) +
        '&os=' + encodeURIComponent(filters.os) +
        '&store=' + encodeURIComponent(filters.store) +
        '&developer=' + encodeURIComponent(filters.developer) +
        '&publisher=' + encodeURIComponent(filters.publisher) +
        '&controller=' + encodeURIComponent(filters.controller) +
        '&soundtrack=' + encodeURIComponent(filters.soundtrack) +
        '&intel=' + encodeURIComponent(filters.intel) +
        '&wine=' + encodeURIComponent(filters.wine) +
        '&rating' + (filters.rating !== '' ? ratingState + '=' + encodeURIComponent(filters.rating.toString()) : '=') +
        '&steamRating' + (filters.steamRating !== '' ? steamRatingState + '=' + encodeURIComponent(filters.steamRating.toString()) : '=')
    const getGames = admin ? await agent.GamesAdmin.list(filterString) : await agent.Games.list(filterString)
    gamesState.set(getGames)
    loadedState.set(true)
}

export default getWithFilters