import React from 'react';
import Integration, { IntegrationFeature, IntegrationAlbum, IntegrationArtist, IntegrationSong } from '../Integration';
import StoreLinkIcon, { ExternalStore } from '../../../components/common/StoreLinkIcon';

enum SearchType {
    Song = 'song',
    Artist = 'artist',
    Album = 'album',
};

export default class SpotifyClientCreds extends Integration {
    integrationId: number;

    constructor(integrationId: number) {
        super(integrationId);
        this.integrationId = integrationId;
    }

    static getFeatures(): IntegrationFeature[] {
        return [
            IntegrationFeature.Test,
            IntegrationFeature.SearchSong,
            IntegrationFeature.SearchAlbum,
            IntegrationFeature.SearchArtist,
        ]
    }

    static getIcon(props: any) {
        return <StoreLinkIcon whichStore={ExternalStore.Spotify} {...props} />
    }

    async test(testParams: {}) {
        const response = await fetch(
            (process.env.REACT_APP_BACKEND || "") +
            `/integrations/${this.integrationId}/v1/search?q=queens&type=artist`);

        if (!response.ok) {
            throw new Error("Spttify Client Credentails test failed: " + JSON.stringify(response));
        }
    }

    async searchSong(songProps: IntegrationSong): Promise<IntegrationSong[]> { return []; }
    async searchAlbum(albumProps: IntegrationAlbum): Promise<IntegrationAlbum[]> { return []; }
    async searchArtist(artistProps: IntegrationArtist): Promise<IntegrationArtist[]> { return []; }

    async search(query: string, type: SearchType):
        Promise<IntegrationSong[] | IntegrationAlbum[] | IntegrationArtist[]> {
            const response = await fetch(
                (process.env.REACT_APP_BACKEND || "") +
                `/integrations/${this.integrationId}/v1/search?q=${encodeURIComponent(query)}&type=${type}`);
    
            if (!response.ok) {
                throw new Error("Spotify Client Credentails search failed: " + JSON.stringify(response));
            }

            switch(type) {
                case SearchType.Song: {
                    return (await response.json()).tracks.items.map((r: any): IntegrationSong => {
                        return {
                            title: r.name,
                            url: r.external_urls.spotify,
                            artist: {
                                name: r.artists[0].name,
                                url: r.artists[0].external_urls.spotify,
                            },
                            album: {
                                name: r.albums[0].name,
                                url: r.albums[0].external_urls.spotify,
                            }
                        }
                    })
                }
                case SearchType.Artist: {
                    return (await response.json()).artists.items.map((r: any): IntegrationArtist => {
                        return {
                            name: r.name,
                            url: r.external_urls.spotify,
                        }
                    })
                }
                case SearchType.Album: {
                    return (await response.json()).albums.items.map((r: any): IntegrationAlbum => {
                        return {
                            name: r.name,
                            url: r.external_urls.spotify,
                            artist: {
                                name: r.artists[0].name,
                                url: r.artists[0].external_urls.spotify,
                            },
                        }
                    })
                }
            }
        }
}