namespace DualScreenDemo
{
    public partial class PrimaryForm
    {
        private Button groupSearchButton;
        private Bitmap groupSearchNormalBackground;
        private Bitmap groupSearchActiveBackground;

        private Button groupGuoYuButton;
        private Bitmap groupGuoYuNormalBackground;
        private Bitmap groupGuoYuActiveBackground;
        private Button groupTaiYuButton;
        private Bitmap groupTaiYuNormalBackground;
        private Bitmap groupTaiYuActiveBackground;
        private Button groupYueYuButton;
        private Bitmap groupYueYuNormalBackground;
        private Bitmap groupYueYuActiveBackground;
        private Button groupYingWenButton;
        private Bitmap groupYingWenNormalBackground;
        private Bitmap groupYingWenActiveBackground;
        private Button groupRiYuButton;
        private Bitmap groupRiYuNormalBackground;
        private Bitmap groupRiYuActiveBackground;
        private Button groupHanYuButton;
        private Bitmap groupHanYuNormalBackground;
        private Bitmap groupHanYuActiveBackground;


        private void UpdateGroupSubBtns(Button activeButton, Image activeBackground)
        {
            groupGuoYuButton.BackgroundImage = groupGuoYuNormalBackground;
            groupTaiYuButton.BackgroundImage = groupTaiYuNormalBackground;
            groupYueYuButton.BackgroundImage = groupYueYuNormalBackground;
            groupYingWenButton.BackgroundImage = groupYingWenNormalBackground;
            groupRiYuButton.BackgroundImage = groupRiYuNormalBackground;
            groupHanYuButton.BackgroundImage = groupHanYuNormalBackground;

            activeButton.BackgroundImage = activeBackground;
        }

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


            InitializeButton(ref groupGuoYuButton, ref groupGuoYuNormalBackground, ref groupGuoYuActiveBackground, "groupGuoYuButton", 1197, 225, 225, 50, data["GroupSubBtn"]["GuoYuNormal"], data["GroupSubBtn"]["GuoYuActive"], GroupGuoYuButton_Click);

            InitializeButton(ref groupTaiYuButton, ref groupTaiYuNormalBackground, ref groupTaiYuActiveBackground, "groupTaiYuButton", 1197, 280, 225, 50, data["GroupSubBtn"]["TaiYuNormal"], data["GroupSubBtn"]["TaiYuActive"], GroupTaiYuButton_Click);

            InitializeButton(ref groupYueYuButton, ref groupYueYuNormalBackground, ref groupYueYuActiveBackground, "groupYueYuButton", 1197, 335, 225, 50, data["GroupSubBtn"]["YueYuNormal"], data["GroupSubBtn"]["YueYuActive"], GroupYueYuButton_Click);

            InitializeButton(ref groupYingWenButton, ref groupYingWenNormalBackground, ref groupYingWenActiveBackground, "groupYingWenButton", 1197, 390, 225, 50, data["GroupSubBtn"]["YingWenNormal"], data["GroupSubBtn"]["YingWenActive"], GroupYingWenButton_Click);

            InitializeButton(ref groupRiYuButton, ref groupRiYuNormalBackground, ref groupRiYuActiveBackground, "groupRiYuButton", 1197, 445, 225, 50, data["GroupSubBtn"]["RiYuNormal"], data["GroupSubBtn"]["RiYuActive"], GroupRiYuButton_Click);

            InitializeButton(ref groupHanYuButton, ref groupHanYuNormalBackground, ref groupHanYuActiveBackground, "groupHanYuButton", 1197, 500, 225, 50, data["GroupSubBtn"]["HanYuNormal"], data["GroupSubBtn"]["HanYuActive"], GroupHanYuButton_Click);

        }


        private void GroupSongSelectionButton_Click(object sender, EventArgs e)
        {
            UpdateButtonBackgrounds(groupSearchButton, groupSearchActiveBackground);

            isOnOrderedSongsPage = false;



            /*guoYuSongs = allSongs.Where(song => song.Category == "國語" && (song.ArtistACategory == "團" || song.ArtistBCategory == "團"))
                                .OrderByDescending(song => song.Plays) 
                                .ToList();*/
            /* 清空搜尋欄 */
            ResetinputBox();
            string query = "SELECT * FROM song_library_cache WHERE language_name = '國語' AND (`artistA_category` = '團' OR `artistB_category` = '團') ORDER BY song_counts DESC;";
            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);
            SetPictureBoxLanguageButtonsVisibility(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); 

            SetGroupButtonsVisibility(true);

            UpdateGroupSubBtns(groupGuoYuButton, groupGuoYuActiveBackground);

            if (pictureBoxQRCode != null)
            {
                pictureBoxQRCode.Visible = false;
                closeQRCodeButton.Visible = false;
            }
        }
        
        private void SetGroupButtonsVisibility(bool isVisible)
        {
            Button[] pictureBox6Buttons = { groupGuoYuButton, groupTaiYuButton, groupYueYuButton, groupYingWenButton, groupRiYuButton, groupHanYuButton };

            foreach (var button in pictureBox6Buttons)
            {
                button.Visible = isVisible;
                if (isVisible)
                {
                    button.BringToFront();
                }
            }
        }
    }
}
