using System.IO;
using DBObj;

namespace DualScreenDemo
{
    public partial class PrimaryForm
    {
        private Button vodButton;
        private Button insertButton;
        private Button albumButton;
        private Button favoriteButton;
        private Panel disabledPanel;
        private Button vodScreenCloseButton;

        private void InitializeButtonsForVodScreenPictureBox()
        {
            int screenWidth = 1920;
            int screenHeight = 1080;
            int pictureBoxWidth = 938;
            int pictureBoxHeight = 209;

            int xPosition = (screenWidth - pictureBoxWidth) / 2;
            int yPosition = (screenHeight - pictureBoxHeight) / 2;

            var data = LoadBtnConfigData();


            vodButton = new Button();
            vodButton.Text = "";
            ConfigureButton(vodButton, xPosition - 119, yPosition + 35, 110, 50,
                 new Bitmap(Path.Combine(serverPath, data["SongOrderPanel"]["Request"])),
                 new Bitmap(Path.Combine(serverPath, data["SongOrderPanel"]["Request"])),
                 new Bitmap(Path.Combine(serverPath, data["SongOrderPanel"]["Request"])),
                 VodButton_Click);

            vodButton.Visible = false;

            insertButton = new Button();
            insertButton.Text = "";
            ConfigureButton(insertButton, xPosition + 12, yPosition + 35, 110, 50,
                 new Bitmap(Path.Combine(serverPath, data["SongOrderPanel"]["Insert"])),
                 new Bitmap(Path.Combine(serverPath, data["SongOrderPanel"]["Insert"])),
                 new Bitmap(Path.Combine(serverPath, data["SongOrderPanel"]["Insert"])),
                 InsertButton_Click);

            insertButton.Visible = false;


            albumButton = new Button();
            albumButton.Text = "";
            ConfigureButton(albumButton, xPosition + 142, yPosition + 35, 140, 50,
                 new Bitmap(Path.Combine(serverPath, data["SongOrderPanel"]["Discography"])),
                 new Bitmap(Path.Combine(serverPath, data["SongOrderPanel"]["Discography"])),
                 new Bitmap(Path.Combine(serverPath, data["SongOrderPanel"]["Discography"])),
                 AlbumButton_Click);

            albumButton.Visible = false;


            favoriteButton = new Button();
            favoriteButton.Text = "";
            ConfigureButton(favoriteButton, xPosition + 302, yPosition + 35, 140, 50,
                 new Bitmap(Path.Combine(serverPath, data["SongOrderPanel"]["Favorites"])),
                 new Bitmap(Path.Combine(serverPath, data["SongOrderPanel"]["Favorites"])),
                 new Bitmap(Path.Combine(serverPath, data["SongOrderPanel"]["Favorites"])),
                 FavoriteButton_Click);


            disabledPanel = new Panel();
            disabledPanel.BackColor = Color.FromArgb(128, Color.Black);
            disabledPanel.Dock = DockStyle.Fill;

            disabledPanel.Visible = !isLoggedIn;


            favoriteButton.Controls.Add(disabledPanel);


            if (!isLoggedIn)
            {
                favoriteButton.Enabled = false;
                favoriteButton.BackColor = SystemColors.Control;
            }
            favoriteButton.Visible = isLoggedIn;


            vodScreenCloseButton = new Button();
            vodScreenCloseButton.Text = "";
            ConfigureButton(vodScreenCloseButton, xPosition + 462, yPosition + 35, 110, 50,
                 new Bitmap(Path.Combine(serverPath, data["SongOrderPanel"]["Exit"])),
                 new Bitmap(Path.Combine(serverPath, data["SongOrderPanel"]["Exit"])),
                 new Bitmap(Path.Combine(serverPath, data["SongOrderPanel"]["Exit"])),
                 VodScreenCloseButton_Click);


            this.Controls.Add(vodButton);
            this.Controls.Add(insertButton);
            this.Controls.Add(albumButton);
            this.Controls.Add(favoriteButton);
            this.Controls.Add(vodScreenCloseButton);
        }
        private void VodButton_Click(object sender, EventArgs e)
        {
            if (isOnOrderedSongsPage)
            {
                SetVodScreenPictureBoxAndButtonsVisibility(false);
                SongList.Cancel(currentSelectedSong);
                orderedSongsButton.PerformClick();
            }
            else
            {
                SetVodScreenPictureBoxAndButtonsVisibility(false);
                SongList.Add(currentSelectedSong);
            }

        }

        private void InsertButton_Click(object sender, EventArgs e)
        {
            SetVodScreenPictureBoxAndButtonsVisibility(false);
            SongList.Insert(currentSelectedSong);
        }

        private void AlbumButton_Click(object sender, EventArgs e)
        {
            string name = currentSelectedSong.getArtist_A(false);
            string query = $"SELECT * FROM song_library_cache WHERE artistA = '{name}' ORDER BY add_date DESC;";
            var selectedSongs = SearchSongs_Mysql(query);
            /*var selectedSongs = allSongs.Where(song => song.ArtistA == currentSelectedSong.ArtistA)
                            .OrderByDescending(song => song.AddedTime)
                            .ToList();*/

            UpdateSongList(selectedSongs);
            SetVodScreenPictureBoxAndButtonsVisibility(false);
        }

        private void FavoriteButton_Click(object sender, EventArgs e)
        {
            Console.WriteLine("Favorite Button Clicked");
            InsertNewFavoriteSong(currentSelectedSong.getNumber());
            var searchResults = SearchSongs_Mysql(SearchFavoriteSongs_Mysql());
            // 重置分頁    
            currentPage = 0;
            totalPages = (int)Math.Ceiling((double)searchResults.Count / itemsPerPage);
            // 更新多頁面面板的內容
            multiPagePanel.currentPageIndex = 0;
            multiPagePanel.LoadSongs(searchResults);

            SetVodScreenPictureBoxAndButtonsVisibility(false);
        }

        private void VodScreenCloseButton_Click(object sender, EventArgs e)
        {
            SetVodScreenPictureBoxAndButtonsVisibility(false);
        }



        private void SetVodScreenPictureBoxAndButtonsVisibility(bool isVisible)
        {
            overlayPanel.Visible = isVisible;
            VodScreenPictureBox.Visible = isVisible;
            vodButton.Visible = isVisible;
            insertButton.Visible = isVisible;
            albumButton.Visible = isVisible;
            favoriteButton.Visible = isVisible;
            vodScreenCloseButton.Visible = isVisible;
            if (isVisible)
            {
                if (isLoggedIn)
                {
                    favoriteButton.Enabled = true;
                    favoriteButton.Controls.Remove(disabledPanel);
                }
                else
                {
                    favoriteButton.Enabled = false;

                }
                overlayPanel.BringToFront();
                VodScreenPictureBox.BringToFront();
                vodButton.BringToFront();
                insertButton.BringToFront();
                albumButton.BringToFront();
                favoriteButton.BringToFront();
                vodScreenCloseButton.BringToFront();
            }
        }
    }
}