namespace DualScreenDemo
{
    public partial class PrimaryForm
    {
        private Button categorySearchButton;
        private Bitmap categorySearchNormalBackground; 
        private Bitmap categorySearchActiveBackground; 

        private Button loveDuetButton;
        private Bitmap loveDuetNormalBackground;
        private Bitmap loveDuetActiveBackground;
        private Button talentShowButton;
        private Bitmap talentShowNormalBackground;
        private Bitmap talentShowActiveBackground;
        private Button medleyDanceButton;
        private Bitmap medleyDanceNormalBackground;
        private Bitmap medleyDanceActiveBackground;
        private Button ninetiesButton;
        private Bitmap ninetiesNormalBackground;
        private Bitmap ninetiesActiveBackground;
        private Button nostalgicSongsButton;
        private Bitmap nostalgicSongsNormalBackground;
        private Bitmap nostalgicSongsActiveBackground;
        private Button chinaSongsButton;
        private Bitmap chinaNormalBackground;
        private Bitmap chinaActiveBackground;
        private Button vietnameseSongsButton;
        private Bitmap vietnameseNormalBackground;
        private Bitmap vietnameseActiveBackground;

        private void UpdateCatBtns(Button activeButton, Image activeBackground) 
        {
            loveDuetButton.BackgroundImage = loveDuetNormalBackground;
            talentShowButton.BackgroundImage = talentShowNormalBackground;
            medleyDanceButton.BackgroundImage = medleyDanceNormalBackground;
            ninetiesButton.BackgroundImage = ninetiesNormalBackground;
            nostalgicSongsButton.BackgroundImage = nostalgicSongsNormalBackground;
            chinaSongsButton.BackgroundImage = chinaNormalBackground;
            vietnameseSongsButton.BackgroundImage = vietnameseNormalBackground;

            activeButton.BackgroundImage = activeBackground;
        }

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

            InitializeButton(ref loveDuetButton, ref loveDuetNormalBackground, ref loveDuetActiveBackground, "loveDuetButton", 1197, 225, 225, 50, data["CategorySubBtn"]["LoveDuetNormal"], data["CategorySubBtn"]["LoveDuetActive"], LoveDuetButton_Click);

            InitializeButton(ref talentShowButton, ref talentShowNormalBackground, ref talentShowActiveBackground, "talentShowButton", 1197, 280, 225, 50, data["CategorySubBtn"]["TalentNormal"], data["CategorySubBtn"]["TalentActive"], TalentShowButton_Click);

            InitializeButton(ref medleyDanceButton, ref medleyDanceNormalBackground, ref medleyDanceActiveBackground, "medleyDanceButton", 1197, 335, 225, 50, data["CategorySubBtn"]["MedleyNormal"], data["CategorySubBtn"]["MedleyActive"], MedleyDanceButton_Click);

            InitializeButton(ref ninetiesButton, ref ninetiesNormalBackground, ref ninetiesActiveBackground, "ninetiesButton", 1197, 390, 225, 50, data["CategorySubBtn"]["NintiesNormail"], data["CategorySubBtn"]["NintiesActive"], NinetiesButton_Click);

            InitializeButton(ref nostalgicSongsButton, ref nostalgicSongsNormalBackground, ref nostalgicSongsActiveBackground, "nostalgicSongsButton", 1197, 445, 225, 50, data["CategorySubBtn"]["NostalgicNormal"], data["CategorySubBtn"]["NostalgicActive"], NostalgicSongsButton_Click);

            InitializeButton(ref chinaSongsButton, ref chinaNormalBackground, ref chinaActiveBackground, "chinaSongsButton", 1197, 500, 225, 50, data["CategorySubBtn"]["ChinaNormal"], data["CategorySubBtn"]["ChinaActive"], ChinaSongsButton_Click);

            InitializeButton(ref vietnameseSongsButton, ref vietnameseNormalBackground, ref vietnameseActiveBackground, "vietnameseSongsButton", 1197, 555, 225, 50, data["CategorySubBtn"]["VietNormal"], data["CategorySubBtn"]["VietActive"], VietnameseSongsButton_Click);

        }

        private void CategorySearchButton_Click(object sender, EventArgs e)
        {
            UpdateButtonBackgrounds(categorySearchButton, categorySearchActiveBackground);

            isOnOrderedSongsPage = false;

 
            /*loveDuetSongs = allSongs.Where(song => song.SongGenre.Contains("A1"))
                                .OrderByDescending(song => song.Plays) 
                                .ToList();*/
            /* 清空搜尋欄 */
            ResetinputBox();
            string query = SetQueryforSQL("A1"); 
            var loveDuetSongs = SearchSongs_Mysql(query);
            currentPage = 0; 
            totalPages = (int)Math.Ceiling((double)loveDuetSongs.Count / itemsPerPage);

            
            multiPagePanel.currentPageIndex = 0;
            multiPagePanel.LoadSongs(loveDuetSongs);


            SetHotSongButtonsVisibility(false);
            SetNewSongButtonsVisibility(false);
            SetSingerSearchButtonsVisibility(false);
            SetSongSearchButtonsVisibility(false);
            SetPictureBoxLanguageButtonsVisibility(false);
            SetGroupButtonsVisibility(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); 

            SetPictureBoxCategoryAndButtonsVisibility(true);
           
            UpdateCatBtns(loveDuetButton, loveDuetActiveBackground);

            if (pictureBoxQRCode != null)
            {
                pictureBoxQRCode.Visible = false;
                closeQRCodeButton.Visible = false;
            }
        }
        
        private void TogglePictureBoxCategoryButtonsVisibility()
        {
            
            bool areButtonsVisible = loveDuetButton.Visible; 

            
            SetPictureBoxCategoryAndButtonsVisibility(!areButtonsVisible);
        }
        
        private void SetPictureBoxCategoryAndButtonsVisibility(bool isVisible)
        {
            loveDuetButton.Visible = isVisible;
            loveDuetButton.BringToFront();

            talentShowButton.Visible = isVisible;
            talentShowButton.BringToFront();

            medleyDanceButton.Visible = isVisible;
            medleyDanceButton.BringToFront();

            ninetiesButton.Visible = isVisible;
            ninetiesButton.BringToFront();

            nostalgicSongsButton.Visible = isVisible;
            nostalgicSongsButton.BringToFront();

            chinaSongsButton.Visible = isVisible;
            chinaSongsButton.BringToFront();

            vietnameseSongsButton.Visible = isVisible;
            vietnameseSongsButton.BringToFront();



        }

        private string SetQueryforSQL(string contains){
            string query = $"SELECT * FROM song_library_cache WHERE song_category LIKE '%{contains}%' ORDER BY `song_id` DESC;";
            return query;
        }
    }
}