using DBObj;
using MySqlConnector;
using System.Globalization; 

namespace DualScreenDemo
{
    public partial class PrimaryForm
    {       
        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;

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

            activeButton.BackgroundImage = activeBackground;
        }

        private void InitializeLangSearchButtons()
        {
            var data = LoadBtnConfigData();


            InitializeButton(ref guoYuButton, ref guoYuNormalBackground, ref guoYuActiveBackground, "guoYuButton", 1197, 225, 225, 50, data["LangSearchSubBtn"]["GuoYuLangNormal"], data["LangSearchSubBtn"]["GuoYuLangActive"], GuoYuButton_Click);

            InitializeButton(ref taiYuButton, ref taiYuNormalBackground, ref taiYuActiveBackground, "taiYuButton", 1197, 280, 225, 50, data["LangSearchSubBtn"]["TaiYuLangNormal"], data["LangSearchSubBtn"]["TaiYuLangActive"], TaiYuButton_Click);

            InitializeButton(ref yueYuButton, ref yueYuNormalBackground, ref yueYuActiveBackground, "yueYuButton", 1197, 335, 225, 50, data["LangSearchSubBtn"]["YueYuLangNormal"], data["LangSearchSubBtn"]["YueYuLangActive"], YueYuButton_Click);

            InitializeButton(ref yingWenButton, ref yingWenNormalBackground, ref yingWenActiveBackground, "yingWenButton", 1197, 390, 225, 50, data["LangSearchSubBtn"]["EngLangNormal"], data["LangSearchSubBtn"]["EngLangActive"], YingWenButton_Click);

            InitializeButton(ref riYuButton, ref riYuNormalBackground, ref riYuActiveBackground, "riYuButton", 1197, 445, 225, 50, data["LangSearchSubBtn"]["JapLangNormal"], data["LangSearchSubBtn"]["JapLangActive"], RiYuButton_Click);

            InitializeButton(ref hanYuButton, ref hanYuNormalBackground, ref hanYuActiveBackground, "hanYuButton", 1197, 500, 225, 50, data["LangSearchSubBtn"]["KrLangNormal"], data["LangSearchSubBtn"]["KrLangActive"], HanYuButton_Click);

            InitializeButton(ref keYuButton, ref keYuNormalBackground, ref keYuActiveBackground, "keYuButton", 1197, 555, 225, 50, data["LangSearchSubBtn"]["KeYuLangNormal"], data["LangSearchSubBtn"]["KeYuLangActive"], KeYuButton_Click);

        }

        private void LanguageSongSelectionButton_Click(object sender, EventArgs e)
        {

            UpdateButtonBackgrounds(languageSearchButton, languageSearchActiveBackground);
            isOnOrderedSongsPage = false;


            /* 清空搜尋欄 */
            ResetinputBox();
            string query = $"SELECT * FROM song_library_cache WHERE language_name = '國語' ORDER BY song_counts DESC LIMIT 1000";
            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);
            SetHandWritingForSongsAndButtonsVisibility(false);
            SetWordCountSongsAndButtonsVisibility(false);
            SetSongIDSearchAndButtonsVisibility(false);  
            SetHandWritingForSingersAndButtonsVisibility(false);
            SetWordCountSingersAndButtonsVisibility(false); 

            SetPictureBoxLanguageButtonsVisibility(true);


            UpdateLangBtns(guoYuButton, guoYuActiveBackground);


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

        private void OnLanguageButtonClick(Button activeButton, Image activeBackground, string category)
        {

            UpdateLangBtns(activeButton, activeBackground);

            string query = $"SELECT * FROM song_library_cache WHERE language_name = '{category}' ORDER BY song_counts DESC LIMIT 1000";
            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();
        }

    }
}