using System;
using System.Drawing; 
using System.IO; 
using System.Windows.Forms; 
using NAudio.Wave; 
using WMPLib; 
using System.Collections.Generic; 

namespace DualScreenDemo
{
    public partial class PrimaryForm : Form
    {
        private WindowsMediaPlayer mediaPlayer;
        private IWavePlayer waveOut;
        private AudioFileReader audioFileReader;
        private PictureBox pictureBoxSceneSoundEffects;
        private Button constructionButton;
        private Button marketButton;
        private Button drivingButton;
        private Button airportButton;
        private Button officeButton;
        private Button closeButton;

        private void InitializeMediaPlayer()
        {
            mediaPlayer = new WindowsMediaPlayer();
        }
        private void ConfigureImageButton(Button button, int posX, int posY, int width, int height, 
                                        string imagePath, EventHandler clickEventHandler)
        {
            Bitmap image = new Bitmap(imagePath);
            button.SetBounds(posX, posY, image.Width, image.Height);

            // 載入圖片
            button.BackgroundImage = image;
            button.BackgroundImageLayout = ImageLayout.Stretch;

            // 按鈕樣式設定
            button.FlatStyle = FlatStyle.Flat;
            button.FlatAppearance.BorderSize = 0;
            button.FlatAppearance.MouseDownBackColor = Color.Transparent;
            button.FlatAppearance.MouseOverBackColor = Color.Transparent;

            // 點擊事件
            if (clickEventHandler != null)
                button.Click += clickEventHandler;

            this.Controls.Add(button);
        }

        private void InitializeSoundEffectButtons()
        {
            
            constructionButton = new Button
            {
                Name = "constructionButton",
            };
            string path = Path.Combine(Application.StartupPath, @"themes\superstar\場景音效\場景音效_工地.png");
            ConfigureImageButton(constructionButton, 1183, 634, 148, 64, 
                                        path, ConstructionButton_Click);
            this.Controls.Add(constructionButton);

            
            marketButton = new Button
            {
                Name = "marketButton",
            };
            path = Path.Combine(Application.StartupPath, @"themes\superstar\場景音效\場景音效_市場.png");
            ConfigureImageButton(marketButton, 1394, 634, 148, 63, 
                            path, MarketButton_Click);
            this.Controls.Add(marketButton);

            
            drivingButton = new Button
            {
                Name = "drivingButton",
            };
            path = Path.Combine(Application.StartupPath, @"themes\superstar\場景音效\場景音效_開車.png");
            ConfigureImageButton(drivingButton, 1183, 720, 148, 63, 
                            path, DrivingButton_Click);
            this.Controls.Add(drivingButton);

            
            airportButton = new Button
            {
                Name = "airportButton",
            };
            path = Path.Combine(Application.StartupPath, @"themes\superstar\場景音效\場景音效_機場.png");
            ConfigureImageButton(airportButton, 1394, 720, 148, 63, 
                            path, AirportButton_Click);
            this.Controls.Add(airportButton);

            
            officeButton = new Button
            {
                Name = "officeButton",
            };
            path = Path.Combine(Application.StartupPath, @"themes\superstar\場景音效\場景音效_辦公室.png");
            ConfigureImageButton(officeButton, 1183, 806, 148, 64, 
                            path, OfficeButton_Click);
            this.Controls.Add(officeButton);

            
            closeButton = new Button
            {
                Name = "closeButton",
            };
            path = Path.Combine(Application.StartupPath, @"themes\superstar\場景音效\場景音效_關閉.png");
            ConfigureImageButton(closeButton, 1394, 806, 150, 63, 
                            path, CloseButton_Click);
            this.Controls.Add(closeButton);
        }

        private void SoundEffectButton_Click(object sender, EventArgs e)
        {
            
            SetHotSongButtonsVisibility(false);
            SetNewSongButtonsVisibility(false);
            SetSingerSearchButtonsVisibility(false);
            SetSongSearchButtonsVisibility(false);

            if (!pictureBoxSceneSoundEffects.Visible)
            {
                ShowImageOnPictureBoxSceneSoundEffects(Path.Combine(Application.StartupPath, @"themes\superstar\場景音效\場景音效.png"));
                SetPictureBoxSceneSoundEffectsAndButtonsVisibility(true);
            }
            else
            {
                TogglePictureBoxSceneSoundEffectsButtonsVisibility();
            }
        }
        
        private void ConstructionButton_Click(object sender, EventArgs e) => PlaySound(@"sounds\1857.mp3");
        private void MarketButton_Click(object sender, EventArgs e) => PlaySound(@"sounds\13472_Audio Trimmer.mp3");
        private void DrivingButton_Click(object sender, EventArgs e) => PlaySound(@"sounds\kc1.mp3");
        private void AirportButton_Click(object sender, EventArgs e) => PlayMediaSound(@"sounds\xm2401.m4a");
        private void OfficeButton_Click(object sender, EventArgs e) => PlayMediaSound(@"sounds\y1640.m4a");
        private void CloseButton_Click(object sender, EventArgs e) => TogglePictureBoxSceneSoundEffectsButtonsVisibility();

        private void PlaySound(string filePath)
        {
            waveOut?.Dispose();
            audioFileReader?.Dispose();

            waveOut = new WaveOutEvent();
            audioFileReader = new AudioFileReader(Path.Combine(Application.StartupPath, filePath));
            waveOut.Init(audioFileReader);
            waveOut.Play();
        }

        private void PlayMediaSound(string filePath)
        {
            mediaPlayer.URL = Path.Combine(Application.StartupPath, filePath);
            mediaPlayer.controls.play();
        }

        public void PlayApplauseSound()
        {
            mediaPlayer.URL = Path.Combine(Application.StartupPath,"sounds" ,"zs.m4a");
            mediaPlayer.controls.play();
        }
        // 按鈕位置需要更改，底圖需要更改
        private void ShowImageOnPictureBoxSceneSoundEffects(string imagePath)
        {
            
            if (File.Exists(imagePath))
            {
                // 直接載入完整圖
                Bitmap image = new Bitmap(imagePath);

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

                // 設定 PictureBox 的大小與位置（依你的需要調整）
                ResizeAndPositionPictureBox(pictureBoxSceneSoundEffects, 850, 450, image.Width , image.Height);

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

        private void TogglePictureBoxSceneSoundEffectsButtonsVisibility()
        {
            
            bool areButtonsVisible = pictureBoxSceneSoundEffects.Visible; 

            
            SetPictureBoxSceneSoundEffectsAndButtonsVisibility(!areButtonsVisible);
        }

        
        private void SetPictureBoxSceneSoundEffectsAndButtonsVisibility(bool isVisible)
        {
            
            pictureBoxSceneSoundEffects.Visible = isVisible;

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

            
            List<Button> soundEffectButtons = new List<Button>
            {
                constructionButton,
                marketButton,
                drivingButton,
                airportButton,
                officeButton,
                closeButton
            };

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