using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Helper;
using System.Text.RegularExpressions;
using System.IO;
namespace Model
{
    /// <summary>
    /// 音乐实体
    /// </summary>
    [Serializable()]
    public class Music
    {
        /// <summary>
        /// ID
        /// </summary>
        public string Id { get; set; }
        //曲名
        public string Name { get; set; }
        /// <summary>
        /// 艺术家
        /// </summary>
        public string Artist { get; set; }
        /// <summary>
        /// 专辑
        /// </summary>
        public string Album { get; set; }
        /// <summary>
        /// 封面ID
        /// </summary>
        public string PicId { get; set; }
        /// <summary>
        /// 封面链接
        /// </summary>
        public string PicUrl { get; set; }
        /// <summary>
        /// 歌词
        /// </summary>
        public string Lrc { get; set; }
        /// <summary>
        /// 播放地址
        /// </summary>
        public string Url { get; set; }
        /// <summary>
        /// 来源
        /// </summary>
        public string Source { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public object Tag { get; set; }
        public Lyric GetLrc()
        {
            return Lyric.Parse(Lrc);
        }
        public bool IsVideo()
        {
            return Url.Contains("mp4") || Url.Contains("swf");
        }
        /// <summary>
        /// Music默认构造
        /// </summary>
        public Music()
        { }
        public string ToBase64()
        {
            return ConvertEx.ObjectToBase64(this);
        }
        public static Music FromBase64(string code)
        {
            return ConvertEx.Base64ToObject(code) as Music;
        }
        /// <summary>
        /// 从DataRow中获取实体
        /// </summary>
        /// <param name="row">行</param>
        /// <returns></returns>
        public static Music FromRow(DataRow row)
        {
            ModelConvertHelper<Music> h = new ModelConvertHelper<Music>();
            return h.Row2Model<Music>(row);
        }
        /// <summary>
        /// 从DataGridView中获取实体
        /// </summary>
        /// <param name="row">行</param>
        /// <returns></returns>
        public static Music FromDataGridView(System.Windows.Forms.DataGridViewRow row)
        {
            DataRowView row_view = (DataRowView)row.DataBoundItem;
            DataTable dt = row_view.DataView.Table.Clone();//克隆DataTable结构
            dt.ImportRow(row_view.Row);
            ModelConvertHelper<Music> h = new ModelConvertHelper<Music>();
            return h.Row2Model<Music>(dt.Rows[0]);
        }
        /// <summary>
        /// 实体集合转DataTable
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public static DataTable ModelToTable(List<Music> list)
        {
            ModelConvertHelper<Music> h = new ModelConvertHelper<Music>();
            return h.ListToDataTable(list);
        }
        /// <summary>
        /// DataTable转实体集合
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static List<Music> TableToModel(DataTable dt)
        {
            ModelConvertHelper<Music> h = new ModelConvertHelper<Music>();
            return ModelConvertHelper<Music>.DataTableToList(dt);
        }

        public static string ListToJson(List<Music> list)
        {
            return ConvertEx.DateTableToJson( ModelToTable(list));
        }

        public static List<Music> JsonToList(string json)
        {
            return TableToModel(ConvertEx.JsonToDataTable(json));
        }

        public override bool Equals(object obj)
        {
            if (obj != null)
                return SimilarWith(obj as Music) == 1.0;
            else { return false; }
        }
        /// <summary>
        /// 比较两首歌信息的相似度
        /// </summary>
        /// <param name="s">要比较的歌曲</param>
        /// <returns></returns>
        public double SimilarWith(Music s)
        {
            string name = Regex.Replace(this.Name.ToLower().Replace(" ", ""), "-《.*", "");
            string targetName = Regex.Replace(s.Name.ToLower().Replace(" ", ""), "-《.*", "");
            string artist = this.Artist.ToLower().Replace(" ", "");
            string targetArtist = s.Artist.ToLower().Replace(" ", "");
            if (name == targetName && artist == targetArtist)
            { return 1; }
            //	你的眼睛-《在一起》电视剧插曲
            if (CommonHelper.GetSimilarityWith(name, targetName) < 0.9) { return 0; }
            return CommonHelper.GetSimilarityWith(artist, targetArtist);
        }
        /// <summary>
        /// 输出格式
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            Artist = CommonHelper.RemoveInvalidFile(Artist);
            Name = CommonHelper.RemoveInvalidFile(Name);
            Album = CommonHelper.RemoveInvalidFile(Album);
            return string.Format("{0} - {1}", Artist, Name);
        }
        /// <summary>
        /// 反序列化
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static Music FromFile(string path)
        {
            return Helper.ConvertEx.Bytes2Object(File.ReadAllBytes(path)) as Music;
        }
        /// <summary>
        /// 序列化
        /// </summary>
        /// <param name="path"></param>
        public void SaveFile(string path)
        {
            File.WriteAllBytes(path, Helper.ConvertEx.Object2Bytes(this));
        }
        public static List<Music> LoadList(string path)
        {
            string[] filenames = Directory.GetFiles(path, "*.song", SearchOption.TopDirectoryOnly);
            List<Music> list = new List<Music>();
            foreach (string item in filenames)
            {
                list.Add(FromFile(item));
            }
            return list;
        }
        public static void SaveList(string path, List<Music> list)
        {
            foreach (Music item in list)
            {
                item.SaveFile(string.Format("{0}\\{1} - {2}.song", path, item.Artist, item.Name));
            }
        }
        /// <summary>
        /// 收藏
        /// </summary>
        public bool Love { get; set; }
    }
}
