using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IDAL;
using Model;
using System.Text.RegularExpressions;
using Helper;
namespace DAL
{
    /// <summary>
    /// 网易云音乐music.163.com
    /// </summary>
    public class Netease : IMusic
    {
        public List<Model.Music> Search(string word, int page, int num)
        {
            List<Music> list = new List<Music>();
            string src = HttpHelper.Get(string.Format(
            "https://music.postgraduate.top/api.php?callback=jQuery11130182920795747622_1585018563804&types=search&count={0}&source=netease&pages={1}&name={2}"
            , num, page, word));
            Regex rg = new Regex("\"id\":(.*?),\"name\":\"(.*?)\",\"artist\":\\[\"(.*?)\"\\],\"album\":\"(.*?)\",\"pic_id\":\"(.*?)\",\"url_id\":.*?,\"lyric_id\":(.*?),\"source");
            MatchCollection col = rg.Matches(src);
            foreach (Match i in col)
            {
                Music s = new Music();
                s.Id = i.Groups[1].Value;
                s.Name = i.Groups[2].Value;
                s.Artist = i.Groups[3].Value;
                s.Album = i.Groups[4].Value;
                s.PicId = i.Groups[5].Value;
                s.Source = "Netease";
                s.ToString();
                list.Add(s);
            }
            return list;
        }
        public List<Model.Music> SearchCollection(string id)
        {
            List<Music> list = new List<Music>();
            string src = HttpHelper.Get(string.Format(
            "https://music.postgraduate.top/api.php?callback=jQuery11130915479096928357_1587695091669&types=playlist&id={0}&_=1587695091671"
            , id));
            string[] infos = CommonHelper.GetMiddleTextMultiple(src, "{\"n", "\"vd\"");
            foreach (string i in infos)
            {
                Music s = new Music();
                s.Id = CommonHelper.GetMiddleText(i, "id\":", ",\"pst");
                s.Artist = CommonHelper.GetMiddleText(i, "\"name\":\"", "\",\"tns");
                s.Name = CommonHelper.GetMiddleText(i, "ame\":\"", "\",\"id\":");
                s.Album = CommonHelper.GetMiddleText(i, "", "");
                s.PicId = CommonHelper.GetMiddleText(i, "picUrl\":\"", "\",\"tns").Replace("\\", "");
                s.Source = "Netease";
                list.Add(s);
            }
            return list;
        }
        public bool GetData(Model.Music m)
        {
            m.Source = "Netease";
            string Src = HttpHelper.Get(string.Format(
            "https://guozhivip.com/yinyue/api.php?callback=jQuery11130182920795747622_1585018563804&types=url&id={0}&source=netease"
            , m.Id));
            if (m.PicId.Contains("http")) { m.PicUrl = m.PicId; }
            else
            {
                string picsrc = HttpHelper.Get(string.Format(
                "https://guozhivip.com/yinyue/api.php?callback=jQuery11130182920795747622_1585018563804&types=pic&id={0}&source=netease"
                , m.PicId));
                m.PicUrl = CommonHelper.GetMiddleText(picsrc, "url\":\"", "\"})").Replace("\\", "");
            }
            GetLrc(m);
            m.Url = CommonHelper.GetMiddleText(Src, "url\":\"", "\",\"size").Replace("\\", "");
            return HttpHelper.CheckUri(m.Url);
        }
        public void GetLrc(Model.Music m)
        {
            m.Source = "Netease";
            string Lrcsrc = HttpHelper.Get(string.Format(
            "https://guozhivip.com/yinyue/api.php?callback=jQuery11130182920795747622_1585018563804&types=lyric&id={0}&source=netease"
            , m.Id));
            m.Lrc = CommonHelper.GetMiddleText(Lrcsrc, "lyric\":\"", "\",\"tlyric").Trim()
            .Replace("\\n", "\n").Replace("\\r", "");
        }
    }
}
