
namespace DualScreenDemo
{
    public partial class PrimaryForm
    {

        private Button zhuyinSearchButton;
        private Bitmap zhuyinSearchNormalBackground;
        private Bitmap zhuyinSearchActiveBackground;
        private Button englishSearchButton;
        private Bitmap englishSearchNormalBackground;
        private Bitmap englishSearchActiveBackground;
        private Button pinyinSearchButton;
        private Bitmap pinyinSearchNormalBackground;
        private Bitmap pinyinSearchActiveBackground;



        private Button wordCountSearchButton;
        private Bitmap wordCountSearchNormalBackground;
        private Bitmap wordCountSearchActiveBackground;
        private Button handWritingSearchButton;
        private Bitmap handWritingSearchNormalBackground;
        private Bitmap handWritingSearchActiveBackground;

        private void UpdateSSearchBtn(Button activeButton, Image activeBackground) 
        {
            zhuyinSearchButton.BackgroundImage = zhuyinSearchNormalBackground;
            englishSearchButton.BackgroundImage = englishSearchNormalBackground;
            pinyinSearchButton.BackgroundImage = pinyinSearchNormalBackground;
            wordCountSearchButton.BackgroundImage = wordCountSearchNormalBackground;
            handWritingSearchButton.BackgroundImage = handWritingSearchNormalBackground;

            activeButton.BackgroundImage = activeBackground;
        }

        private void SingerSearchButton_Click(object sender, EventArgs e)
        {

            UpdateButtonBackgrounds(singerSearchButton, singerSearchActiveBackground);

            isOnOrderedSongsPage = false;
            /* 清空搜尋欄 */
            ResetinputBox();
            string query = $"SELECT * FROM artists WHERE category = '男' LIMIT 1000 ;";
            var searchResult = SearchSingers_Mysql(query);
            currentPage = 0;
            currentArtistList = searchResult;
            totalPages = (int)Math.Ceiling((double)searchResult.Count / itemsPerPage);


            multiPagePanel.currentPageIndex = 0;
            multiPagePanel.LoadSingers(currentArtistList);

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

            SetSingerSearchButtonsVisibility(true);


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

        private void SetSingerSearchButtonsVisibility(bool isVisible)
        {
            Button[] singerSearchButtons = { zhuyinSearchButton, englishSearchButton, pinyinSearchButton, wordCountSearchButton, handWritingSearchButton };

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

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

            InitializeButton(ref zhuyinSearchButton, ref zhuyinSearchNormalBackground, ref zhuyinSearchActiveBackground, "zhuyinSearchButton", 1197, 225, 225, 50, data["SingerSearch"]["ZhuYinSingerNormal"], data["SingerSearch"]["ZhuYinSingerActive"], ZhuyinSearchSingersButton_Click);


            InitializeButton(ref englishSearchButton, ref englishSearchNormalBackground, ref englishSearchActiveBackground, "englishSearchButton", 1197, 280, 225, 50, data["SingerSearch"]["EngSingerNormal"], data["SingerSearch"]["EngSingerActive"], EnglishSearchSingersButton_Click);


            InitializeButton(ref pinyinSearchButton, ref pinyinSearchNormalBackground, ref pinyinSearchActiveBackground, "pinyinSearchButton", 1197, 335, 225, 50, data["SingerSearch"]["PinYinSingerNormal"], data["SingerSearch"]["PinYinSingerActive"], PinyinSingerSearchButton_Click);


            InitializeButton(ref wordCountSearchButton, ref wordCountSearchNormalBackground, ref wordCountSearchActiveBackground, "wordCountSearchButton", 1197, 390, 225, 50, data["SingerSearch"]["WordCntSingerNormal"], data["SingerSearch"]["WordCntSingerActive"], WordCountSearchSingersButton_Click);


            InitializeButton(ref handWritingSearchButton, ref handWritingSearchNormalBackground, ref handWritingSearchActiveBackground, "handWritingSearchButton", 1197, 445, 225, 50, data["SingerSearch"]["HWritingSingerNormal"], data["SingerSearch"]["HWritingSingerActive"], HandWritingSearchButtonForSingers_Click);

        }

        private void InitializeSearchButton(ref Button button, string name, int x, int y, int width, int height, ref Bitmap normalBackground, ref Bitmap activeBackground, Bitmap normalImage, Bitmap activeImage, EventHandler clickEventHandler)
        {
            button = new Button { Text = "", Visible = true, Name = name };
            ResizeAndPositionButton(button, x, y, width, height);
            Rectangle buttonCropArea = new Rectangle(x, y, width, height);
            //normalBackground = normalImage.Clone(buttonCropArea, normalImage.PixelFormat);
            //activeBackground = activeImage.Clone(buttonCropArea, activeImage.PixelFormat);
            button.BackgroundImage = normalBackground;
            button.BackgroundImageLayout = ImageLayout.Stretch;
            button.FlatStyle = FlatStyle.Flat;
            button.FlatAppearance.BorderSize = 0;
            button.Click += clickEventHandler;
            this.Controls.Add(button);
        }
    }
}