using System.Drawing;
using System.IO;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TextBox;
namespace DualScreenDemo
{
    public partial class PrimaryForm : Form
    {

        private Button btnTurnOn;
        //private Button btnTurnOff;
        private Button btnBright;
        private Button btnRomantic;
        private Button btnAuto;
        private Button btnColorTuning;
        private Button btnSoft;
        private Button btnDynamic;
        private Button btnDeskLamp;
        private Button btnStageLight;
        private Button btnShelfLight;
        private Button btnWallLight;
        private Button btnBrightnessUp1;
        private Button btnBrightnessDown1;
        private Button btnBrightnessUp2;
        private Button btnBrightnessDown2;
        private int OnOffState;

        private PictureBox pictureBoxToggleLight;
        //private PictureBox Brightness1;
        //private PictureBox Brightness2;

        private void InitializeButtonsForPictureBoxToggleLight()
        {
            var data=LoadBtnConfigData();

            btnTurnOn = new Button { Text = "" };
            ConfigureButton(btnTurnOn, 29, 45, 250, 67,
                new Bitmap(Path.Combine(serverPath, data["LightControl"]["OnOff"])),
                new Bitmap(Path.Combine(serverPath, data["LightControl"]["OnOff"])),
                new Bitmap(Path.Combine(serverPath, data["LightControl"]["OnOff"])),
                null);


            btnTurnOn.Click += (sender, e) =>
            {
                if (OnOffState == 1) 
                {
                    SendCommandThroughSerialPort("a2 dc a4");
                    Console.WriteLine("light off");
                    OnOffState = 0;
                }
                if (SerialPortManager.mySerialPort != null && SerialPortManager.mySerialPort.IsOpen && OnOffState!=1)
                {

                    byte[] commandBytes = new byte[] { 0xA2, 0xDB, 0xA4 };
                    Console.WriteLine("light on");
                    SerialPortManager.mySerialPort.Write(commandBytes, 0, commandBytes.Length);
                    OnOffState = 1;
                }
                else
                {
                    MessageBox.Show("Serial port is not open. Cannot send track correction command.");
                }

            };


            //btnTurnOff = new Button { Text = "" };
            //ConfigureButton(btnTurnOff, 753, 411, 122, 62,
            //    resizedNormalStateImageForLightControl, resizedNormalStateImageForLightControl, resizedNormalStateImageForLightControl,
            //    (sender, e) => SendCommandThroughSerialPort("a2 dc a4"));


            btnBright = new Button{ Text = "" };
            ConfigureButton(btnBright, 295, 45, 118, 65,
                new Bitmap(Path.Combine(serverPath, data["LightControl"]["Brightness"])),
                new Bitmap(Path.Combine(serverPath, data["LightControl"]["Brightness"])),
                new Bitmap(Path.Combine(serverPath, data["LightControl"]["Brightness"])), 
                null);
            btnBright.Click += (sender, e) => 
            {
                
                if (SerialPortManager.mySerialPort != null && SerialPortManager.mySerialPort.IsOpen)
                {
                    
                    byte[] commandBytes = new byte[] { 0xA2, 0xD5, 0xA4 };
                    
                    
                    SerialPortManager.mySerialPort.Write(commandBytes, 0, commandBytes.Length);
                }
                else
                {
                    MessageBox.Show("Serial port is not open. Cannot send track correction command.");
                }
            };
            

            btnRomantic = new Button{ Text = "" };                       
            ConfigureButton(btnRomantic, 430, 45, 118, 65,
                new Bitmap(Path.Combine(serverPath, data["LightControl"]["Romantic"])),
                new Bitmap(Path.Combine(serverPath, data["LightControl"]["Romantic"])),
                new Bitmap(Path.Combine(serverPath, data["LightControl"]["Romantic"])), 
                (sender, e) => SendCommandThroughSerialPort("a2 d7 a4"));
            

            btnAuto = new Button{ Text = "" };            
            ConfigureButton(btnAuto, 430, 125, 118, 65, 
                new Bitmap(Path.Combine(serverPath, data["LightControl"]["Auto"])),
                new Bitmap(Path.Combine(serverPath, data["LightControl"]["Auto"])),
                new Bitmap(Path.Combine(serverPath, data["LightControl"]["Auto"])), 
                null);


            btnColorTuning = new Button{ Text = "" };            
            ConfigureButton(btnColorTuning, 430, 203, 118, 65,
                new Bitmap(Path.Combine(serverPath, data["LightControl"]["ColorTune"])),
                new Bitmap(Path.Combine(serverPath, data["LightControl"]["ColorTune"])),
                new Bitmap(Path.Combine(serverPath, data["LightControl"]["ColorTune"])), 
                (sender, e) => SendCommandThroughSerialPort("a2 75 a4"));

            
            btnSoft = new Button{ Text = "" };           
            ConfigureButton(btnSoft, 295, 125, 118, 65,
                new Bitmap(Path.Combine(serverPath, data["LightControl"]["Soft"])),
                new Bitmap(Path.Combine(serverPath, data["LightControl"]["Soft"])), 
                new Bitmap(Path.Combine(serverPath, data["LightControl"]["Soft"])), 
                (sender, e) => SendCommandThroughSerialPort("a2 d6 a4"));
            


            btnDynamic = new Button{ Text = "" };            
            ConfigureButton(btnDynamic, 295, 203, 118, 65, 
                new Bitmap(Path.Combine(serverPath, data["LightControl"]["Dynamic"])),
                new Bitmap(Path.Combine(serverPath, data["LightControl"]["Dynamic"])),
                new Bitmap(Path.Combine(serverPath, data["LightControl"]["Dynamic"])), 
                (sender, e) => SendCommandThroughSerialPort("a2 d8 a4"));
            

            btnDeskLamp = new Button{ Text = "" };            
            ConfigureButton(btnDeskLamp, 430, 283, 118, 65,
                new Bitmap(Path.Combine(serverPath, data["LightControl"]["Lamp"])),
                new Bitmap(Path.Combine(serverPath, data["LightControl"]["Lamp"])),
                new Bitmap(Path.Combine(serverPath, data["LightControl"]["Lamp"])), 
                (sender, e) => SendCommandThroughSerialPort("a2 fb a4"));
            

            btnStageLight = new Button{ Text = "" };
            ConfigureButton(btnStageLight, 295, 283, 118, 65,
                new Bitmap(Path.Combine(serverPath, data["LightControl"]["StageLight"])),
                new Bitmap(Path.Combine(serverPath, data["LightControl"]["StageLight"])), 
                new Bitmap(Path.Combine(serverPath, data["LightControl"]["StageLight"])), 
                (sender, e) => SendCommandThroughSerialPort("a2 fa a4"));
 
            
            btnShelfLight = new Button{ Text = "" };
            ConfigureButton(btnShelfLight, 163, 283, 118, 65,
                new Bitmap(Path.Combine(serverPath, data["LightControl"]["ShelfLight"])),
                new Bitmap(Path.Combine(serverPath, data["LightControl"]["ShelfLight"])),
                new Bitmap(Path.Combine(serverPath, data["LightControl"]["ShelfLight"])), 
                (sender, e) => SendCommandThroughSerialPort("a2 f9 a4"));
            

            btnWallLight = new Button{ Text = "" };       
            ConfigureButton(btnWallLight, 29, 283, 118, 65,
                new Bitmap(Path.Combine(serverPath, data["LightControl"]["WallLight"])),
                new Bitmap(Path.Combine(serverPath, data["LightControl"]["WallLight"])),
                new Bitmap(Path.Combine(serverPath, data["LightControl"]["WallLight"])), 
                (sender, e) => SendCommandThroughSerialPort("a2 f8 a4"));


            //Brightness1 = new PictureBox { };
            //Brightness1.BackgroundImage = new Bitmap(Path.Combine(serverPath, "themes\\superstar\\button\\3.介面\\燈光控制_桌燈調光.png"));
            //Brightness1.BackgroundImageLayout = ImageLayout.Stretch;
            //Brightness1.Size = new Size(155, 172);
            //Brightness1.Location = new Point(38, 151);


            btnBrightnessUp1 = new Button{ Text = "" };
            ConfigureTransButton(btnBrightnessUp1, 30, 125, 118, 65, null);  
            btnBrightnessUp1.MouseDown += (sender, e) => 
            {
                lightControlTimer.Tag = "a2 d9 a4"; 
                lightControlTimer.Start();
            };
            //btnBrightnessUp1.BackColor = Color.Red;

            btnBrightnessUp1.MouseUp += (sender, e) => 
            {
                lightControlTimer.Stop(); 
            };

            btnBrightnessDown1 = new Button{ Text = "" };
            ConfigureTransButton(btnBrightnessDown1, 30, 203, 118, 65, null);
            btnBrightnessDown1.MouseDown += (sender, e) => { lightControlTimer.Tag = "a2 da a4"; lightControlTimer.Start(); };
            btnBrightnessDown1.MouseUp += (sender, e) => { lightControlTimer.Stop(); };
            //btnBrightnessDown1.BackColor = Color.Red;

            //Brightness2 = new PictureBox { };
            //Brightness2.BackgroundImage = new Bitmap(Path.Combine(serverPath, "themes\\superstar\\button\\3.介面\\燈光控制_層板調光.png"));
            //Brightness2.BackgroundImageLayout = ImageLayout.Stretch;
            //Brightness2.Size = new Size(155, 172);
            //Brightness2.Location = new Point(216, 151);


            btnBrightnessUp2 = new Button{ Text = "" };
            ConfigureTransButton(btnBrightnessUp2, 163, 125, 118, 65, null);
            btnBrightnessUp2.MouseDown += (sender, e) => { lightControlTimer.Tag = "a2 f6 a4"; lightControlTimer.Start();  };
            btnBrightnessUp2.MouseUp += (sender, e) => { lightControlTimer.Stop(); };


            btnBrightnessDown2 = new Button{ Text = "" };
            ConfigureTransButton(btnBrightnessDown2, 163, 203, 118, 65, null);
            btnBrightnessDown2.MouseDown += (sender, e) => { lightControlTimer.Tag = "a2 f7 a4"; lightControlTimer.Start(); };
            btnBrightnessDown2.MouseUp += (sender, e) => { lightControlTimer.Stop(); };


            pictureBoxToggleLight.Controls.Add(btnTurnOn);
            pictureBoxToggleLight.Controls.Add(btnBright);
            pictureBoxToggleLight.Controls.Add(btnRomantic);
            pictureBoxToggleLight.Controls.Add(btnAuto);
            pictureBoxToggleLight.Controls.Add(btnColorTuning);
            pictureBoxToggleLight.Controls.Add(btnSoft);
            pictureBoxToggleLight.Controls.Add(btnDynamic);
            pictureBoxToggleLight.Controls.Add(btnDeskLamp);
            pictureBoxToggleLight.Controls.Add(btnStageLight);
            pictureBoxToggleLight.Controls.Add(btnShelfLight);
            pictureBoxToggleLight.Controls.Add(btnWallLight);
            //Brightness1.Controls.Add(btnBrightnessUp1);
            //Brightness1.Controls.Add(btnBrightnessDown1);
            //Brightness2.Controls.Add(btnBrightnessUp2);
            //Brightness2.Controls.Add(btnBrightnessDown2);
            pictureBoxToggleLight.Controls.Add(btnBrightnessUp1);
            pictureBoxToggleLight.Controls.Add(btnBrightnessDown1);
            pictureBoxToggleLight.Controls.Add(btnBrightnessUp2);
            pictureBoxToggleLight.Controls.Add(btnBrightnessDown2);
            //pictureBoxToggleLight.Controls.Add(Brightness1);
            //pictureBoxToggleLight.Controls.Add(Brightness2);
        }

        
        private void ToggleLightButton_Click(object sender, EventArgs e)
        {

            //SetHotSongButtonsVisibility(false);
            //SetNewSongButtonsVisibility(false);
            //SetSingerSearchButtonsVisibility(false);
            //SetSongSearchButtonsVisibility(false);


            if (!pictureBoxToggleLight.Visible)
            {
                var data = LoadBtnConfigData();

                pictureBoxToggleLight.Size = new Size(630, 379);
                pictureBoxToggleLight.Location = new Point(570, 359);
                ResizeAndPositionPictureBox(pictureBoxToggleLight, 570, 359, 570, 359);
                pictureBoxToggleLight.BackgroundImage = new Bitmap(Path.Combine(serverPath, data["LightControl"]["LightControlBaseUI"]));
                pictureBoxToggleLight.BackgroundImageLayout = ImageLayout.Stretch;
                pictureBoxToggleLight.BringToFront();
                SetUIVisible(pictureBoxToggleLight);
            }
            else
            {
                CloseUI(pictureBoxToggleLight);
            }


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

        private void SetPictureBoxToggleLightAndButtonsVisibility(bool isVisible)
        {

            pictureBoxToggleLight.Visible = isVisible;


            btnTurnOn.Visible = isVisible;
            //btnTurnOff.Visible = isVisible;
            btnBright.Visible = isVisible;
            btnRomantic.Visible = isVisible;
            btnAuto.Visible = isVisible;
            btnColorTuning.Visible = isVisible;
            btnSoft.Visible = isVisible;
            btnDynamic.Visible = isVisible;
            btnDeskLamp.Visible = isVisible;
            btnStageLight.Visible = isVisible;
            btnShelfLight.Visible = isVisible;
            btnWallLight.Visible = isVisible;
            btnBrightnessUp1.Visible = isVisible;
            btnBrightnessDown1.Visible = isVisible;
            btnBrightnessUp2.Visible = isVisible;
            btnBrightnessDown2.Visible = isVisible;

            if (isVisible)
            {

                pictureBoxToggleLight.BringToFront();


                btnTurnOn.BringToFront();
                //btnTurnOff.BringToFront();
                btnBright.BringToFront();
                btnRomantic.BringToFront();
                btnAuto.BringToFront();
                btnColorTuning.BringToFront();
                btnSoft.BringToFront();
                btnDynamic.BringToFront();
                btnDeskLamp.BringToFront();
                btnStageLight.BringToFront();
                btnShelfLight.BringToFront();
                btnWallLight.BringToFront();
                btnBrightnessUp1.BringToFront();
                btnBrightnessDown1.BringToFront();
                btnBrightnessUp2.BringToFront();
                btnBrightnessDown2.BringToFront();
            }
        }
    }
}