using MySqlConnector;
using DBObj;
using System.Globalization; 
using System.IO;
namespace DualScreenDemo{
    public partial class PrimaryForm
    {
        public List<SongData> SearchSongs_Mysql(string query)
        {
            List<SongData> searchResults = new List<SongData>();
            Console.WriteLine(query);
            string connectionString = "Server=192.168.22.170;Port=3306;Database=Karaoke-Kingpin;User=Karaoke-Kingpin;Password=ESM7yTPMnavFmbBH;";

            using (var connection = new MySqlConnection(connectionString))
            {
                connection.Open();
                Console.WriteLine("MyDB 連線成功！");

                using (var command = new MySqlCommand(query, connection))
                {

                    using (var reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            string songNumber = reader["歌曲編號"].ToString();
                            string category = reader["語別"].ToString();
                            string song = reader["歌曲名稱"].ToString();
                            int plays = Convert.ToInt32(reader["點播次數"]);
                            string artistA = reader["歌星 A"].ToString();
                            string artistB = reader["歌星 B"].ToString();
                            string artistACategory = reader["歌星A分類"].ToString();
                            string artistBCategory = reader["歌星B分類"].ToString();
                            string dateValue = reader["新增日期"]?.ToString() ?? "";
                            DateTime addedTime;
                            try
                            {
                                addedTime = DateTime.Parse(dateValue, CultureInfo.InvariantCulture).Date;
                            }
                            catch
                            {
                                addedTime = DateTime.Today;
                            }

                            string basePathHost1 = reader["路徑 1"].ToString();
                            string basePathHost2 = reader["路徑 2"].ToString();
                            string fileName = reader["歌曲檔名"].ToString();
                            string songFilePathHost1 = Path.Combine(basePathHost1, fileName);
                            string songFilePathHost2 = Path.Combine(basePathHost2, fileName);
                            string phoneticNotation = reader["歌曲注音"].ToString();
                            string pinyinNotation = reader["歌曲拼音"].ToString();
                            string artistAPhonetic = reader["歌星A注音"].ToString();
                            string artistBPhonetic = reader["歌星B注音"].ToString();
                            string artistASimplified = reader["歌星A簡體"].ToString();
                            string artistBSimplified = reader["歌星B簡體"].ToString();
                            string songSimplified = reader["歌名簡體"].ToString();
                            string songGenre = reader["分類"].ToString();
                            string artistAPinyin = reader["歌星A拼音"].ToString();
                            string artistBPinyin = reader["歌星B拼音"].ToString();
                            int humanVoice = Convert.ToInt32(reader["人聲"]);

                            searchResults.Add(new SongData(
                                songNumber, category, song, plays, artistA, artistB,
                                artistACategory, artistBCategory, addedTime,
                                songFilePathHost1, songFilePathHost2,
                                phoneticNotation, pinyinNotation,
                                artistAPhonetic, artistBPhonetic,
                                artistASimplified, artistBSimplified,
                                songSimplified, songGenre,
                                artistAPinyin, artistBPinyin,
                                humanVoice
                            ));
                        }
                    }
                }

                connection.Close();
                Console.WriteLine("MyDB 連線已關閉！");
            }

            return searchResults;
        }

        public List<Artist> SearchSingers_Mysql(string query){
            List<Artist> searchResults = new List<Artist>();
            Console.WriteLine(query);
             string connectionString = "Server=192.168.22.170;Port=3306;Database=Karaoke-Kingpin;User=Karaoke-Kingpin;Password=ESM7yTPMnavFmbBH;";

            using (var connection = new MySqlConnection(connectionString))
            {
                connection.Open();
                Console.WriteLine("MyDB 連線成功！");

                using (var command = new MySqlCommand(query, connection))
                {

                    using (var reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            string artist = reader["歌手姓名"].ToString();
                            string phonetic = reader["歌手注音"].ToString();
                            string category = reader["歌手分類"].ToString();
                            string strokesStr = reader["歌手筆畫"].ToString();
                            if (double.TryParse(strokesStr, out double strokesDouble))
                            {
                                int strokes = (int)Math.Round(strokesDouble); 
                                searchResults.Add(new Artist(artist, phonetic, category, strokes));
                            }

                        }
                    }
                }
                connection.Close();
                Console.WriteLine("MyDB 連線已關閉！");
            }
            return searchResults;

        }
    }

}