class TYTD.Music
{
    public getUserAgent() $"TYTDMusic/{this.getUserAgent.File.Version.Major}.{this.getUserAgent.File.Version.Minor}.{this.getUserAgent.File.Version.Patch} ( tesses@tesses.net )";
    public TYTD;
    public Music(tytd)
    {
        this.TYTD = tytd;
        TYTD.Storage.CreateDirectory(/"Album Arts");
    }

    public GetArtists(query)
    {

        var req = {
            RequestHeaders = [
                {
                    Key = "User-Agent",
                    Value = this.UserAgent
                }
            ]
        };
        var resp = Net.Http.MakeRequest($"https://musicbrainz.org/ws/2/artist?query={Net.Http.UrlEncode(query)}&fmt=json",req);
        
        if(resp.StatusCode >= 200 && resp.StatusCode <= 299)
        {
            var jo = Json.Decode(resp.ReadAsString());
            var items = [];
            each(var item : jo.artists)
            {
                items.Add({
                    id = item.id,
                    type = item.type,
                    name = item.name
                });
            }

            return items;
        }
        
        return null;
    }
    public GetArtistsAlbums(artist_id)
    {
        var req = {
            RequestHeaders = [
                {
                    Key = "User-Agent",
                    Value = this.UserAgent
                }
            ]
        };
        var resp = Net.Http.MakeRequest($"https://musicbrainz.org/ws/2/artist/{Net.Http.UrlPathEncode(artist_id)}?inc=artist-credits+releases&fmt=json",req);
        if(resp.StatusCode >= 200 && resp.StatusCode <= 299)
        {
            var jo = Json.Decode(resp.ReadAsString());
            var items = [];
            each(var item : jo.releases)
            {
                items.Add({
                    id = item.id,
                    packaging = item.packaging,
                    title = item.title,
                    date = item.date
                });
            }

            return {items,name=jo.name};
        }
        
        return null;
    }
    public GetAlbumArts(album_id)
    {
         var req = {
            RequestHeaders = [
                {
                    Key = "User-Agent",
                    Value = UserAgent
                }
            ],
            FollowRedirects=true
        };
        var url = $"https://coverartarchive.org/release/{Net.Http.UrlPathEncode(album_id)}";
        var resp = Net.Http.MakeRequest(url,req);
        if(resp.StatusCode >= 200 && resp.StatusCode <= 299)
        {
            return Json.Decode(resp.ReadAsString());
        }
        return null;
    }

    public GetAlbumArt(album_id, res)
    {
        var path = /"Album Arts"/$"{album_id}-{res}.jpg";
        if(TYTD.Storage.FileExists(path)) return FS.ReadAllBytes(TYTD.Storage,path);
        var arts = GetAlbumArts(album_id);
        if(arts == null) return null;
        
        each(var img : arts.images)
        {
            if(img.front)
            {
                var url = res == "full" ? img.image : img.thumbnails.[res];
                var req = {
                    RequestHeaders = [
                        {
                            Key = "User-Agent",
                            Value = UserAgent
                        }
                    ],
                    FollowRedirects=true
                };
                var resp = Net.Http.MakeRequest(url.Replace("http://","https://"),req);
                if(resp.StatusCode >= 200 && resp.StatusCode <= 299)
                {
                    var strm = TYTD.Storage.OpenFile(path,"wb");
                    resp.CopyToStream(strm);
                    strm.Close();
                    return FS.ReadAllBytes(TYTD.Storage,path);
                }
                
                
            }
        }
        return null;
    }
}