using DAL;
using Model;
using BLL;
using Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace 控制台_无界面
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            try
            {
                Demo();
                return;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                GC.Collect();
                Console.WriteLine("按任意键退出");
                Console.ReadKey();
            }
        }

        public static void Demo()
        {
            Console.Title = "遗憾迟香音乐助手";
            Console.WriteLine("请选择搜索源");
            for (int i = 0; i < DAL.Info.Sources.Count; i++)
            {
                Console.WriteLine("{0}:{1}", i + 1, DAL.Info.Sources[i]);
            }
            MusicBLL bll = new MusicBLL(DAL.Info.Sources[int.Parse(Console.ReadLine()) - 1].Value);
            Console.WriteLine("请输入关键字:(@+歌单Id为歌单解析)");
            string word = Console.ReadLine();
            Console.WriteLine("正在搜索");
            List<Music> list = bll.Search(word, 1, 30);
            for (int i = 0; i < list.Count; i++)
            {
                Console.WriteLine("{0}:{1}", i + 1, list[i]);
            }
            Console.WriteLine("请选择一首歌");
            Music m = list[int.Parse(Console.ReadLine()) - 1];
            Console.WriteLine("正在获取歌曲信息");
            bll = new MusicBLL(m.Source);
            if (!bll.GetData(m))
            {
                Console.WriteLine("未能解析出播放地址,正在搜索其它平台");
                bll.ReSearch(m, 0.75);
            }
            Console.WriteLine("曲名:{0}\n艺术家:{1}\n专辑:{2}\n封面地址:\n{3}\n播放地址:\n{4}\n", m.Name, m.Artist, m.Album, m.PicUrl, m.Url);
            Console.WriteLine("按任意键播放");
            Console.ReadKey();
            DateTime now = DateTime.Now;
            Mp3Player.Play(m.Url, false);
            Lyric l = m.GetLrc();
            //计算超前时间
            while (true)
            {
                try
                {
                    double dur = (DateTime.Now - now).TotalSeconds;
                    double offset = dur + l.Offset;
                    //Console.Clear();
                    for (int i = 0; i < l.Words.Count; i++)
                    {//循环遍历歌词文本
                        if (l.Words[i].Time >= offset)
                        {//如果那句歌词时间大于等于进度则显示
                            Console.Clear();
                            Console.WriteLine(l.Words[i - 1]);
                            break;
                        }
                    }
                    Thread.Sleep(100);
                }
                catch (Exception)
                { }
            }
        }
    }
}
