using DAL;
using Helper;
using IDAL;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
namespace BLL
{
    /// <summary>
    /// 业务逻辑层
    /// </summary>
    public class MusicBLL
    {
        /// <summary>
        /// 数据访问层对象
        /// </summary>
        public IMusic Source { get; set; }
        /// <summary>
        /// BLL有参构造
        /// </summary>
        /// <param name="s"></param>
        public MusicBLL(string s)
        {
            Source = DALFactory.DataAccess.CreateMusic(s);
        }
        /// <summary>
        /// 默认构造
        /// </summary>
        public MusicBLL()
        { }
        /// <summary>
        /// 搜索
        /// </summary>
        /// <param name="word">关键字</param>
        /// <param name="page">页码</param>
        /// <param name="num">返回数量</param>
        /// <returns></returns>
        public List<Music> Search(string word, int page, int num)
        {
            List<Music> list = new List<Music>();
            if (string.IsNullOrEmpty(word.Trim()))
            { return list; }
            list = word.Contains("@") ? 
            SearchCollection(word.Substring(1, word.Length - 1))
            : Source.Search(word, page, num);
            //foreach (var item in list)
            //{
            //    item.Artist=item.Artist.Replace("TFBOYS", "掏粪男孩");
            //}
            return list;
        }
        /// <summary>
        /// 歌单解析
        /// </summary>
        /// <param name="id">歌单ID</param>
        /// <returns></returns>
        public List<Music> SearchCollection(string id)
        {
            List<Music> list = Source.SearchCollection(id);
            return list;
        }
        /// <summary>
        /// 重新搜索
        /// </summary>
        /// <param name="s">下架的歌曲</param>
        /// <param name="similar">最低准确率</param>
        /// <returns></returns>
        public Music ReSearch(Music s, double similar)
        {
            string[] source = Info.ResearchSource;
            List<Music> list = new List<Music>();
            List<Music> result = new List<Music>();
            foreach (string i in source)
            {
                try
                {
                    IMusic dal = DALFactory.DataAccess.CreateMusic(i);
                    list = dal.Search(s.Name, 1, 30);
                    foreach (Music j in list)
                    {
                        i.ToString();
                        double num = j.SimilarWith(s);
                        if (num >= similar)
                        {
                            j.Tag = num;
                            if (dal.GetData(j))
                            {
                                if ((double)j.Tag >= 0.99)
                                {
                                    s = j;
                                    return j;
                                }
                                result.Add(j);
                            }
                        }
                    }
                }
                catch (Exception)
                {

                }
            }
            if (result.Count == 0)
            { throw new Exception("对不起,未能找到你想要的歌曲资源"); }
            return result.Aggregate((u, v) => (double)u.Tag < (double)v.Tag ? u : v);
        }
        /// <summary>
        /// 获取信息
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        public bool GetData(Model.Music m)
        {
            //m.Artist = m.Artist.Replace("TFBOYS", "掏粪男孩");
            if (HttpHelper.CheckUri(m.Url))
            { return true; }
            return Source.GetData(m);
        }
        /// <summary>
        /// 获取歌词
        /// </summary>
        /// <param name="m"></param>
        public void GetLrc(Model.Music m)
        {
            Source.GetLrc(m);
        }
    }
}