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 QQ : 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://player.x1995.cn/api.php?callback=jQuery11130182920795747622_1585018563804&types=search&count={0}&source=tencent&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.Album = CommonHelper.GetMiddleText(i, "album\":\"", "\",\"pic_id");
                s.Artist = CommonHelper.GetMiddleText(i, "artist\":[\"", "\"],\"album");
                s.PicId = CommonHelper.GetMiddleText(i, "pic_id\":\"", "\",\"url_id");
                s.Source = "QQ";
                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(Model.Music m)
        {
            string Src = HttpHelper.Get(string.Format(
                "https://guozhivip.com/yinyue/api.php?callback=jQuery11130182920795747622_1585018563804&types=url&id={0}&source=tencent"
                , m.Id));
            string picsrc = HttpHelper.Get(string.Format(
                "https://guozhivip.com/yinyue/api.php?callback=jQuery11130182920795747622_1585018563804&types=pic&id={0}&source=tencent"
                , m.PicId));
            m.PicUrl = CommonHelper.GetMiddleText(picsrc, "url\":\"", "\"})").Replace("\\", "");
            GetLrc(m);
            m.Url = CommonHelper.GetMiddleText(Src, "url\":\"", "\",\"size").Replace("\\", "");
            if (!HttpHelper.CheckUri(m.Url))
            {
                string src = HttpHelper.Get(
                    "https://api88.net/api/qqmusic/?key=6a3228c7c98daaaf&cache=1&type=song&id="
                    + m.Id);
                m.Url = HttpHelper.ConvereUrl(CommonHelper.GetMiddleText(src, "url\":\"", "\",\"pic"));
            }
            return HttpHelper.CheckUri(m.Url);
        }
        public void GetLrc(Model.Music m)
        {
            string Lrcsrc = HttpHelper.Get(string.Format(
                "https://guozhivip.com/yinyue/api.php?callback=jQuery11130182920795747622_1585018563804&types=lyric&id={0}&source=tencent"
                , m.Id));
            m.Lrc = CommonHelper.GetMiddleText(Lrcsrc, "lyric\":\"", "\",\"tlyric").Trim().Replace("\\n", "\n").Replace("\\r", "");
        }
    }
}
