using ATC_MaterialBind.Entity;
using Microsoft.Extensions.DependencyInjection;
using SlnMesnac.Model.domain;
using SlnMesnac.Model.dto;
using SlnMesnac.Plc;
using SlnMesnac.Rfid;
using SlnMesnac.WPF.Attribute;
using SlnMesnac.WPF.Model;
using SlnMesnac.WPF.ViewModel.IndexPage;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Automation.Peers;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SlnMesnac.WPF.Page.IndexPage
{
    /// <summary>
    /// MaterialBind.xaml 的交互逻辑
    /// </summary>
    [RegisterAsSingletonAttribute]
    public partial class MiddleWare : UserControl
    {
        public List<RfidAbsractFactory> rfidList;
        MiddleWareViewModel indexContentViewModel;
        public static Action<string, string> RefreshStateEvent;
        public MiddleWare()
        {
            rfidList = App.ServiceProvider.GetRequiredService<List<RfidAbsractFactory>>();
            InitializeComponent();
            indexContentViewModel = new MiddleWareViewModel();
            //indexContentViewModel._Action += RefreshStationQuallity;
            this.DataContext = indexContentViewModel;
        }

        private void DataGridRow_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            // 1. 标记事件已处理，避免冒泡到父控件（可选，防止重复触发）
            e.Handled = true;

            // 2. 获取当前右键的行对象
            if (sender is not DataGridRow row || row.DataContext is not SenorInfo selectedEmp) return;

            // 2. 创建右键菜单（动态/静态均可）
            var contextMenu = new ContextMenu();
            var menuItem1 = new MenuItem { Header = $"修改" };
            //var menuItem2 = new MenuItem { Header = $"读取" };

            menuItem1.Click += (s, args) =>
            {
                SetPower setPowerContent = new SetPower(selectedEmp.Combineid);
                setPowerContent.ShowDialog();
            };
            //menuItem2.Click += async (s, args) =>
            //{
            //    List<TagInfo> rfid = await rfidList.Find(x=>x.ConfigKey == selectedEmp.Combineid).GetRFIDAsync();
            //    if (rfid != null && rfid.Count > 0)
            //    {
            //        rfid = rfid.Where((x, i) => rfid.FindIndex(z => z.EPCstring == x.EPCstring) == i).ToList();

            //        RefreshStateEvent?.Invoke(selectedEmp.Combineid, string.Join(",", rfid.Select(item => item.EPCstring)));

            //    }
                

            //};

            contextMenu.Items.Add(menuItem1);
            //contextMenu.Items.Add(menuItem2);

            // 3. 绑定菜单到当前行并显示
            row.ContextMenu = contextMenu;
            contextMenu.PlacementTarget = row;
            contextMenu.IsOpen = true;

        }

        private void RefreshStationQuallity(List<StationQualityInfo> stationQualityInfos)
        {
            Application.Current.Dispatcher.Invoke(new Action(() =>
            {
                stackpanel1.Children.Clear();
                if (stationQualityInfos != null && stationQualityInfos.Count > 0)
                {
                    List<UIElement> buttons = new List<UIElement>();
                    foreach (var item in stationQualityInfos)
                    {
                        buttons.Add(new Button { FontSize = 20, Background = item.State == "1" ? Brushes.Lime : Brushes.Red, Height = 50, Content = item.StationName });
                    }

                    foreach (UIElement button in buttons)
                    {
                        stackpanel1.Children.Add(button);
                    }
                }
                else {
                    stackpanel1.Children.Remove(this);
                }

            }));
           
        }

        //在载入行的时候在行表头添加编号	
        private void dgv_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            e.Row.Header = (e.Row.GetIndex() + 1).ToString();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {

        }
    }
}
