using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using VisionBuilder.UI.Common;
using VisionBuilder.UI.IOCommander.Interfaces;

namespace VisionBuilder.UI.IOCommander.Windows
{
    public partial class IOCommanderVirtualInput : Form, IIOCommander
    {
        private CheckBox[] _checkBoxes;

        public IOCommanderVirtualInput()
        {
            InitializeComponent();
            InitializeCheckboxes();
        }

        public BitArray PinState { get; set; } = new BitArray(10);
        public event Action<int, bool>? OnPinChanged = delegate { };
        
        public void SetPins(int pin, bool state)
        {
            // This is a dummy function as per requirement
        }

        private void InitializeCheckboxes()
        {
            // Set form properties
            this.Text = "IO Commander Virtual Input";
            this.ClientSize = new Size(200, 280);
            this.FormBorderStyle = FormBorderStyle.FixedDialog;
            this.MaximizeBox = false;
            this.StartPosition = FormStartPosition.Manual;
            this.Location = new Point(350, 50); // Set a fixed position for the form

            // Create a panel to contain checkboxes with scrolling if needed
            Panel panel = new Panel
            {
                Dock = DockStyle.Fill,
                AutoScroll = true
            };
            this.Controls.Add(panel);

            // Initialize checkboxes
            _checkBoxes = new CheckBox[10];
            
            for (int i = 0; i < 10; i++)
            {
                int pinNumber = i + 1; // Pins are 1-indexed (1-10)
                _checkBoxes[i] = new CheckBox
                {
                    Text = pinNumber.ToString(),
                    Location = new Point(20, 20 + (i * 22)),
                    Size = new Size(150, 20),
                    Tag = pinNumber, // Store the pin number in Tag for easy reference
                };
                
                // Add event handler
                _checkBoxes[i].CheckedChanged += CheckBox_CheckedChanged;
                
                panel.Controls.Add(_checkBoxes[i]);
            }
        }

        private void CheckBox_CheckedChanged(object? sender, EventArgs e)
        {
            if (sender is CheckBox checkBox)
            {
                int pin = (int)checkBox.Tag;
                bool state = checkBox.Checked;
                
                // Update the BitArray (convert 1-based pin to 0-based index)
                PinState.Set(pin - 1, state);
                
                // Trigger the event
                OnPinChanged?.Invoke(pin, state);
            }
        }


        protected override void OnShown(EventArgs e)
        {
            TopMost= true;
            Focus();
            TopMost = true;
        }
    }
}
