using NEIntelligentControl2.Models.Alarm;
using NEIntelligentControl2.Models.BoostStation;
using NEIntelligentControl2.Models.Messages;
using NEIntelligentControl2.Models.Pages;
using NEIntelligentControl2.Properties;
using NEIntelligentControl2.Service.Station;
using NEIntelligentControl2.Service.Voice;
using NEIntelligentControl2.Service.WebSocket;
using NEIntelligentControl2.Views.BoostStation;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
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 NEIntelligentControl2.Pages.BoostStation
{
    /// <summary>
    /// 升压站缩略图
    /// </summary>
    public partial class PageStationAll : Page
    {
        private List<SVGViewer.Models.Info.ShapeInfo> shapeInfos;
        private StationManager _StationManager;
        private FaultPopupBridge _FaultPopupBridge;
        private MessageBridge _MessageBridge;

        private Dictionary<string, ThumbnailView> _ThumbnailViews;

        public Action<string> StationChanged { get; internal set; }

        public PageStationAll(List<SVGViewer.Models.Info.ShapeInfo> sis)
        {
            InitializeComponent();
            shapeInfos = sis;
            _StationManager = App.ServiceProvider.GetService(typeof(StationManager)) as StationManager;
            _FaultPopupBridge = new FaultPopupBridge() { Messaged = OnAlarmMessage };
            _MessageBridge = App.ServiceProvider.GetService(typeof(MessageBridge)) as MessageBridge;
            _ThumbnailViews = new Dictionary<string, ThumbnailView>();
            Init();
        }

        private void Init()
        {
            var cs = Math.Max(3, Settings.Default.BoostStationColumns);
            _ug.Columns = cs;
            foreach (var v in shapeInfos)
            {
                var vv = v.Clone() as SVGViewer.Models.Info.ShapeInfo;
                ThumbnailView tv = new ThumbnailView(vv, _StationManager.GetCustomizeShapesDic()) { DoubleClicked = ThumbnailDoubleClicked };
                _ug.Children.Add(tv);
                if (!_ThumbnailViews.ContainsKey(v.Id))
                {
                    _ThumbnailViews.Add(v.Id, tv);
                }
            }
        }

        private void ThumbnailDoubleClicked(string obj)
        {
            StationChanged?.Invoke(obj);
        }

        private void OnAlarmMessage(List<FaultInfo> obj)
        {
            if (obj == null || obj.Count == 0) return;
            var set = obj.Where(o => _ThumbnailViews.ContainsKey(o.Category2)).Select(o => o.Category2).ToHashSet();
            Dispatcher.Invoke(() =>
            {
                foreach (var v in _ThumbnailViews)
                {
                    v.Value.IsAlarm = set.Contains(v.Key);
                }
            });
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            _MessageBridge.Register(_FaultPopupBridge);
        }

        private void Page_Unloaded(object sender, RoutedEventArgs e)
        {
            _MessageBridge.Unregister(_FaultPopupBridge);
        }

        internal void SwitchSize(int v)
        {
            _ug.Columns = v;
            Settings.Default.BoostStationColumns = v;
            Settings.Default.Save();
        }
    }
}
