using System.IO; 
namespace DualScreenDemo
{
    public partial class PrimaryForm : Form
    {
        public PictureBox pictureBoxQRCode;
        public Button closeQRCodeButton;

        public void OverlayQRCodeOnImage(string randomFolderPath)
        {
            try
            {
                
                string imagePath = Path.Combine(Application.StartupPath, "themes/superstar/cropped_qrcode.jpg");
                if (!File.Exists(imagePath))
                {
                    Console.WriteLine("Base image not found: " + imagePath);
                    return;
                }

                using (Image baseImage = Image.FromFile(imagePath))
                {
                    string qrImagePath = Path.Combine(Application.StartupPath, "themes/superstar/_www", randomFolderPath, "qrcode.png");
                    if (!File.Exists(qrImagePath))
                    {
                        Console.WriteLine("布局 image not found: " + qrImagePath);
                        return;
                    }

                    string tempPath = Path.GetTempFileName();
                    File.Copy(qrImagePath, tempPath, true); // 複製一份再讀，避開快取

                    Image qrCodeImage = null;
                    for (int i = 0; i < 3; i++)
                    {
                        try
                        {
                            using (var fs = new FileStream(tempPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                            {
                                qrCodeImage = Image.FromStream(fs);
                            }
                            break;
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("Error loading QR code image: " + ex.Message);
                            Thread.Sleep(100);
                        }
                    }
                    File.Delete(tempPath); // 用完記得刪除

                    if (qrCodeImage == null)
                    {
                        Console.WriteLine("Failed to load QR code image after multiple attempts.");
                        return;
                    }

                    using (qrCodeImage)
                    {
                        
                        using (Bitmap bitmap = new Bitmap(baseImage.Width, baseImage.Height))
                        {
                            using (Graphics g = Graphics.FromImage(bitmap))
                            {

                                // g.DrawImage(baseImage, 0, 0);
                                // cropped qrcode 版型不同設定調整
                                g.DrawImage(baseImage, new Rectangle(0, 0, bitmap.Width, bitmap.Height));
                                Rectangle qrCodeRect = new Rectangle(32, 39, 165, 165);
                                
                                g.DrawImage(qrCodeImage, qrCodeRect);
                            }                            
                            pictureBoxQRCode.Image = new Bitmap(bitmap);
                        }
                    }
                }

                
                ResizeAndPositionControl(pictureBoxQRCode, 975, 442, 226, 274);

                
                Bitmap originalImage = new Bitmap(Path.Combine(Application.StartupPath, "themes\\superstar\\cropped_qrcode.jpg"));

                
                Rectangle closeQRCodeCropArea = new Rectangle(198, 6, 22, 22);

                
                Bitmap closeQRCodeCroppedImage = new Bitmap(closeQRCodeCropArea.Width, closeQRCodeCropArea.Height);
                using (Graphics g = Graphics.FromImage(closeQRCodeCroppedImage))
                {
                    g.DrawImage(originalImage, new Rectangle(0, 0, closeQRCodeCropArea.Width, closeQRCodeCropArea.Height), closeQRCodeCropArea, GraphicsUnit.Pixel);
                }

                
                closeQRCodeButton = new Button { Text = "" };
                closeQRCodeButton.Name = "closeQRCodeButton";
                ResizeAndPositionButton(closeQRCodeButton, 1173, 448, 22, 22);
                closeQRCodeButton.BackgroundImage = closeQRCodeCroppedImage;
                closeQRCodeButton.BackgroundImageLayout = ImageLayout.Stretch;
                closeQRCodeButton.FlatStyle = FlatStyle.Flat;
                closeQRCodeButton.FlatAppearance.BorderSize = 0; 
                closeQRCodeButton.Click += CloseQRCodeButton_Click;
                this.Controls.Add(closeQRCodeButton);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error in OverlayQRCodeOnImage: " + ex.Message);
                if (ex.InnerException != null)
                {
                    Console.WriteLine("Inner exception: " + ex.InnerException.Message);
                }
            }
        }

        private void CloseQRCodeButton_Click(object sender, EventArgs e)
        {
            pictureBoxQRCode.Visible = false;
            closeQRCodeButton.Visible = false;
        }
    }
}