using Quasar.Common.Messages;
using Quasar.Server.Forms.DarkMode;
using Quasar.Server.Networking;
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using System.Xml.Serialization;

namespace Quasar.Server.Forms
{
    public partial class FrmAutoTasks : Form
    {
        private static readonly string AutoTasksPath = Path.Combine(Application.StartupPath, "autotasks.xml");
        public static List<AutoTaskConfig> ConfiguredTasks = new List<AutoTaskConfig>();

        // UI Controls
        private TabControl tabControl;
        private TabPage tabExecuteUrl;
        private TabPage tabExecuteDisk;
        private TabPage tabCommand;
        private TabPage tabMessageBox;
        private Button btnExecuteUrl;
        private TextBox txtUrl;
        private Label lblUrl;
        private Button btnExecuteDisk;
        private Button btnBrowseDisk;
        private TextBox txtDiskPath;
        private Label lblDiskPath;
        private Button btnExecuteCommand;
        private TextBox txtCommand;
        private Label lblCommand;
        private RadioButton rbPowerShell;
        private RadioButton rbCmd;
        private Label lblShell;
        private Button btnShowMessage;
        private GroupBox gbIcon;
        private RadioButton rbQuestion;
        private RadioButton rbError;
        private RadioButton rbWarning;
        private RadioButton rbInfo;
        private RadioButton rbNone;
        private TextBox txtMsgText;
        private Label lblMsgText;
        private TextBox txtMsgCaption;
        private Label lblMsgCaption;
        private GroupBox gbTrigger;
        private RadioButton rbInitial;
        private RadioButton rbAllConnections;
        private ListBox lstTasks;
        private Label lblTasks;
        private Button btnRemove;
        private SplitContainer splitContainer;
        private Button btnClearAll;

        public FrmAutoTasks()
        {
            InitializeComponent();
            Utilities.ThemeManager.ApplyThemeToNewForm(this);
            LoadAutoTasks();
            RefreshTaskList();
        }

        public static void LoadAutoTasks()
        {
            try
            {
                if (File.Exists(AutoTasksPath))
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(List<AutoTaskConfig>));
                    using (FileStream fs = new FileStream(AutoTasksPath, FileMode.Open))
                    {
                        ConfiguredTasks = (List<AutoTaskConfig>)serializer.Deserialize(fs);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"Failed to load auto tasks: {ex.Message}", "Load Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                ConfiguredTasks = new List<AutoTaskConfig>();
            }
        }

        public static void SaveAutoTasks()
        {
            try
            {
                XmlSerializer serializer = new XmlSerializer(typeof(List<AutoTaskConfig>));
                using (FileStream fs = new FileStream(AutoTasksPath, FileMode.Create))
                {
                    serializer.Serialize(fs, ConfiguredTasks);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"Failed to save auto tasks: {ex.Message}", "Save Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void InitializeComponent()
        {
            this.splitContainer = new System.Windows.Forms.SplitContainer();
            this.tabControl = new System.Windows.Forms.TabControl();
            this.tabExecuteUrl = new System.Windows.Forms.TabPage();
            this.gbTrigger = new System.Windows.Forms.GroupBox();
            this.rbAllConnections = new System.Windows.Forms.RadioButton();
            this.rbInitial = new System.Windows.Forms.RadioButton();
            this.btnExecuteUrl = new System.Windows.Forms.Button();
            this.txtUrl = new System.Windows.Forms.TextBox();
            this.lblUrl = new System.Windows.Forms.Label();
            this.tabExecuteDisk = new System.Windows.Forms.TabPage();
            this.btnExecuteDisk = new System.Windows.Forms.Button();
            this.btnBrowseDisk = new System.Windows.Forms.Button();
            this.txtDiskPath = new System.Windows.Forms.TextBox();
            this.lblDiskPath = new System.Windows.Forms.Label();
            this.tabCommand = new System.Windows.Forms.TabPage();
            this.btnExecuteCommand = new System.Windows.Forms.Button();
            this.txtCommand = new System.Windows.Forms.TextBox();
            this.lblCommand = new System.Windows.Forms.Label();
            this.rbPowerShell = new System.Windows.Forms.RadioButton();
            this.rbCmd = new System.Windows.Forms.RadioButton();
            this.lblShell = new System.Windows.Forms.Label();
            this.tabMessageBox = new System.Windows.Forms.TabPage();
            this.btnShowMessage = new System.Windows.Forms.Button();
            this.gbIcon = new System.Windows.Forms.GroupBox();
            this.rbQuestion = new System.Windows.Forms.RadioButton();
            this.rbError = new System.Windows.Forms.RadioButton();
            this.rbWarning = new System.Windows.Forms.RadioButton();
            this.rbInfo = new System.Windows.Forms.RadioButton();
            this.rbNone = new System.Windows.Forms.RadioButton();
            this.txtMsgText = new System.Windows.Forms.TextBox();
            this.lblMsgText = new System.Windows.Forms.Label();
            this.txtMsgCaption = new System.Windows.Forms.TextBox();
            this.lblMsgCaption = new System.Windows.Forms.Label();
            this.btnClearAll = new System.Windows.Forms.Button();
            this.btnRemove = new System.Windows.Forms.Button();
            this.lstTasks = new System.Windows.Forms.ListBox();
            this.lblTasks = new System.Windows.Forms.Label();
            ((System.ComponentModel.ISupportInitialize)(this.splitContainer)).BeginInit();
            this.splitContainer.Panel1.SuspendLayout();
            this.splitContainer.Panel2.SuspendLayout();
            this.splitContainer.SuspendLayout();
            this.tabControl.SuspendLayout();
            this.tabExecuteUrl.SuspendLayout();
            this.gbTrigger.SuspendLayout();
            this.tabExecuteDisk.SuspendLayout();
            this.tabCommand.SuspendLayout();
            this.tabMessageBox.SuspendLayout();
            this.gbIcon.SuspendLayout();
            this.SuspendLayout();
            // 
            // splitContainer
            // 
            this.splitContainer.Dock = System.Windows.Forms.DockStyle.Fill;
            this.splitContainer.Location = new System.Drawing.Point(0, 0);
            this.splitContainer.Margin = new System.Windows.Forms.Padding(4);
            this.splitContainer.Name = "splitContainer";
            // 
            // splitContainer.Panel1
            // 
            this.splitContainer.Panel1.Controls.Add(this.tabControl);
            // 
            // splitContainer.Panel2
            // 
            this.splitContainer.Panel2.Controls.Add(this.btnClearAll);
            this.splitContainer.Panel2.Controls.Add(this.btnRemove);
            this.splitContainer.Panel2.Controls.Add(this.lstTasks);
            this.splitContainer.Panel2.Controls.Add(this.lblTasks);
            this.splitContainer.Panel2.ForeColor = System.Drawing.Color.White;
            this.splitContainer.Size = new System.Drawing.Size(1200, 558);
            this.splitContainer.SplitterDistance = 666;
            this.splitContainer.SplitterWidth = 5;
            this.splitContainer.TabIndex = 0;
            // 
            // tabControl
            // 
            this.tabControl.Controls.Add(this.tabExecuteUrl);
            this.tabControl.Controls.Add(this.tabExecuteDisk);
            this.tabControl.Controls.Add(this.tabCommand);
            this.tabControl.Controls.Add(this.tabMessageBox);
            this.tabControl.Dock = System.Windows.Forms.DockStyle.Fill;
            this.tabControl.Location = new System.Drawing.Point(0, 0);
            this.tabControl.Margin = new System.Windows.Forms.Padding(4);
            this.tabControl.Name = "tabControl";
            this.tabControl.SelectedIndex = 0;
            this.tabControl.Size = new System.Drawing.Size(666, 558);
            this.tabControl.TabIndex = 0;
            // 
            // tabExecuteUrl
            // 
            this.tabExecuteUrl.Controls.Add(this.gbTrigger);
            this.tabExecuteUrl.Controls.Add(this.btnExecuteUrl);
            this.tabExecuteUrl.Controls.Add(this.txtUrl);
            this.tabExecuteUrl.Controls.Add(this.lblUrl);
            this.tabExecuteUrl.ForeColor = System.Drawing.Color.White;
            this.tabExecuteUrl.Location = new System.Drawing.Point(4, 25);
            this.tabExecuteUrl.Margin = new System.Windows.Forms.Padding(4);
            this.tabExecuteUrl.Name = "tabExecuteUrl";
            this.tabExecuteUrl.Padding = new System.Windows.Forms.Padding(4);
            this.tabExecuteUrl.Size = new System.Drawing.Size(658, 529);
            this.tabExecuteUrl.TabIndex = 0;
            this.tabExecuteUrl.Text = "Execute from URL";
            this.tabExecuteUrl.UseVisualStyleBackColor = true;
            // 
            // gbTrigger
            // 
            this.gbTrigger.Controls.Add(this.rbAllConnections);
            this.gbTrigger.Controls.Add(this.rbInitial);
            this.gbTrigger.Location = new System.Drawing.Point(20, 98);
            this.gbTrigger.Margin = new System.Windows.Forms.Padding(4);
            this.gbTrigger.Name = "gbTrigger";
            this.gbTrigger.Padding = new System.Windows.Forms.Padding(4);
            this.gbTrigger.Size = new System.Drawing.Size(560, 74);
            this.gbTrigger.TabIndex = 3;
            this.gbTrigger.TabStop = false;
            this.gbTrigger.Text = "Trigger";
            // 
            // rbAllConnections
            // 
            this.rbAllConnections.AutoSize = true;
            this.rbAllConnections.Location = new System.Drawing.Point(200, 31);
            this.rbAllConnections.Margin = new System.Windows.Forms.Padding(4);
            this.rbAllConnections.Name = "rbAllConnections";
            this.rbAllConnections.Size = new System.Drawing.Size(120, 20);
            this.rbAllConnections.TabIndex = 1;
            this.rbAllConnections.Text = "All Connections";
            this.rbAllConnections.UseVisualStyleBackColor = true;
            // 
            // rbInitial
            // 
            this.rbInitial.AutoSize = true;
            this.rbInitial.Checked = true;
            this.rbInitial.Location = new System.Drawing.Point(20, 31);
            this.rbInitial.Margin = new System.Windows.Forms.Padding(4);
            this.rbInitial.Name = "rbInitial";
            this.rbInitial.Size = new System.Drawing.Size(128, 20);
            this.rbInitial.TabIndex = 0;
            this.rbInitial.TabStop = true;
            this.rbInitial.Text = "Initial Connection";
            this.rbInitial.UseVisualStyleBackColor = true;
            // 
            // btnExecuteUrl
            // 
            this.btnExecuteUrl.Location = new System.Drawing.Point(20, 197);
            this.btnExecuteUrl.Margin = new System.Windows.Forms.Padding(4);
            this.btnExecuteUrl.Name = "btnExecuteUrl";
            this.btnExecuteUrl.Size = new System.Drawing.Size(160, 37);
            this.btnExecuteUrl.TabIndex = 2;
            this.btnExecuteUrl.Text = "Add Task";
            this.btnExecuteUrl.UseVisualStyleBackColor = true;
            this.btnExecuteUrl.Click += new System.EventHandler(this.btnExecuteUrl_Click);
            // 
            // txtUrl
            // 
            this.txtUrl.Location = new System.Drawing.Point(20, 49);
            this.txtUrl.Margin = new System.Windows.Forms.Padding(4);
            this.txtUrl.Name = "txtUrl";
            this.txtUrl.Size = new System.Drawing.Size(559, 22);
            this.txtUrl.TabIndex = 1;
            // 
            // lblUrl
            // 
            this.lblUrl.AutoSize = true;
            this.lblUrl.Location = new System.Drawing.Point(16, 25);
            this.lblUrl.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
            this.lblUrl.Name = "lblUrl";
            this.lblUrl.Size = new System.Drawing.Size(37, 16);
            this.lblUrl.TabIndex = 0;
            this.lblUrl.Text = "URL:";
            // 
            // tabExecuteDisk
            // 
            this.tabExecuteDisk.Controls.Add(this.btnExecuteDisk);
            this.tabExecuteDisk.Controls.Add(this.btnBrowseDisk);
            this.tabExecuteDisk.Controls.Add(this.txtDiskPath);
            this.tabExecuteDisk.Controls.Add(this.lblDiskPath);
            this.tabExecuteDisk.ForeColor = System.Drawing.Color.White;
            this.tabExecuteDisk.Location = new System.Drawing.Point(4, 25);
            this.tabExecuteDisk.Margin = new System.Windows.Forms.Padding(4);
            this.tabExecuteDisk.Name = "tabExecuteDisk";
            this.tabExecuteDisk.Padding = new System.Windows.Forms.Padding(4);
            this.tabExecuteDisk.Size = new System.Drawing.Size(658, 529);
            this.tabExecuteDisk.TabIndex = 1;
            this.tabExecuteDisk.Text = "Execute from Disk";
            this.tabExecuteDisk.UseVisualStyleBackColor = true;
            // 
            // btnExecuteDisk
            // 
            this.btnExecuteDisk.Location = new System.Drawing.Point(20, 98);
            this.btnExecuteDisk.Margin = new System.Windows.Forms.Padding(4);
            this.btnExecuteDisk.Name = "btnExecuteDisk";
            this.btnExecuteDisk.Size = new System.Drawing.Size(160, 37);
            this.btnExecuteDisk.TabIndex = 3;
            this.btnExecuteDisk.Text = "Add Task";
            this.btnExecuteDisk.UseVisualStyleBackColor = true;
            this.btnExecuteDisk.Click += new System.EventHandler(this.btnExecuteDisk_Click);
            // 
            // btnBrowseDisk
            // 
            this.btnBrowseDisk.Location = new System.Drawing.Point(455, 47);
            this.btnBrowseDisk.Margin = new System.Windows.Forms.Padding(4);
            this.btnBrowseDisk.Name = "btnBrowseDisk";
            this.btnBrowseDisk.Size = new System.Drawing.Size(125, 32);
            this.btnBrowseDisk.TabIndex = 2;
            this.btnBrowseDisk.Text = "Browse...";
            this.btnBrowseDisk.UseVisualStyleBackColor = true;
            this.btnBrowseDisk.Click += new System.EventHandler(this.btnBrowseDisk_Click);
            // 
            // txtDiskPath
            // 
            this.txtDiskPath.Location = new System.Drawing.Point(20, 49);
            this.txtDiskPath.Margin = new System.Windows.Forms.Padding(4);
            this.txtDiskPath.Name = "txtDiskPath";
            this.txtDiskPath.ReadOnly = true;
            this.txtDiskPath.Size = new System.Drawing.Size(425, 22);
            this.txtDiskPath.TabIndex = 1;
            // 
            // lblDiskPath
            // 
            this.lblDiskPath.AutoSize = true;
            this.lblDiskPath.Location = new System.Drawing.Point(16, 25);
            this.lblDiskPath.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
            this.lblDiskPath.Name = "lblDiskPath";
            this.lblDiskPath.Size = new System.Drawing.Size(143, 16);
            this.lblDiskPath.TabIndex = 0;
            this.lblDiskPath.Text = "File Path (on client PC):";
            // 
            // tabCommand
            // 
            this.tabCommand.Controls.Add(this.btnExecuteCommand);
            this.tabCommand.Controls.Add(this.txtCommand);
            this.tabCommand.Controls.Add(this.lblCommand);
            this.tabCommand.Controls.Add(this.rbPowerShell);
            this.tabCommand.Controls.Add(this.rbCmd);
            this.tabCommand.Controls.Add(this.lblShell);
            this.tabCommand.ForeColor = System.Drawing.Color.White;
            this.tabCommand.Location = new System.Drawing.Point(4, 25);
            this.tabCommand.Margin = new System.Windows.Forms.Padding(4);
            this.tabCommand.Name = "tabCommand";
            this.tabCommand.Size = new System.Drawing.Size(658, 529);
            this.tabCommand.TabIndex = 2;
            this.tabCommand.Text = "Command";
            this.tabCommand.UseVisualStyleBackColor = true;
            // 
            // btnExecuteCommand
            // 
            this.btnExecuteCommand.Location = new System.Drawing.Point(20, 283);
            this.btnExecuteCommand.Margin = new System.Windows.Forms.Padding(4);
            this.btnExecuteCommand.Name = "btnExecuteCommand";
            this.btnExecuteCommand.Size = new System.Drawing.Size(160, 37);
            this.btnExecuteCommand.TabIndex = 5;
            this.btnExecuteCommand.Text = "Add Task";
            this.btnExecuteCommand.UseVisualStyleBackColor = true;
            this.btnExecuteCommand.Click += new System.EventHandler(this.btnExecuteCommand_Click);
            // 
            // txtCommand
            // 
            this.txtCommand.Location = new System.Drawing.Point(20, 111);
            this.txtCommand.Margin = new System.Windows.Forms.Padding(4);
            this.txtCommand.Multiline = true;
            this.txtCommand.Name = "txtCommand";
            this.txtCommand.Size = new System.Drawing.Size(559, 159);
            this.txtCommand.TabIndex = 4;
            // 
            // lblCommand
            // 
            this.lblCommand.AutoSize = true;
            this.lblCommand.Location = new System.Drawing.Point(16, 86);
            this.lblCommand.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
            this.lblCommand.Name = "lblCommand";
            this.lblCommand.Size = new System.Drawing.Size(72, 16);
            this.lblCommand.TabIndex = 3;
            this.lblCommand.Text = "Command:";
            // 
            // rbPowerShell
            // 
            this.rbPowerShell.AutoSize = true;
            this.rbPowerShell.Location = new System.Drawing.Point(120, 49);
            this.rbPowerShell.Margin = new System.Windows.Forms.Padding(4);
            this.rbPowerShell.Name = "rbPowerShell";
            this.rbPowerShell.Size = new System.Drawing.Size(96, 20);
            this.rbPowerShell.TabIndex = 2;
            this.rbPowerShell.Text = "PowerShell";
            this.rbPowerShell.UseVisualStyleBackColor = true;
            // 
            // rbCmd
            // 
            this.rbCmd.AutoSize = true;
            this.rbCmd.Checked = true;
            this.rbCmd.Location = new System.Drawing.Point(20, 49);
            this.rbCmd.Margin = new System.Windows.Forms.Padding(4);
            this.rbCmd.Name = "rbCmd";
            this.rbCmd.Size = new System.Drawing.Size(58, 20);
            this.rbCmd.TabIndex = 1;
            this.rbCmd.TabStop = true;
            this.rbCmd.Text = "CMD";
            this.rbCmd.UseVisualStyleBackColor = true;
            // 
            // lblShell
            // 
            this.lblShell.AutoSize = true;
            this.lblShell.Location = new System.Drawing.Point(16, 25);
            this.lblShell.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
            this.lblShell.Name = "lblShell";
            this.lblShell.Size = new System.Drawing.Size(40, 16);
            this.lblShell.TabIndex = 0;
            this.lblShell.Text = "Shell:";
            // 
            // tabMessageBox
            // 
            this.tabMessageBox.Controls.Add(this.btnShowMessage);
            this.tabMessageBox.Controls.Add(this.gbIcon);
            this.tabMessageBox.Controls.Add(this.txtMsgText);
            this.tabMessageBox.Controls.Add(this.lblMsgText);
            this.tabMessageBox.Controls.Add(this.txtMsgCaption);
            this.tabMessageBox.Controls.Add(this.lblMsgCaption);
            this.tabMessageBox.ForeColor = System.Drawing.Color.White;
            this.tabMessageBox.Location = new System.Drawing.Point(4, 25);
            this.tabMessageBox.Margin = new System.Windows.Forms.Padding(4);
            this.tabMessageBox.Name = "tabMessageBox";
            this.tabMessageBox.Size = new System.Drawing.Size(658, 529);
            this.tabMessageBox.TabIndex = 3;
            this.tabMessageBox.Text = "Message Box";
            this.tabMessageBox.UseVisualStyleBackColor = true;
            // 
            // btnShowMessage
            // 
            this.btnShowMessage.Location = new System.Drawing.Point(20, 283);
            this.btnShowMessage.Margin = new System.Windows.Forms.Padding(4);
            this.btnShowMessage.Name = "btnShowMessage";
            this.btnShowMessage.Size = new System.Drawing.Size(160, 37);
            this.btnShowMessage.TabIndex = 5;
            this.btnShowMessage.Text = "Add Task";
            this.btnShowMessage.UseVisualStyleBackColor = true;
            this.btnShowMessage.Click += new System.EventHandler(this.btnShowMessage_Click);
            // 
            // gbIcon
            // 
            this.gbIcon.Controls.Add(this.rbQuestion);
            this.gbIcon.Controls.Add(this.rbError);
            this.gbIcon.Controls.Add(this.rbWarning);
            this.gbIcon.Controls.Add(this.rbInfo);
            this.gbIcon.Controls.Add(this.rbNone);
            this.gbIcon.Location = new System.Drawing.Point(333, 25);
            this.gbIcon.Margin = new System.Windows.Forms.Padding(4);
            this.gbIcon.Name = "gbIcon";
            this.gbIcon.Padding = new System.Windows.Forms.Padding(4);
            this.gbIcon.Size = new System.Drawing.Size(247, 246);
            this.gbIcon.TabIndex = 4;
            this.gbIcon.TabStop = false;
            this.gbIcon.Text = "Icon";
            // 
            // rbQuestion
            // 
            this.rbQuestion.AutoSize = true;
            this.rbQuestion.Location = new System.Drawing.Point(20, 142);
            this.rbQuestion.Margin = new System.Windows.Forms.Padding(4);
            this.rbQuestion.Name = "rbQuestion";
            this.rbQuestion.Size = new System.Drawing.Size(81, 20);
            this.rbQuestion.TabIndex = 4;
            this.rbQuestion.Text = "Question";
            this.rbQuestion.UseVisualStyleBackColor = true;
            // 
            // rbError
            // 
            this.rbError.AutoSize = true;
            this.rbError.Location = new System.Drawing.Point(20, 113);
            this.rbError.Margin = new System.Windows.Forms.Padding(4);
            this.rbError.Name = "rbError";
            this.rbError.Size = new System.Drawing.Size(57, 20);
            this.rbError.TabIndex = 3;
            this.rbError.Text = "Error";
            this.rbError.UseVisualStyleBackColor = true;
            // 
            // rbWarning
            // 
            this.rbWarning.AutoSize = true;
            this.rbWarning.Location = new System.Drawing.Point(20, 85);
            this.rbWarning.Margin = new System.Windows.Forms.Padding(4);
            this.rbWarning.Name = "rbWarning";
            this.rbWarning.Size = new System.Drawing.Size(78, 20);
            this.rbWarning.TabIndex = 2;
            this.rbWarning.Text = "Warning";
            this.rbWarning.UseVisualStyleBackColor = true;
            // 
            // rbInfo
            // 
            this.rbInfo.AutoSize = true;
            this.rbInfo.Location = new System.Drawing.Point(20, 57);
            this.rbInfo.Margin = new System.Windows.Forms.Padding(4);
            this.rbInfo.Name = "rbInfo";
            this.rbInfo.Size = new System.Drawing.Size(93, 20);
            this.rbInfo.TabIndex = 1;
            this.rbInfo.Text = "Information";
            this.rbInfo.UseVisualStyleBackColor = true;
            // 
            // rbNone
            // 
            this.rbNone.AutoSize = true;
            this.rbNone.Checked = true;
            this.rbNone.Location = new System.Drawing.Point(20, 28);
            this.rbNone.Margin = new System.Windows.Forms.Padding(4);
            this.rbNone.Name = "rbNone";
            this.rbNone.Size = new System.Drawing.Size(61, 20);
            this.rbNone.TabIndex = 0;
            this.rbNone.TabStop = true;
            this.rbNone.Text = "None";
            this.rbNone.UseVisualStyleBackColor = true;
            // 
            // txtMsgText
            // 
            this.txtMsgText.Location = new System.Drawing.Point(20, 111);
            this.txtMsgText.Margin = new System.Windows.Forms.Padding(4);
            this.txtMsgText.Multiline = true;
            this.txtMsgText.Name = "txtMsgText";
            this.txtMsgText.Size = new System.Drawing.Size(292, 159);
            this.txtMsgText.TabIndex = 3;
            // 
            // lblMsgText
            // 
            this.lblMsgText.AutoSize = true;
            this.lblMsgText.Location = new System.Drawing.Point(16, 86);
            this.lblMsgText.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
            this.lblMsgText.Name = "lblMsgText";
            this.lblMsgText.Size = new System.Drawing.Size(36, 16);
            this.lblMsgText.TabIndex = 2;
            this.lblMsgText.Text = "Text:";
            // 
            // txtMsgCaption
            // 
            this.txtMsgCaption.Location = new System.Drawing.Point(20, 49);
            this.txtMsgCaption.Margin = new System.Windows.Forms.Padding(4);
            this.txtMsgCaption.Name = "txtMsgCaption";
            this.txtMsgCaption.Size = new System.Drawing.Size(292, 22);
            this.txtMsgCaption.TabIndex = 1;
            // 
            // lblMsgCaption
            // 
            this.lblMsgCaption.AutoSize = true;
            this.lblMsgCaption.Location = new System.Drawing.Point(16, 25);
            this.lblMsgCaption.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
            this.lblMsgCaption.Name = "lblMsgCaption";
            this.lblMsgCaption.Size = new System.Drawing.Size(56, 16);
            this.lblMsgCaption.TabIndex = 0;
            this.lblMsgCaption.Text = "Caption:";
            // 
            // btnClearAll
            // 
            this.btnClearAll.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.btnClearAll.Location = new System.Drawing.Point(194, 222);
            this.btnClearAll.Margin = new System.Windows.Forms.Padding(4);
            this.btnClearAll.Name = "btnClearAll";
            this.btnClearAll.Size = new System.Drawing.Size(160, 37);
            this.btnClearAll.TabIndex = 3;
            this.btnClearAll.Text = "Clear All";
            this.btnClearAll.UseVisualStyleBackColor = true;
            this.btnClearAll.Click += new System.EventHandler(this.btnClearAll_Click);
            // 
            // btnRemove
            // 
            this.btnRemove.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.btnRemove.Location = new System.Drawing.Point(17, 222);
            this.btnRemove.Margin = new System.Windows.Forms.Padding(4);
            this.btnRemove.Name = "btnRemove";
            this.btnRemove.Size = new System.Drawing.Size(160, 37);
            this.btnRemove.TabIndex = 2;
            this.btnRemove.Text = "Remove Selected";
            this.btnRemove.UseVisualStyleBackColor = true;
            this.btnRemove.Click += new System.EventHandler(this.btnRemove_Click);
            // 
            // lstTasks
            // 
            this.lstTasks.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
            | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.lstTasks.FormattingEnabled = true;
            this.lstTasks.ItemHeight = 16;
            this.lstTasks.Location = new System.Drawing.Point(13, 37);
            this.lstTasks.Margin = new System.Windows.Forms.Padding(4);
            this.lstTasks.Name = "lstTasks";
            this.lstTasks.Size = new System.Drawing.Size(492, 180);
            this.lstTasks.TabIndex = 0;
            // 
            // lblTasks
            // 
            this.lblTasks.AutoSize = true;
            this.lblTasks.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Bold);
            this.lblTasks.Location = new System.Drawing.Point(13, 12);
            this.lblTasks.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
            this.lblTasks.Name = "lblTasks";
            this.lblTasks.Size = new System.Drawing.Size(133, 20);
            this.lblTasks.TabIndex = 1;
            this.lblTasks.Text = "Configured Tasks:";
            // 
            // FrmAutoTasks
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(1200, 558);
            this.Controls.Add(this.splitContainer);
            this.Margin = new System.Windows.Forms.Padding(4);
            this.MinimumSize = new System.Drawing.Size(1061, 605);
            this.Name = "FrmAutoTasks";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
            this.Text = "Auto Tasks";
            this.splitContainer.Panel1.ResumeLayout(false);
            this.splitContainer.Panel2.ResumeLayout(false);
            this.splitContainer.Panel2.PerformLayout();
            ((System.ComponentModel.ISupportInitialize)(this.splitContainer)).EndInit();
            this.splitContainer.ResumeLayout(false);
            this.tabControl.ResumeLayout(false);
            this.tabExecuteUrl.ResumeLayout(false);
            this.tabExecuteUrl.PerformLayout();
            this.gbTrigger.ResumeLayout(false);
            this.gbTrigger.PerformLayout();
            this.tabExecuteDisk.ResumeLayout(false);
            this.tabExecuteDisk.PerformLayout();
            this.tabCommand.ResumeLayout(false);
            this.tabCommand.PerformLayout();
            this.tabMessageBox.ResumeLayout(false);
            this.tabMessageBox.PerformLayout();
            this.gbIcon.ResumeLayout(false);
            this.gbIcon.PerformLayout();
            this.ResumeLayout(false);

        }


        private string GetTrigger()
        {
            if (rbInitial.Checked) return "Initial";
            if (rbAllConnections.Checked) return "All";
            return "Initial";
        }

        private void RefreshTaskList()
        {
            lstTasks.Items.Clear();
            foreach (var task in ConfiguredTasks)
            {
                string display = $"[{task.Trigger}] {task.TaskType}";
                if (!string.IsNullOrEmpty(task.Param1))
                    display += $" - {task.Param1}";
                lstTasks.Items.Add(display);
            }
        }

        private void btnExecuteUrl_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(txtUrl.Text))
            {
                MessageBox.Show("Please enter a URL.", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            ConfiguredTasks.Add(new AutoTaskConfig
            {
                Trigger = GetTrigger(),
                TaskType = "EXECUTE_URL",
                Param1 = txtUrl.Text
            });

            SaveAutoTasks();
            MessageBox.Show("Task added successfully!", "Auto Tasks", MessageBoxButtons.OK, MessageBoxIcon.Information);
            txtUrl.Clear();
            RefreshTaskList();
        }

        private void btnBrowseDisk_Click(object sender, EventArgs e)
        {
            using (var openFileDialog = new OpenFileDialog())
            {
                openFileDialog.Title = "Select File to Execute";
                openFileDialog.Filter = "Executable Files (*.exe)|*.exe|All Files (*.*)|*.*";
                
                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    txtDiskPath.Text = openFileDialog.FileName;
                }
            }
        }

        private void btnExecuteDisk_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(txtDiskPath.Text))
            {
                MessageBox.Show("Please select a file path.", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            ConfiguredTasks.Add(new AutoTaskConfig
            {
                Trigger = "Initial",
                TaskType = "EXECUTE_DISK",
                Param1 = txtDiskPath.Text
            });

            SaveAutoTasks();
            MessageBox.Show("Task added successfully!", "Auto Tasks", MessageBoxButtons.OK, MessageBoxIcon.Information);
            txtDiskPath.Clear();
            RefreshTaskList();
        }


        private void btnExecuteCommand_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(txtCommand.Text))
            {
                MessageBox.Show("Please enter a command.", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            string shell = rbCmd.Checked ? "cmd" : "powershell";

            ConfiguredTasks.Add(new AutoTaskConfig
            {
                Trigger = "Initial",
                TaskType = "COMMAND",
                Param1 = shell,
                Param2 = txtCommand.Text
            });

            SaveAutoTasks();
            MessageBox.Show("Task added successfully!", "Auto Tasks", MessageBoxButtons.OK, MessageBoxIcon.Information);
            txtCommand.Clear();
            RefreshTaskList();
        }

        private void btnShowMessage_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(txtMsgCaption.Text) || string.IsNullOrWhiteSpace(txtMsgText.Text))
            {
                MessageBox.Show("Please enter both caption and message text.", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            string icon = "None";
            if (rbInfo.Checked) icon = "Information";
            else if (rbWarning.Checked) icon = "Warning";
            else if (rbError.Checked) icon = "Error";
            else if (rbQuestion.Checked) icon = "Question";

            ConfiguredTasks.Add(new AutoTaskConfig
            {
                Trigger = "Initial",
                TaskType = "MESSAGEBOX",
                Param1 = txtMsgCaption.Text,
                Param2 = txtMsgText.Text,
                Param3 = icon
            });

            SaveAutoTasks();
            MessageBox.Show("Task added successfully!", "Auto Tasks", MessageBoxButtons.OK, MessageBoxIcon.Information);
            txtMsgCaption.Clear();
            txtMsgText.Clear();
            RefreshTaskList();
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            if (lstTasks.SelectedIndex >= 0)
            {
                ConfiguredTasks.RemoveAt(lstTasks.SelectedIndex);
                SaveAutoTasks();
                RefreshTaskList();
                MessageBox.Show("Task removed successfully!", "Auto Tasks", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("Please select a task to remove.", "No Selection", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void btnClearAll_Click(object sender, EventArgs e)
        {
            if (ConfiguredTasks.Count == 0)
            {
                MessageBox.Show("No tasks to clear.", "Auto Tasks", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            var result = MessageBox.Show($"Are you sure you want to remove all {ConfiguredTasks.Count} task(s)?", 
                "Confirm Clear All", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            if (result == DialogResult.Yes)
            {
                ConfiguredTasks.Clear();
                SaveAutoTasks();
                RefreshTaskList();
                MessageBox.Show("All tasks cleared!", "Auto Tasks", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            // Don't close if embedded in a tab (TopLevel = false)
            if (this.TopLevel)
                this.Close();
        }
    }

    [Serializable]
    public class AutoTaskConfig
    {
        public string Trigger { get; set; }
        public string TaskType { get; set; }
        public string Param1 { get; set; }
        public string Param2 { get; set; }
        public string Param3 { get; set; }
    }
}
