using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.IO;
using Newtonsoft.Json;


namespace NetEZ
{
    public partial class MusicNetEZ : Form
    {
        public bool togglefloat = false;
        public MusicNetEZ()
        {

            InitializeComponent();
            if (Directory.Exists("NetEZmusic") == false)
                Directory.CreateDirectory("NetEZmusic");
        }

        private void btn_download_Click(object sender, EventArgs e)
        {   
            pb_down.Value = 10;
            pb_down.Visible = true;
            
            
            string SongsId = t_songid.Text;
            HttpWebRequest getlink = WebRequest.CreateHttp("http://music.163.com/song/media/outer/url?id=" + SongsId + ".mp3");
            getlink.AllowAutoRedirect = false;
            getlink.Method = "GET";
            getlink.ContentType = "text/html;charset=UTF-8";
            getlink.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9";
            getlink.Host = "music.163.com";
            getlink.Headers.Add(HttpRequestHeader.KeepAlive, "TRUE");
            HttpWebResponse songres = (HttpWebResponse)getlink.GetResponse();
            string downlink = songres.Headers["Location"];
            HttpWebRequest dldSong = WebRequest.CreateHttp(downlink);
            HttpWebResponse dldres = dldSong.GetResponse() as HttpWebResponse;
            Stream dldstream = dldres.GetResponseStream();
            pb_down.Value = 99;
            string inpath;
            if (t_path.Text == null)
            {
                inpath = "newNetEZsong";
            }
            else
            {
                inpath = t_path.Text;
            }
            Stream stream = new FileStream("NetEZmusic/" + inpath + ".mp3", FileMode.Create);
            byte[] bArr = new byte[1024];
            int size = dldstream.Read(bArr, 0, (int)bArr.Length);
            while (size > 0)
            {
                stream.Write(bArr, 0, size);
                size = dldstream.Read(bArr, 0, (int)bArr.Length);
            }
            stream.Close();
            dldstream.Close();
            pb_down.Value = 100;
            pb_down.Visible = false;
            

        }

        private void btn_up_Click(object sender, EventArgs e)
        {

            if (togglefloat == false)
            {
                togglefloat = true;
                this.TopMost = togglefloat;

            }
            else
            {
                togglefloat = false;
                this.TopMost = togglefloat;
                
            }
        }

        private void ll_email_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start("http://mail.qq.com/");
        }

        private void btn_quest_Click(object sender, EventArgs e)
        {
            new quest().ShowDialog();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string keyword = t_search.Text;
            HttpWebRequest getsongs = WebRequest.CreateHttp("https://v1.alapi.cn/api/music/search?keyword=" + keyword);
            getsongs.Method = "GET";
            getsongs.ContentType = "text/html;charset=UTF-8";
            getsongs.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9";
            getsongs.Headers.Add(HttpRequestHeader.KeepAlive, "TRUE");
            HttpWebResponse searchback = (HttpWebResponse)getsongs.GetResponse();
            Stream searchstream = searchback.GetResponseStream();
            StreamReader myStreamReader = new StreamReader(searchstream, Encoding.GetEncoding("utf-8"));
            string retString = myStreamReader.ReadToEnd();
            searchstream.Close();
            //MessageBox.Show(retString);
            //传入我们的实体类还有需要解析的JSON字符串这样就OK了。然后就可以通过实体类使用数据了。
            Root rt = JsonConvert.DeserializeObject<Root>(retString);
            //这样就可以取出json数据里面的值
            //MessageBox.Show("id=" + rt.id + "\r\n" + "mark=" + rt.mark + "\r\n" + "mvid=" + rt.mvid + "\r\n" + "name=" + rt.name + "\r\n" + "rtype=" + rt.rtype + "\r\n" + "rUrl=" + rt.rUrl + "\r\n" + "status=" + rt.status + "\r\n" + "artists=" + rt.artists);
            //由于这个JSON字符串的 public List<DataItem> data 是一个集合，所以我们需要遍历集合里面的所有数据
            for (int i = 0; i < rt.data.songs.Count; i++)
            {
                //MessageBox.Show("id=" + rt.data.songs[i].id + "\r\n" + rt.data.songs[i].name);
                //dgv_search.Rows.Clear();
                dgv_search.Rows.Add();
                dgv_search.Rows[i].Cells[0].Value = rt.data.songs[i].name;
                dgv_search.Rows[i].Cells[1].Value = rt.data.songs[i].artists[0].name;
                dgv_search.Rows[i].Cells[2].Value = rt.data.songs[i].id;
            }
        }

        private void dgv_search_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
        }

        private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {

        }

        private void 复制ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
            string C_selectedcell = dgv_search.CurrentCell.Value.ToString();
            if (C_selectedcell != "")
                Clipboard.SetDataObject(C_selectedcell);
        }

        private void 搜索ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string C_selectedcell = dgv_search.CurrentRow.Cells[2].Value.ToString();
            string C_selectedname = dgv_search.CurrentRow.Cells[0].Value.ToString() + "-" + dgv_search.CurrentRow.Cells[1].Value.ToString();

            if (C_selectedcell != "")
            {
                t_songid.Text = C_selectedcell;
                t_path.Text = C_selectedname;
            }
        }

        private void btn_open_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("ExpLore", Environment.CurrentDirectory);
        }
    }
}
