using System.IO;

namespace DualScreenDemo
{
    /* 
     * 主要視窗 (PrimaryForm) 的部分定義
     * 負責初始化搜尋按鈕並處理歌曲搜尋 UI 相關邏輯
     */
    public partial class PrimaryForm
    {
        // 各種歌曲搜尋按鈕及其對應的背景圖片 (一般狀態 / 啟動狀態)
        private Button zhuyinSearchSongButton;
        private Bitmap zhuyinSearchSongNormalBackground;
        private Bitmap zhuyinSearchSongActiveBackground;
        private Button englishSearchSongButton;
        private Bitmap englishSearchSongNormalBackground;
        private Bitmap englishSearchSongActiveBackground;
        private Button wordCountSearchSongButton;
        private Bitmap wordCountSearchSongNormalBackground;
        private Bitmap wordCountSearchSongActiveBackground;
        private Button pinyinSearchSongButton;
        private Bitmap pinyinSearchSongNormalBackground;
        private Bitmap pinyinSearchSongActiveBackground;
        private Button handWritingSearchSongButton;
        private Bitmap handWritingSearchSongNormalBackground;
        private Bitmap handWritingSearchSongActiveBackground;
        private Button numberSearchSongButton;
        private Bitmap numberSearchSongNormalBackground;
        private Bitmap numberSearchSongActiveBackground;


        private void UpdateSongSearchBtn(Button activeButton, Image activeBackground)
        {
            zhuyinSearchSongButton.BackgroundImage = zhuyinSearchSongNormalBackground;
            englishSearchSongButton.BackgroundImage = englishSearchSongNormalBackground;
            pinyinSearchSongButton.BackgroundImage = pinyinSearchSongNormalBackground;
            wordCountSearchSongButton.BackgroundImage = wordCountSearchSongNormalBackground;
            handWritingSearchSongButton.BackgroundImage = handWritingSearchSongNormalBackground;
            numberSearchSongButton.BackgroundImage = numberSearchSongNormalBackground;

            activeButton.BackgroundImage = activeBackground;
        }


        /// <summary>
        /// 點擊「歌曲搜尋」按鈕時的事件處理函式
        /// 1. 更新導航按鈕的背景圖片，使「歌曲搜尋」按鈕顯示為啟動狀態
        /// 2. 隱藏其他搜尋/分類 UI，僅顯示歌曲搜尋選單
        /// 3. 若有 QR Code 顯示，則將其隱藏
        /// </summary>
        private void SongSearchButton_Click(object sender, EventArgs e)
        {

            isOnOrderedSongsPage = false;
            UpdateButtonBackgrounds(songSearchButton, songSearchActiveBackground);
            ResetinputBox();
            string query = $"SELECT * FROM song_library_cache WHERE language_name = '國語' LIMIT 1000;";
            var searchResult = SearchSongs_Mysql(query);
            currentPage = 0;
            totalPages = (int)Math.Ceiling((double)searchResult.Count / itemsPerPage);


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

            // 隱藏其他 UI
            SetHotSongButtonsVisibility(false);
            SetNewSongButtonsVisibility(false);
            SetSongSearchButtonsVisibility(false);
            SetPictureBoxLanguageButtonsVisibility(false);
            SetGroupButtonsVisibility(false);
            SetPictureBoxCategoryAndButtonsVisibility(false);
            SetZhuYinSingersAndButtonsVisibility(false);
            SetZhuYinSongsAndButtonsVisibility(false);
            SetEnglishSingersAndButtonsVisibility(false);
            SetPinYinSingersAndButtonsVisibility(false);
            SetPinYinSongsAndButtonsVisibility(false);
            SetPictureBoxToggleLightAndButtonsVisibility(false);
            SetHandWritingForSongsAndButtonsVisibility(false);            
            SetPictureBoxSceneSoundEffectsAndButtonsVisibility(false);
            SetWordCountSongsAndButtonsVisibility(false);            
            SetSongIDSearchAndButtonsVisibility(false);  
            SetHandWritingForSingersAndButtonsVisibility(false);
            SetWordCountSingersAndButtonsVisibility(false);                      
            // 顯示歌曲搜尋選單按鈕
            SetSongSearchButtonsVisibility(true);

            // 隱藏 QR Code (若有)
            if (pictureBoxQRCode != null)
            {
                pictureBoxQRCode.Visible = false;
                closeQRCodeButton.Visible = false;
            }
        }

        /// <summary>
        /// 設定「歌曲搜尋」相關按鈕的可見性
        /// </summary>
        /// <param name="isVisible">是否顯示</param>
        private void SetSongSearchButtonsVisibility(bool isVisible)
        {
            pictureBox4.Visible = isVisible; // 控制搜尋 UI 背景的可見性

            // 歌曲搜尋的按鈕陣列
            Button[] songSearchButtons = {
                zhuyinSearchSongButton,
                englishSearchSongButton,
                wordCountSearchSongButton,
                pinyinSearchSongButton,
                handWritingSearchSongButton,
                numberSearchSongButton
            };

            // 設定按鈕可見性，若顯示則移至最前
            foreach (var button in songSearchButtons)
            {
                button.Visible = isVisible;
                if (isVisible)
                {
                    button.BringToFront();
                }
            }
        }

        /// <summary>
        /// 初始化所有「歌曲搜尋」按鈕
        /// 依據不同的搜尋方式 (注音、英文、拼音、筆劃、手寫、數字) 建立對應按鈕
        /// </summary>
        private void InitializeButtonsForSongSearch()
        {

            var data = LoadBtnConfigData();

            InitializeButton(ref zhuyinSearchSongButton, ref zhuyinSearchSongNormalBackground, ref zhuyinSearchSongActiveBackground, "zhuyinSearchSongButton", 1197, 225, 225, 50, data["SongSearch"]["ZhuYinSongNormal"], data["SongSearch"]["ZhuYinSongActive"], ZhuyinSearchSongsButton_Click);


            InitializeButton(ref englishSearchSongButton, ref englishSearchSongNormalBackground, ref englishSearchSongActiveBackground, "englishSearchSongButton", 1197, 280, 225, 50, data["SongSearch"]["EngSongNormal"], data["SongSearch"]["EngSongActive"], EnglishSearchSongsButton_Click);


            InitializeButton(ref pinyinSearchSongButton, ref pinyinSearchSongNormalBackground, ref pinyinSearchSongActiveBackground, "pinyinSearchSongButton", 1197, 335, 225, 50, data["SongSearch"]["PinYingNormal"], data["SongSearch"]["PinYingActive"], PinyinSearchSongsButton_Click);


            InitializeButton(ref wordCountSearchSongButton, ref wordCountSearchSongNormalBackground, ref wordCountSearchSongActiveBackground, "wordCountSearchSongButton", 1197, 390, 225, 50, data["SongSearch"]["WordCntSongNormal"], data["SongSearch"]["WordCntSongActive"], WordCountSearchSongsButton_Click);


            InitializeButton(ref handWritingSearchSongButton, ref handWritingSearchSongNormalBackground, ref handWritingSearchSongActiveBackground, "handWritingSearchSongButton", 1197, 445, 225, 50, data["SongSearch"]["HWritingSongNormal"], data["SongSearch"]["HWritingSongActive"], HandWritingSearchButtonForSongs_Click);


            InitializeButton(ref numberSearchSongButton, ref numberSearchSongNormalBackground, ref numberSearchSongActiveBackground, "numberSearchSongButton", 1197, 500, 225, 50, data["SongSearch"]["IDSearchNormal"], data["SongSearch"]["IDSearchActive"], SongIDSearchSongsButton_Click);

        }
    }
}
