using PlcUiControl;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;
using PlcUiForm;
namespace YangjieTester.工站操作
{
    [FormDescriptionAttribute("工站操作主画面")]
    public partial class StationOpMainForm : PlcBaseForm
    {
        public StationOpMainForm()
        {
            InitializeComponent();
            uiFlowLayoutPanel1.Height = btn0主站马达.Height * 3 + 25;
        }

        private void uiFlowLayoutPanel1_SizeChanged(object sender, EventArgs e)
        {
            foreach (Control control in uiFlowLayoutPanel1.AllControls)
            {
                control.Size = new Size((int)(uiFlowLayoutPanel1.Width *0.094), control.Height);
            }
        }

        static object preClickedBtn = null;
        private void btnStationForm_Click(object sender, EventArgs e)
        {
            var formType = GetFormTypeByButtonName(((UISymbolButton)sender).Name);
            if (formType != null)
            {
                FormEx.SetButtonSelected(sender);

                preClickedBtn = sender;
                UIForm form = (UIForm)Activator.CreateInstance(formType);
               
                FormEx.ShowSubForm(uiPanel1, form);
            }
        }

        private Type GetFormTypeByButtonName(string buttonName)
        {
            // 1. 从按钮名提取开头的数字（btn 后连续数字）
            var match = Regex.Match(buttonName, @"^btn(\d+)", RegexOptions.IgnoreCase);
            if (!match.Success)
                return null;

            string number = match.Groups[1].Value; // 如 "1", "12"

            string namespaceName = typeof(StationOpMainForm).Namespace;
            Assembly assembly = Assembly.GetExecutingAssembly();

            // 2. 构造正则：匹配 Form{number} + (结尾 或 下划线开头的后缀)
            // 例如 number="1" → ^Form1(_|$)
            string pattern = $@"^Form{Regex.Escape(number)}(?:_|$)";
            var formNameRegex = new Regex(pattern, RegexOptions.IgnoreCase);

            var formType = assembly.GetTypes()
                .FirstOrDefault(t =>
                    t.IsClass &&
                    !t.IsAbstract &&
                    typeof(UIForm).IsAssignableFrom(t) &&
                    t.Namespace == namespaceName &&
                    formNameRegex.IsMatch(t.Name));

            return formType;
        }

        private void StationOpMainForm_Load(object sender, EventArgs e)
        {
            if (preClickedBtn != null)
            {
                btnStationForm_Click(preClickedBtn, null);
                var btnIndex= uiFlowLayoutPanel1.AllControls.IndexOfKey(((UIButton)preClickedBtn).Name);
                if(btnIndex>0)
                {
                    FormEx.SetButtonSelected(uiFlowLayoutPanel1.AllControls[btnIndex]);
                }
            }
            else
                btnStationForm_Click(btn0主站马达, null);
        }
    }
}
