using DBObj;
using MySqlConnector;
using System.Globalization; 
using System.IO;

namespace DualScreenDemo
{
    public partial class PrimaryForm
    {
        
        private void InitializeButton(ref Button button, string buttonText, int x, int y, int width, int height, Rectangle cropArea, Image normalBackground, out Bitmap normalBackgroundOut, Image activeBackground, out Bitmap activeBackgroundOut, EventHandler clickEventHandler)
        {
            button = new Button { Text = "", Visible = false };
            ResizeAndPositionButton(button, x, y, width, height);
            normalBackgroundOut = new Bitmap(normalBackground).Clone(cropArea, normalBackground.PixelFormat);
            activeBackgroundOut = new Bitmap(activeBackground).Clone(cropArea, activeBackground.PixelFormat);
            button.BackgroundImage = normalBackgroundOut;
            button.BackgroundImageLayout = ImageLayout.Stretch;
            button.FlatStyle = FlatStyle.Flat;
            button.FlatAppearance.BorderSize = 0;
            button.Click += clickEventHandler;
            this.Controls.Add(button);
        }

        private Button languageSearchButton;
        private Bitmap languageSearchNormalBackground; 
        private Bitmap languageSearchActiveBackground; 

        private Button guoYuButton;
        private Bitmap guoYuNormalBackground;
        private Bitmap guoYuActiveBackground;
        private Button taiYuButton;
        private Bitmap taiYuNormalBackground;
        private Bitmap taiYuActiveBackground;
        private Button yueYuButton;
        private Bitmap yueYuNormalBackground;
        private Bitmap yueYuActiveBackground;
        private Button yingWenButton;
        private Bitmap yingWenNormalBackground;
        private Bitmap yingWenActiveBackground;
        private Button riYuButton;
        private Bitmap riYuNormalBackground;
        private Bitmap riYuActiveBackground;
        private Button hanYuButton;
        private Bitmap hanYuNormalBackground;
        private Bitmap hanYuActiveBackground;
        private Button keYuButton;
        private Bitmap keYuNormalBackground;
        private Bitmap keYuActiveBackground;
        
        // 語別查詢 query LIMIT 100 可更改

        private void LanguageSongSelectionButton_Click(object sender, EventArgs e)
        {
            newSongAlertButton.BackgroundImage = newSongAlertNormalBackground;
            hotPlayButton.BackgroundImage = hotPlayNormalBackground;
            singerSearchButton.BackgroundImage = singerSearchNormalBackground;
            songSearchButton.BackgroundImage = songSearchNormalBackground;
            languageSearchButton.BackgroundImage = languageSearchActiveBackground;
            groupSearchButton.BackgroundImage = groupSearchNormalBackground;
            categorySearchButton.BackgroundImage = categorySearchNormalBackground;
            orderedSongsButton.BackgroundImage = orderedSongsNormalBackground;
            myFavoritesButton.BackgroundImage = myFavoritesNormalBackground;
            promotionsButton.BackgroundImage = promotionsNormalBackground;
            deliciousFoodButton.BackgroundImage = deliciousFoodNormalBackground;
            isOnOrderedSongsPage = false;

            guoYuButton.BackgroundImage = guoYuActiveBackground;
            taiYuButton.BackgroundImage = taiYuNormalBackground;
            yueYuButton.BackgroundImage = yueYuNormalBackground;
            yingWenButton.BackgroundImage = yingWenNormalBackground;
            riYuButton.BackgroundImage = riYuNormalBackground;
            hanYuButton.BackgroundImage = hanYuNormalBackground;
            keYuButton.BackgroundImage = keYuNormalBackground;

            /* 清空搜尋欄 */
            ResetinputBox();
            string query = $"SELECT * FROM song_library_cache WHERE language_name = '國語' ORDER BY song_counts DESC LIMIT 100";
            var guoYuSongs = SearchSongs_Mysql(query);
            currentPage = 0; 
            totalPages = (int)Math.Ceiling((double)guoYuSongs.Count / itemsPerPage);

            multiPagePanel.currentPageIndex = 0;
            multiPagePanel.LoadSongs(guoYuSongs);
            
            SetHotSongButtonsVisibility(false);
            SetNewSongButtonsVisibility(false);
            SetSingerSearchButtonsVisibility(false);
            SetSongSearchButtonsVisibility(false);
            SetGroupButtonsVisibility(false);
            SetPictureBoxCategoryAndButtonsVisibility(false);
            SetZhuYinSingersAndButtonsVisibility(false);
            SetZhuYinSongsAndButtonsVisibility(false);
            SetEnglishSingersAndButtonsVisibility(false);
            SetEnglishSongsAndButtonsVisibility(false);
            SetPinYinSingersAndButtonsVisibility(false);
            SetPinYinSongsAndButtonsVisibility(false);
            SetPictureBoxToggleLightAndButtonsVisibility(false);
            SetPictureBoxSceneSoundEffectsAndButtonsVisibility(false);
            SetPictureBoxLanguageButtonsVisibility(true);

            
            if (pictureBoxQRCode != null)
            {
                pictureBoxQRCode.Visible = false;
                closeQRCodeButton.Visible = false;
            }
        }

        private void OnLanguageButtonClick(Button activeButton, Image activeBackground, string category)
        {
            
            guoYuButton.BackgroundImage = guoYuNormalBackground;
            taiYuButton.BackgroundImage = taiYuNormalBackground;
            yueYuButton.BackgroundImage = yueYuNormalBackground;
            yingWenButton.BackgroundImage = yingWenNormalBackground;
            riYuButton.BackgroundImage = riYuNormalBackground;
            hanYuButton.BackgroundImage = hanYuNormalBackground;
            keYuButton.BackgroundImage = keYuNormalBackground;

            
            activeButton.BackgroundImage = activeBackground;
            string query = $"SELECT * FROM song_library_cache WHERE language_name = '{category}' ORDER BY song_counts DESC LIMIT 100";
            var selectedSongs = SearchSongs_Mysql(query);

            currentPage = 0; 
            totalPages = (int)Math.Ceiling((double)selectedSongs.Count / itemsPerPage);

            multiPagePanel.currentPageIndex = 0;
            multiPagePanel.LoadSongs(selectedSongs);
        }

        private void SetPictureBoxLanguageButtonsVisibility(bool isVisible)
        {   
            guoYuButton.Visible = isVisible;
            guoYuButton.BringToFront();
            
            taiYuButton.Visible = isVisible;
            taiYuButton.BringToFront();
            
            yueYuButton.Visible = isVisible;
            yueYuButton.BringToFront();
            
            yingWenButton.Visible = isVisible;
            yingWenButton.BringToFront();
            
            riYuButton.Visible = isVisible;
            riYuButton.BringToFront();
            
            hanYuButton.Visible = isVisible;
            hanYuButton.BringToFront();

            keYuButton.Visible = isVisible;
            keYuButton.BringToFront();
        }

        private void InitializeButtonsForPictureBoxLanguageQuery()
        {
            InitializeGuoYuButton();
            InitializeTaiYuButton();
            InitializeYueYuButton();
            InitializeYingWenButton();
            InitializeRiYuButton();
            InitializeHanYuButton();
            InitializeKeYuButton();
        }
    }
}