using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IDAL;
using Model;
using Helper;
using System.Text.RegularExpressions;
namespace DAL
{
    public class Xiami : 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=xiami&pages={1}&name={2}"
                , num, page, word));
            string[] infos = CommonHelper.GetMiddleTextMultiple(src, "{\"", "},");
            foreach (string i in infos)
            {
                Music s = new Music();
                s.Id = CommonHelper.GetMiddleText(i, "id\":", ",\"name");
                s.Name = CommonHelper.GetMiddleText(i, "name\":\"", "\",\"artist");
                s.Artist = CommonHelper.GetMiddleText(i, "artist\":[\"", "\"],\"album");
                s.Album = CommonHelper.GetMiddleText(i, "album\":\"", "\",\"pic_id");
                s.PicId = s.Id;
                s.Source = "Xiami";
                s.ToString();
                list.Add(s);
            }
            return list;
        }
        public List<Model.Music> SearchCollection(string id)
        {
            List<Music> list = new List<Music>();
            return list;
        }
        public bool GetData(Music m)
        {
            string Src = HttpHelper.Get(string.Format(
                "https://guozhivip.com/yinyue/api.php?callback=jQuery11130182920795747622_1585018563804&types=url&id={0}&source=xiami"
                , m.Id));
            string picsrc = HttpHelper.Get(string.Format(
                "https://guozhivip.com/yinyue/api.php?callback=jQuery11130182920795747622_1585018563804&types=pic&id={0}&source=xiami"
                , m.PicId));
            m.PicUrl = CommonHelper.GetMiddleText(picsrc, "url\":\"", "\"})").Replace("\\", "");
            GetLrc(m);
            //if (Lrc.Count < 2)
            //{ base.GetLrc(); }
            m.Url = CommonHelper.GetMiddleText(Src, "url\":\"", "\",\"size").Replace("\\", "");
            return HttpHelper.CheckUri(m.Url);
        }
        public void GetLrc(Music m)
        {
            string Lrcsrc = HttpHelper.Get(string.Format(
                "https://guozhivip.com/yinyue/api.php?callback=jQuery11130182920795747622_1585018563804&types=lyric&id={0}&source=xiami"
                , m.Id));
            m.Lrc = CommonHelper.GetMiddleText(Lrcsrc, "lyric\":\"", "\",\"tlyric").Trim().Replace("\\n", "\n").Replace("\\r", "");
        }
    }
}
