2017-11-09 14:36:53 +00:00
|
|
|
|
using SpotifyAPI.Local.Enums;
|
|
|
|
|
using System;
|
2016-03-20 04:02:15 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace SpotifyAPI.Local.Models
|
|
|
|
|
{
|
|
|
|
|
public class SpotifyUri
|
|
|
|
|
{
|
2018-03-11 23:13:38 +00:00
|
|
|
|
private readonly Dictionary<UriType, string> _properties = new Dictionary<UriType, string>();
|
2017-11-09 14:36:53 +00:00
|
|
|
|
|
2018-03-11 23:13:38 +00:00
|
|
|
|
public string Base { get; }
|
2017-11-09 14:36:53 +00:00
|
|
|
|
public UriType Type => _properties?.LastOrDefault().Key ?? UriType.none;
|
|
|
|
|
public string Id => _properties?.LastOrDefault().Value;
|
|
|
|
|
|
|
|
|
|
public SpotifyUri(string uriBase, Dictionary<UriType, string> properties)
|
|
|
|
|
{
|
|
|
|
|
Base = uriBase;
|
|
|
|
|
_properties = properties;
|
|
|
|
|
}
|
2016-03-20 04:02:15 +00:00
|
|
|
|
|
2017-11-09 14:36:53 +00:00
|
|
|
|
public SpotifyUri(string uriBase, UriType uriType, string uriId)
|
2016-03-21 14:48:50 +00:00
|
|
|
|
{
|
|
|
|
|
Base = uriBase;
|
2017-11-09 14:36:53 +00:00
|
|
|
|
_properties.Add(uriType, uriId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string GetUriPropValue(UriType type)
|
|
|
|
|
{
|
2018-03-11 23:13:38 +00:00
|
|
|
|
return !_properties.ContainsKey(type) ? null : _properties[type];
|
2016-03-21 14:48:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static SpotifyUri Parse(string uri)
|
2016-03-20 04:02:15 +00:00
|
|
|
|
{
|
2016-03-31 11:08:23 +01:00
|
|
|
|
if (string.IsNullOrEmpty(uri))
|
2016-03-21 11:56:06 +00:00
|
|
|
|
throw new ArgumentNullException("Uri");
|
|
|
|
|
|
|
|
|
|
string[] props = uri.Split(':');
|
2018-03-11 23:13:38 +00:00
|
|
|
|
if (props.Length < 3 || !Enum.TryParse(props[1], out UriType uriType))
|
2016-03-21 11:56:06 +00:00
|
|
|
|
throw new ArgumentException("Unexpected Uri");
|
|
|
|
|
|
2017-11-09 14:36:53 +00:00
|
|
|
|
Dictionary<UriType, string> properties = new Dictionary<UriType, string> { { uriType, props[2] } };
|
|
|
|
|
|
|
|
|
|
for (int index = 3; index < props.Length; index += 2)
|
|
|
|
|
{
|
2018-03-11 23:13:38 +00:00
|
|
|
|
if (Enum.TryParse(props[index], out UriType type))
|
2017-11-09 14:36:53 +00:00
|
|
|
|
properties.Add(type, props[index + 1]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new SpotifyUri(props[0], properties);
|
2016-03-21 14:48:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string ToString()
|
|
|
|
|
{
|
2018-03-11 23:13:38 +00:00
|
|
|
|
return $"{Base}:{string.Join(":", _properties.SelectMany(x => new[] { x.Key.ToString(), x.Value }))}";
|
2016-03-20 04:02:15 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|