using System.IO; 
using DBObj;
namespace DualScreenDemo
{
    public partial class PrimaryForm
    {
        private Button hotPlayButton;
        private Bitmap hotPlayNormalBackground; 
        private Bitmap hotPlayActiveBackground; 

        private Button guoYuButtonHotSong;
        private Bitmap guoYuHotSongNormalBackground;
        private Bitmap guoYuHotSongActiveBackground;
        private Button taiYuButtonHotSong;
        private Bitmap taiYuHotSongNormalBackground;
        private Bitmap taiYuHotSongActiveBackground;
        private Button taiYuNewSongButtonHotSong;
        private Bitmap taiYuNewSongHotSongNormalBackground;
        private Bitmap taiYuNewSongHotSongActiveBackground;
        private Button guoYuNewSongButtonHotSong;
        private Bitmap guoYuNewSongHotSongNormalBackground;
        private Bitmap guoYuNewSongHotSongActiveBackground;
        private Button yingWenButtonHotSong;
        private Bitmap yingWenHotSongNormalBackground;
        private Bitmap yingWenHotSongActiveBackground;
        private Button riYuButtonHotSong;
        private Bitmap riYuHotSongNormalBackground;
        private Bitmap riYuHotSongActiveBackground;
        private Button hanYuButtonHotSong;
        private Bitmap hanYuHotSongNormalBackground;
        private Bitmap hanYuHotSongActiveBackground;


        private void SetHotSongButtonsVisibility(bool isVisible)
        {
            
            

            
            Button[] hotSongButtons = { guoYuButtonHotSong, taiYuButtonHotSong, taiYuNewSongButtonHotSong, guoYuNewSongButtonHotSong, yingWenButtonHotSong, riYuButtonHotSong, hanYuButtonHotSong };

            
            foreach (var button in hotSongButtons)
            {
                button.Visible = isVisible;

                
                if (isVisible)
                {
                    button.BringToFront();
                }
            }
        }

        public void HotPlayButton_Click(object sender, EventArgs e)
        {
            UpdateButtonBackgrounds(hotPlayButton, new Bitmap(Path.Combine(serverPath, "themes\\superstar\\button\\1.主類別\\主類別上方_熱門排行(已按).png")));
            UpdateHotSongButtons(guoYuButtonHotSong, guoYuHotSongActiveBackground);
            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);
            UpdateSongList(guoYuSongs);
            
            SetButtonsVisibility();
            //HideQRCode();
        }

        private void UpdateButtonBackgrounds(Button activeButton, Image activeBackground)
        {
            newSongAlertButton.BackgroundImage = newSongAlertNormalBackground;
            hotPlayButton.BackgroundImage = hotPlayNormalBackground;
            singerSearchButton.BackgroundImage = singerSearchNormalBackground;
            songSearchButton.BackgroundImage = songSearchNormalBackground;
            languageSearchButton.BackgroundImage = languageSearchNormalBackground;
            groupSearchButton.BackgroundImage = groupSearchNormalBackground;
            categorySearchButton.BackgroundImage = categorySearchNormalBackground;
            orderedSongsButton.BackgroundImage = orderedSongsNormalBackground;
            myFavoritesButton.BackgroundImage = myFavoritesNormalBackground;
            promotionsButton.BackgroundImage = promotionsNormalBackground;
            deliciousFoodButton.BackgroundImage = deliciousFoodNormalBackground;
            activeButton.BackgroundImage = activeBackground;
        }

        private void OnHotSongButtonClick(Button activeButton, Bitmap activeBackground, string category)
        {
            UpdateHotSongButtons(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);

            UpdateSongList(selectedSongs);
        }

        private void UpdateHotSongButtons(Button activeButton, Image activeBackground)
        {
            guoYuButtonHotSong.BackgroundImage = guoYuHotSongNormalBackground;
            taiYuButtonHotSong.BackgroundImage = taiYuHotSongNormalBackground;
            taiYuNewSongButtonHotSong.BackgroundImage = taiYuNewSongHotSongNormalBackground;
            guoYuNewSongButtonHotSong.BackgroundImage = guoYuNewSongHotSongNormalBackground;
            yingWenButtonHotSong.BackgroundImage = yingWenHotSongNormalBackground;
            riYuButtonHotSong.BackgroundImage = riYuHotSongNormalBackground;
            hanYuButtonHotSong.BackgroundImage = hanYuHotSongNormalBackground;

            activeButton.BackgroundImage = activeBackground;
        }

        private void UpdateSongList(List<SongData> songs)
        {
            currentPage = 0; 
            totalPages = (int)Math.Ceiling((double)songs.Count / itemsPerPage);

            
            multiPagePanel.currentPageIndex = 0;
            multiPagePanel.LoadSongs(songs);
        }

        private void SetButtonsVisibility()
        {
            SetNewSongButtonsVisibility(false);
            SetSingerSearchButtonsVisibility(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);
            SetHotSongButtonsVisibility(true);
        }

        private void HideQRCode()
        {
            pictureBoxQRCode.Visible = false;
            closeQRCodeButton.Visible = false;
        }

        private void InitializeButtonsForHotSong()
        {   

            InitializeHotSongButton(ref guoYuNewSongButtonHotSong, "國語新歌", 1197, 225, 225, 50,
                new Bitmap(Path.Combine(serverPath, "themes\\superstar\\button\\2.次類別\\2.熱門排行_國語新歌排行(未按).png")), 
                out guoYuNewSongHotSongNormalBackground,
                new Bitmap(Path.Combine(serverPath, "themes\\superstar\\button\\2.次類別\\2.熱門排行_國語新歌排行(已按).png")), 
                out guoYuNewSongHotSongActiveBackground, 
                GuoYuNewSongButtonHotSong_Click);

            InitializeHotSongButton(ref taiYuNewSongButtonHotSong, "台語新歌", 1197, 280, 225, 50,
                new Bitmap(Path.Combine(serverPath, "themes\\superstar\\button\\2.次類別\\2.熱門排行_台語新歌排行(未按).png")), 
                out taiYuNewSongHotSongNormalBackground,
                new Bitmap(Path.Combine(serverPath, "themes\\superstar\\button\\2.次類別\\2.熱門排行_台語新歌排行(已按).png")), 
                out taiYuNewSongHotSongActiveBackground, 
                TaiYuNewSongButtonHotSong_Click);


            InitializeHotSongButton(ref taiYuButtonHotSong, "台語", 1197, 335, 225, 50,
                new Bitmap(Path.Combine(serverPath, "themes\\superstar\\button\\2.次類別\\2.熱門排行_台語人氣排行(未按).png")), 
                out taiYuHotSongNormalBackground,
                new Bitmap(Path.Combine(serverPath, "themes\\superstar\\button\\2.次類別\\2.熱門排行_台語人氣排行(已按).png")), 
                out taiYuHotSongActiveBackground, 
                TaiYuButtonHotSong_Click);


            InitializeHotSongButton(ref guoYuButtonHotSong, "國語", 1197, 390, 225, 50,
                new Bitmap(Path.Combine(serverPath, "themes\\superstar\\button\\2.次類別\\2.熱門排行_國語人氣排行(未按).png")), 
                out guoYuHotSongNormalBackground,
                new Bitmap(Path.Combine(serverPath, "themes\\superstar\\button\\2.次類別\\2.熱門排行_國語人氣排行(已按).png")), 
                out guoYuHotSongActiveBackground, 
                GuoYuButtonHotSong_Click);

            InitializeHotSongButton(ref yingWenButtonHotSong, "英文", 1197, 445, 225, 50,
                new Bitmap(Path.Combine(serverPath, "themes\\superstar\\button\\2.次類別\\2.熱門排行_英語人氣排行(未按).png")), 
                out yingWenHotSongNormalBackground,
                new Bitmap(Path.Combine(serverPath, "themes\\superstar\\button\\2.次類別\\2.熱門排行_英語人氣排行(已按).png")), 
                out yingWenHotSongActiveBackground, 
                YingWenButtonHotSong_Click);

            InitializeHotSongButton(ref riYuButtonHotSong, "日語", 1197, 500, 225, 50,
                new Bitmap(Path.Combine(serverPath, "themes\\superstar\\button\\2.次類別\\2.熱門排行_日語人氣排行(未按).png")), 
                out riYuHotSongNormalBackground,
                new Bitmap(Path.Combine(serverPath, "themes\\superstar\\button\\2.次類別\\2.熱門排行_日語人氣排行(已按).png")), 
                out riYuHotSongActiveBackground, 
                RiYuButtonHotSong_Click);

            InitializeHotSongButton(ref hanYuButtonHotSong, "韓語", 1197, 555, 225, 50,
                new Bitmap(Path.Combine(serverPath, "themes\\superstar\\button\\2.次類別\\2.熱門排行_韓語人氣排行(未按).png")), 
                out hanYuHotSongNormalBackground,
                new Bitmap(Path.Combine(serverPath, "themes\\superstar\\button\\2.次類別\\2.熱門排行_韓語人氣排行(已按).png")), 
                out hanYuHotSongActiveBackground, 
                HanYuButtonHotSong_Click);

        }

        private void InitializeHotSongButton(ref Button button, string buttonText, int x, int y, int width, int height, 
                                            Image normalBackground, out Bitmap normalBackgroundOut, 
                                            Image activeBackground, out Bitmap activeBackgroundOut, 
                                            EventHandler clickEventHandler)
        {
            button = new Button { 
                Text = "",  // 移除文字
                Visible = false 
            };
            ResizeAndPositionButton(button, x, y, width, height);

            // 修改裁剪區域，避開文字部分
            Rectangle cropArea = new Rectangle(1214, y, 209, 58);  // 使用固定的裁剪區域
            
            //normalBackgroundOut = new Bitmap(normalBackground).Clone(cropArea, normalBackground.PixelFormat);
            //activeBackgroundOut = new Bitmap(activeBackground).Clone(cropArea, activeBackground.PixelFormat);

            normalBackgroundOut = new Bitmap(normalBackground);
            activeBackgroundOut = new Bitmap(activeBackground);

            button.BackgroundImage = normalBackgroundOut;
            button.BackgroundImageLayout = ImageLayout.Stretch;
            button.FlatStyle = FlatStyle.Flat;
            button.FlatAppearance.BorderSize = 0;
            button.Click += clickEventHandler;
            this.Controls.Add(button);
        }

    }
}