using System.Data.SQLite;
using System.IO;
using System.Globalization;  
using System.Diagnostics;
using DualScreenDemo;
namespace DBObj
{
    public class SongListManager
    {
        public List<SongData> FavoriteSongs { get; private set; }  
        //public bool IsUserLoggedIn { get; set; }  
        //public string UserPhoneNumber { get; set; }  
        public SongListManager()
        {
            FavoriteSongs = new List<SongData>();
        }
        /*
        public void AddNewUser(string phoneNumber)
        {
            string databaseFileName = "KSongDatabase.db";
            string databasePath = Path.Combine(Application.StartupPath, "db", databaseFileName);
            string connectionString = String.Format("Data Source={0};Version=3;", databasePath);

            using (var connection = new SQLiteConnection(connectionString))
            {
                try
                {
                    connection.Open();

                    
                    string checkTableSql = "SELECT name FROM sqlite_master WHERE type='table' AND name='FavoriteSongs';";
                    using (var checkCommand = new SQLiteCommand(checkTableSql, connection))
                    {
                        var result = checkCommand.ExecuteScalar();
                        if (result == null)
                        {
                            throw new Exception("Table 'FavoriteSongs' does not exist.");
                        }
                    }

                    string sql = "INSERT INTO FavoriteSongs (PhoneNumber, SongNumber) VALUES (@PhoneNumber, @SongNumber)";
                    using (var command = new SQLiteCommand(sql, connection))
                    {
                        command.Parameters.AddWithValue("@PhoneNumber", phoneNumber);
                        command.Parameters.AddWithValue("@SongNumber", "000000");
                        command.ExecuteNonQuery();
                    }
                    connection.Close();
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Failed to add new user to SQLite database: " + ex.Message);
                }
            }
        }
        */
        public List<SongData> SearchNewSongs(){
            string query= $"SELECT * FROM song_library_cache WHERE language_name = '國語' ORDER BY add_date DESC LIMIT {PrimaryForm.ReadNewSongLimit()}";
            return PrimaryForm.Instance.SearchSongs_Mysql(query);
        }
        public List<SongData> SearchHotSongs(){
            string query= $"SELECT * FROM song_library_cache WHERE language_name = '國語' ORDER BY add_date DESC LIMIT {PrimaryForm.ReadHotSongLimit()}";
            return PrimaryForm.Instance.SearchSongs_Mysql(query);
        }
        public List<SongData> SearchSongsBySinger(string keyword)
        {
            var keywordLower = keyword.ToLower();
            string query = $"SELECT * FROM song_library_cache WHERE artistA ='{keywordLower}' OR artistB='{keywordLower}'";
            return PrimaryForm.Instance.SearchSongs_Mysql(query);
        }
        public List<SongData> SearchSongsByName(string keyword)
        {
            string query = $"SELECT * FROM song_library_cache WHERE LOWER(song_name) LIKE CONCAT('%', LOWER('{keyword}'), '%')";
            return PrimaryForm.Instance.SearchSongs_Mysql(query);
        }        
        public SongData SearchSongByNumber(string songNumber)
        {
            string query = $"SELECT * FROM song_library_cache WHERE song_id = {songNumber}";
            var searchResults =PrimaryForm.Instance.SearchSongs_Mysql(query);
            return  searchResults.FirstOrDefault();
        }

    }
}