using System.IO;
using DBObj;
namespace DualScreenDemo
{
    public partial class PrimaryForm
    {
        private Button myFavoritesButton;
        private Bitmap myFavoritesNormalBackground;
        private Bitmap myFavoritesActiveBackground;

        private string mobileNumber = string.Empty;
        public static bool isPhoneNumberValid;
        private bool showError = false;
        private PictureBox FavoritePictureBox;
        private Button[] favoriteNumberButton;
        private Button enterFavoriteButton;
        private Button newFavoriteButton;
        private Button refillFavoriteButton;
        private Button closeFavoriteButton;
        private Label errorMessageLabel;

        private void InitializeButtonsForFavoritePictureBox()
        {
            
            int[,] coords = new int[,]
            {
                {794, 508, 70, 65}, 
                {878, 508, 70, 65}, 
                {962, 508, 70, 65}, 
                {1046, 508, 70, 65}, 
                {1130, 508, 70, 65}, 
                {794, 580, 70, 65}, 
                {878, 580, 70, 65}, 
                {962, 580, 70, 65}, 
                {1046, 580, 70, 65}, 
                {1130, 580, 70, 65}
            };

            int screenW = Screen.PrimaryScreen.Bounds.Width;
            int screenH = Screen.PrimaryScreen.Bounds.Height;

            
            float widthRatio = screenW / (float)1440;
            float heightRatio = screenH / (float)900;

            favoriteNumberButton = new Button[10];

            for (int i = 0; i < favoriteNumberButton.Length; i++)
            {
                favoriteNumberButton[i] = new Button();

                ResizeAndPositionButton(favoriteNumberButton[i], coords[i, 0], coords[i, 1], coords[i, 2], coords[i, 3]);

                
                string fileName = (i + 2).ToString("00");  
                string filePath = Path.Combine(Application.StartupPath, @"themes\superstar\我的最愛\我的最愛-" + fileName + ".jpg");
                favoriteNumberButton[i].BackgroundImage = Image.FromFile(filePath);
                favoriteNumberButton[i].BackgroundImageLayout = ImageLayout.Stretch;
                favoriteNumberButton[i].FlatStyle = FlatStyle.Flat;
                favoriteNumberButton[i].FlatAppearance.BorderSize = 0; 
                favoriteNumberButton[i].BackColor = Color.Transparent;
                favoriteNumberButton[i].FlatAppearance.MouseDownBackColor = Color.Transparent; 
                favoriteNumberButton[i].FlatAppearance.MouseOverBackColor = Color.Transparent; 

                
                favoriteNumberButton[i].Name = "favoriteNumberButton" + i;
                
                favoriteNumberButton[i].Tag = (i + 1).ToString();
                if (i == 9) 
                {
                    favoriteNumberButton[i].Name = "favoriteNumberButton0";
                    favoriteNumberButton[i].Tag = "0";
                }

                
                favoriteNumberButton[i].Click += FavoriteNumberButton_Click;

                
                this.Controls.Add(favoriteNumberButton[i]);
            }

            
            enterFavoriteButton = new Button()
            {
                Name = "enterFavoriteButton"
            };
            ResizeAndPositionButton(enterFavoriteButton, 832, 657, 70, 65);
            enterFavoriteButton.BackgroundImage = Image.FromFile(Path.Combine(Application.StartupPath, @"themes\superstar\我的最愛\我的最愛-12.jpg"));
            enterFavoriteButton.BackgroundImageLayout = ImageLayout.Stretch;
            enterFavoriteButton.FlatStyle = FlatStyle.Flat;
            enterFavoriteButton.FlatAppearance.BorderSize = 0; 
            enterFavoriteButton.BackColor = Color.Transparent;
            enterFavoriteButton.FlatAppearance.MouseDownBackColor = Color.Transparent; 
            enterFavoriteButton.FlatAppearance.MouseOverBackColor = Color.Transparent; 
            enterFavoriteButton.Click += EnterFavoriteButton_Click; 

            
            newFavoriteButton = new Button()
            {
                Name = "newFavoriteButton"
            };
            ResizeAndPositionButton(newFavoriteButton, 916, 657, 70, 65);
            newFavoriteButton.BackgroundImage = Image.FromFile(Path.Combine(Application.StartupPath, @"themes\superstar\我的最愛\我的最愛-13.jpg"));
            newFavoriteButton.BackgroundImageLayout = ImageLayout.Stretch;
            newFavoriteButton.FlatStyle = FlatStyle.Flat;
            newFavoriteButton.FlatAppearance.BorderSize = 0; 
            newFavoriteButton.BackColor = Color.Transparent;
            newFavoriteButton.FlatAppearance.MouseDownBackColor = Color.Transparent; 
            newFavoriteButton.FlatAppearance.MouseOverBackColor = Color.Transparent; 
            newFavoriteButton.Click += NewFavoriteButton_Click;

            
            refillFavoriteButton = new Button()
            {
                Name = "refillFavoriteButton"
            };
            ResizeAndPositionButton(refillFavoriteButton, 999, 657, 70, 65);
            refillFavoriteButton.BackgroundImage = Image.FromFile(Path.Combine(Application.StartupPath, @"themes\superstar\我的最愛\我的最愛-14.jpg"));
            refillFavoriteButton.BackgroundImageLayout = ImageLayout.Stretch;
            refillFavoriteButton.FlatStyle = FlatStyle.Flat;
            refillFavoriteButton.FlatAppearance.BorderSize = 0; 
            refillFavoriteButton.BackColor = Color.Transparent;
            refillFavoriteButton.FlatAppearance.MouseDownBackColor = Color.Transparent; 
            refillFavoriteButton.FlatAppearance.MouseOverBackColor = Color.Transparent; 
            refillFavoriteButton.Click += RefillFavoriteButton_Click;

            
            closeFavoriteButton = new Button()
            {
                Name = "closeFavoriteButton"
            };
            ResizeAndPositionButton(closeFavoriteButton, 1083, 657, 70, 65);
            closeFavoriteButton.BackgroundImage = Image.FromFile(Path.Combine(Application.StartupPath, @"themes\superstar\我的最愛\我的最愛-15.jpg"));
            closeFavoriteButton.BackgroundImageLayout = ImageLayout.Stretch;
            closeFavoriteButton.FlatStyle = FlatStyle.Flat;
            closeFavoriteButton.FlatAppearance.BorderSize = 0; 
            closeFavoriteButton.BackColor = Color.Transparent;
            closeFavoriteButton.FlatAppearance.MouseDownBackColor = Color.Transparent; 
            closeFavoriteButton.FlatAppearance.MouseOverBackColor = Color.Transparent; 
            closeFavoriteButton.Click += CloseFavoriteButton_Click;

            
            errorMessageLabel = new Label
            {
                Text = "",
                ForeColor = Color.Black,
                Location = new Point(10, 250),  
                AutoSize = true
            };

            
            this.Controls.Add(enterFavoriteButton);
            this.Controls.Add(newFavoriteButton);
            this.Controls.Add(refillFavoriteButton);
            this.Controls.Add(closeFavoriteButton);
            this.Controls.Add(errorMessageLabel);
        }
        
        private void FavoriteNumberButton_Click(object sender, EventArgs e)
        {
            Button clickedButton = sender as Button;
            if (clickedButton != null)
            {
                
                mobileNumber += clickedButton.Tag.ToString();
                phonenumber = mobileNumber;
                
                FavoritePictureBox.Invalidate();
            }
        }
        public static string phonenumber;
        private void FavoritePictureBox_Paint(object sender, PaintEventArgs e)
        {
            if (!string.IsNullOrEmpty(mobileNumber))
            {

                using (Brush brush = new SolidBrush(Color.Black))
                {
                    int screenW = Screen.PrimaryScreen.Bounds.Width;
                    int screenH = Screen.PrimaryScreen.Bounds.Height;

                    float widthRatio = screenW / 1920f;
                    float heightRatio = screenH / 1080f;

                    // 轉成縮放後的座標
                    int x = (int)(30 * widthRatio);
                    int y = (int)(90 * heightRatio);
                    Font font = new Font("Arial", 24*heightRatio);

                    if (showError)
                    {
                        string errorMessage;
                        if (!isPhoneNumberValid)
                        {
                            errorMessage = "查無此手機號碼!!!";
                        }
                        else
                        {
                            errorMessage = "手機號碼輸入錯誤!!!";
                        }
                        e.Graphics.DrawString(errorMessage, font, brush, x, y);
                    }
                    else
                    {
                        e.Graphics.DrawString(mobileNumber, font, brush, x, y);
                    }
                }
            }
        }
        
        
        private void EnterFavoriteButton_Click(object sender, EventArgs e)
        {
            if (mobileNumber.StartsWith("09") && mobileNumber.Length == 10)
            {
                if (checkPhoneNumberExist(mobileNumber))
                {
                    isPhoneNumberValid = true;
                    //SongListManager.Instance.UserLogin(mobileNumber);

                    var searchResults = SearchSongs_Mysql(SearchFavoriteSongs_Mysql());
                    // 重置分頁    
                    currentPage = 0;
                    totalPages = (int)Math.Ceiling((double)searchResults.Count / itemsPerPage);
                    // 更新多頁面面板的內容
                    multiPagePanel.currentPageIndex = 0;
                    multiPagePanel.LoadSongs(searchResults);

                    ToggleFavoritePictureBoxButtonsVisibility();
                }
                else
                {
                    isPhoneNumberValid = false;
                    showError = true; 
                    FavoritePictureBox.Invalidate();
                    FavoritePictureBox.Refresh(); 
                }
            }
            else
            {
                showError = true;
                isPhoneNumberValid = false;
                FavoritePictureBox.Invalidate(); 
                FavoritePictureBox.Refresh(); 
            }
        }
        
        // 我的最愛新增
        private void NewFavoriteButton_Click(object sender, EventArgs e)
        {
            if (mobileNumber.StartsWith("09") && mobileNumber.Length == 10)
            {
                if (checkPhoneNumberExist(mobileNumber))
                {
                    isPhoneNumberValid = true;
                    var searchResults = SearchSongs_Mysql(SearchFavoriteSongs_Mysql());
                    // 重置分頁    
                    currentPage = 0;
                    totalPages = (int)Math.Ceiling((double)searchResults.Count / itemsPerPage);
                    // 更新多頁面面板的內容
                    multiPagePanel.currentPageIndex = 0;
                    multiPagePanel.LoadSongs(searchResults);
                    
                    ToggleFavoritePictureBoxButtonsVisibility();
                }
                else
                {
                    isPhoneNumberValid = true;
                    InsertNewFavoriteUser(mobileNumber);
                    if (checkPhoneNumberExist(mobileNumber))
                    { 
                        var searchResults = SearchSongs_Mysql(SearchFavoriteSongs_Mysql());
                        // 重置分頁    
                        currentPage = 0;
                        totalPages = (int)Math.Ceiling((double)searchResults.Count / itemsPerPage);
                        // 更新多頁面面板的內容
                        multiPagePanel.currentPageIndex = 0;
                        multiPagePanel.LoadSongs(searchResults);
                        
                        ToggleFavoritePictureBoxButtonsVisibility();
                    }
                    else
                    {
                        showError = true; 
                        isPhoneNumberValid = false;
                        FavoritePictureBox.Invalidate(); 
                        FavoritePictureBox.Refresh();
                    }
                    
                }
            }
            else
            {
                showError = true; 
                isPhoneNumberValid = false;
                FavoritePictureBox.Invalidate(); 
                FavoritePictureBox.Refresh();
            }
        }
        
        
        private void RefillFavoriteButton_Click(object sender, EventArgs e)
        {
            
            mobileNumber = string.Empty;

            
            showError = false;

            
            FavoritePictureBox.Invalidate();
            FavoritePictureBox.Refresh();
        }
        
        
        private void CloseFavoriteButton_Click(object sender, EventArgs e)
        {
            mobileNumber = string.Empty;

            
            showError = false;

            
            FavoritePictureBox.Invalidate();
            FavoritePictureBox.Refresh();
            
            ToggleFavoritePictureBoxButtonsVisibility();
        }
        
        private void MyFavoritesButton_Click(object sender, EventArgs e)
        {
            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 = myFavoritesActiveBackground;
            promotionsButton.BackgroundImage = promotionsNormalBackground;
            deliciousFoodButton.BackgroundImage = deliciousFoodNormalBackground;
            isOnOrderedSongsPage = false;

            
            if (!FavoritePictureBox.Visible)
            {
                
                ShowImageOnFavoritePictureBox(Path.Combine(Application.StartupPath, @"themes\superstar\我的最愛\我的最愛_工作區域.jpg"));
                SetFavoritePictureBoxAndButtonsVisibility(true);
            }
            else
            {
                
                ToggleFavoritePictureBoxButtonsVisibility();
            }

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

            SetPictureBoxToggleLightAndButtonsVisibility(false);
            SetPictureBoxSceneSoundEffectsAndButtonsVisibility(false);
        }
        
        private void ShowImageOnFavoritePictureBox(string imagePath)
        {
            if (File.Exists(imagePath))
            {
                // 直接載入完整圖
                Bitmap image = new Bitmap(imagePath);

                // 顯示在 PictureBox 上
                FavoritePictureBox.Image = image;

                // 設定 PictureBox 的大小與位置（依你的需要調整）
                ResizeAndPositionPictureBox(FavoritePictureBox, 773, 380, (int)(image.Width * 0.8f) , (int)(image.Height * 0.8f));

                FavoritePictureBox.Visible = true;
            }
            else
            {
                Console.WriteLine("圖片檔案不存在：" + imagePath);
            }
        }

        private void ToggleFavoritePictureBoxButtonsVisibility()
        {
            
            bool areButtonsVisible = FavoritePictureBox.Visible; 

            
            SetFavoritePictureBoxAndButtonsVisibility(!areButtonsVisible);
        }

        private void SetFavoritePictureBoxAndButtonsVisibility(bool isVisible)
        {
            System.Action action = () =>
            {
                SuspendLayout();

                
                FavoritePictureBox.Visible = isVisible;

                
                if (isVisible) FavoritePictureBox.BringToFront();

                
                enterFavoriteButton.Visible = isVisible;
                newFavoriteButton.Visible = isVisible;
                refillFavoriteButton.Visible = isVisible;
                closeFavoriteButton.Visible = isVisible;

                
                if (isVisible)
                {
                    enterFavoriteButton.BringToFront();
                    newFavoriteButton.BringToFront();
                    refillFavoriteButton.BringToFront();
                    closeFavoriteButton.BringToFront();
                }

                
                foreach (Button button in favoriteNumberButton)
                {
                    button.Visible = isVisible;
                    
                    if (isVisible)
                        button.BringToFront();
                }

                ResumeLayout();
            };

            if (this.InvokeRequired)
            {
                this.Invoke(action);
            }
            else
            {
                action();
            }
        }
    }
}