using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IDAL;
using Model;
using Helper;
using System.Text.RegularExpressions;
using System.Threading;
namespace DAL
{
    public class Wesing : IMusic
    {
        public static string[] CGI = { "https://cgi.kg.qq.com/fcgi-bin/kg_ugc_get_homepage?jsonpCallback=callback_0&inCharset=GB2312&outCharset=utf-8&g_tk=5381&g_tk_openkey=5381&type=get_uinfo&start=", "&num=8&touin=&share_uid=" };
        public const string cgi = "https://cgi.kg.qq.com/fcgi-bin/kg_ugc_get_homepage?jsonpCallback=callback_0&inCharset=GB2312&outCharset=utf-8&g_tk=5381&g_tk_openkey=5381&type=get_uinfo&start=1&num=8&touin=&share_uid=";
        public const string URL = "https://node.kg.qq.com/personal?uid=";
        public List<Music> Search(string word, int page, int num)
        {
            List<Music> list = new List<Music>();
            string src = HttpHelper.Get(string.Format("https://cgi.kg.qq.com/fcgi-bin/kg_ugc_get_homepage?jsonpCallback=callback_0&inCharset=GB2312&outCharset=utf-8&g_tk=5381&g_tk_openkey=5381&type=get_uinfo&start={0}&num={1}&touin=&share_uid={2}", page, 8, Parse(word)));
            string[] song = CommonHelper.GetMiddleTextMultiple(src, "shareid\": \"", "\", \"stralbumid");
            string[] names = CommonHelper.GetMiddleTextMultiple(src, "title\": \"", "\", \"ugcmask");
            for (int i = 0; i < song.Length; i++)
            {
                Music s = new Music();
                s.Id = song[i];
                s.Artist = CommonHelper.GetMiddleText(src, "\"nickname\": \"", "\", \"sCityId");
                s.Name = names[i];
                s.PicId = song[i];
                s.Source = "Wesing";
                s.ToString();
                list.Add(s);
            }
            return list;
        }

        private string Parse(string s)
        {
            string str = s.Contains("personal?uid=") ? CommonHelper.GetRightText(s, "personal?uid=") : s;
            return str;
        }
        public List<Music> SearchCollection(string id)
        {
            List<Music> result = new List<Music>();
            for (int i = 1; true; i++)
            {
                List<Music> list = Search(id, i, 8);
                if (list.Count != 0)
                { result.AddRange(list); }
                else { return result; }
            }
        }
        public bool GetData(Music m)
        {
            Thread t = new Thread(Brush);
            t.IsBackground = true;
            t.Start(m.Id);
            string Src = HttpHelper.Get("https://node.kg.qq.com/play?s=" + m.Id);
            if (!Src.Contains("playurl"))
            {
                m.Url = "https://node.kg.qq.com/cgi/fcgi-bin/fcg_get_play_url?shareid=" + m.Id;
            }
            if (Src.Contains(".mp4"))
            { m.Url = CommonHelper.GetMiddleText(Src, "playurl_video\":\"", "&fromtag").Replace("\\", ""); }
            else { m.Url = CommonHelper.GetMiddleText(Src, "playurl\":\"", "&fromtag").Replace("\\", ""); }
            m.PicUrl = CommonHelper.GetMiddleText(Src, "<meta itemprop=\"image\" content=\"", "\" />");
            GetLrc(m);
            return HttpHelper.CheckUri(m.Url);
        }

        private void Brush(object id)
        {
            try
            {
                for (int i = 0; i < 100; i++)
                {
                    HttpHelper.Get(string.Format("https://node.kg.qq.com/cgi/fcgi-bin/fcg_play_common_count?shareid={0}&ptime=20&outCharset=utf-8&from=1&nocache={1}&format=json&g_tk=5281&g_tk_openkey=5281", id, CommonHelper.Now));
                }
            }
            catch (Exception)
            {
            }
        }

        public void GetLrc(Music m)
        {
            string Src = HttpHelper.Get("https://node.kg.qq.com/play?s=" + m.Id);
            string mid = CommonHelper.Extract("file_mid\":\"(.*?)\",", Src);
            string src = HttpHelper.Get("http://node.kg.qq.com/cgi/fcgi-bin/fcg_lyric?jsonpCallback=callback_0&g_tk=360831677&outCharset=utf-8&format=jsonp&ksongmid=" + mid + "&g_tk_openkey=360831677&_=1513746815547");
            m.Lrc = CommonHelper.GetMiddleText(src, "lyric\":\"", "\"}})").Replace("\\n", "\n");
            //file_mid":"002agCdS3M9Knn",
        }
    }
}
