import * as serverApi from '../../api/api';
import { QueryElem, toApiQuery } from '../query/Query';
import backendRequest from './request';

export async function queryItems(
    types: serverApi.ResourceType[],
    query: QueryElem | undefined,
    offset: number | undefined,
    limit: number | undefined,
    responseType: serverApi.QueryResponseType,
): Promise<serverApi.QueryResponse> {
    console.log("Types:", types);
    var q: serverApi.QueryRequest = {
        query: query ? toApiQuery(query) : {},
        offsetsLimits: {
            artistOffset: (types.includes(serverApi.ResourceType.Artist)) ? (offset || 0) : undefined,
            artistLimit: (types.includes(serverApi.ResourceType.Artist)) ? (limit || -1) : undefined,
            albumOffset: (types.includes(serverApi.ResourceType.Album)) ? (offset || 0) : undefined,
            albumLimit: (types.includes(serverApi.ResourceType.Album)) ? (limit || -1) : undefined,
            trackOffset: (types.includes(serverApi.ResourceType.Track)) ? (offset || 0) : undefined,
            trackLimit: (types.includes(serverApi.ResourceType.Track)) ? (limit || -1) : undefined,
            tagOffset: (types.includes(serverApi.ResourceType.Tag)) ? (offset || 0) : undefined,
            tagLimit: (types.includes(serverApi.ResourceType.Tag)) ? (limit || -1) : undefined,
        },
        ordering: {
            orderBy: {
                type: serverApi.OrderByType.Name,
            },
            ascending: true,
        },
        responseType: responseType,
    };

    console.log(q);

    const requestOpts = {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify(q),
    };

    return (async () => {
        const response = await backendRequest((process.env.REACT_APP_BACKEND || "") + serverApi.QueryEndpoint, requestOpts)
        let json: any = await response.json();
        return json;
    })();
}

export async function queryArtists(
    query: QueryElem | undefined,
    offset: number | undefined,
    limit: number | undefined,
    responseType: serverApi.QueryResponseType,
): Promise<serverApi.QueryResponseArtistDetails[] | number[] | number> {
    let r = await queryItems([serverApi.ResourceType.Artist], query, offset, limit, responseType);
    return r.artists;
}

export async function queryAlbums(
    query: QueryElem | undefined,
    offset: number | undefined,
    limit: number | undefined,
    responseType: serverApi.QueryResponseType,
): Promise<serverApi.QueryResponseAlbumDetails[] | number[] | number> {
    let r = await queryItems([serverApi.ResourceType.Album], query, offset, limit, responseType);
    return r.albums;
}

export async function queryTracks(
    query: QueryElem | undefined,
    offset: number | undefined,
    limit: number | undefined,
    responseType: serverApi.QueryResponseType,
): Promise<serverApi.QueryResponseTrackDetails[] | number[] | number> {
    let r = await queryItems([serverApi.ResourceType.Track], query, offset, limit, responseType);
    return r.tracks;
}

export async function queryTags(
    query: QueryElem | undefined,
    offset: number | undefined,
    limit: number | undefined,
    responseType: serverApi.QueryResponseType,
): Promise<serverApi.QueryResponseTagDetails[] | number[] | number> {
    let r = await queryItems([serverApi.ResourceType.Tag], query, offset, limit, responseType);
    return r.tags;
}