using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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;
using System.Net;
using FLY.Thick.Base.Client;
using Unity;
using FLY.OBJComponents.Common;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace FLY.Thick.Base.UI
{
    /// <summary>
    /// Page_ErrorTable.xaml 的交互逻辑
    /// </summary>
    public partial class PgErrorTable : Page
    {
        FLY.OBJComponents.IService.IWarningSystem2Service warningService;
        IUnityContainer container;

        ObservableCollection<FlyData_WarningHistory> ReasonList = new ObservableCollection<FlyData_WarningHistory>();
        ToDescriptionHandler toDescription;
        /// <summary>
        /// 
        /// </summary>
        public PgErrorTable()
        {
            InitializeComponent();

        }


        [InjectionMethod]
        public void Init(
            IUnityContainer container,
            FLY.OBJComponents.IService.IWarningSystem2Service warningService,
            ToDescriptionHandler toDescription = null
            ) 
        {
            this.container = container;
            this.warningService = warningService;
            this.toDescription = toDescription;

            this.DataContext = warningService;
            this.dgReason.ItemsSource = ReasonList;

            updateReasonList();

            warningService.PropertyChanged += WarningService_PropertyChanged;

        }

        private void WarningService_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == nameof(warningService.ReasonList))
            {
                updateReasonList();
            }
        }
        void updateReasonList()
        {
            ReasonList.Clear();
            if (warningService.ReasonList == null)
                return;

            for (int i = 0; i < warningService.ReasonList.Count(); i++)
            {
                var reason = warningService.ReasonList[i];
                ReasonList.Add(new FlyData_WarningHistory()
                {
                    Time = reason.Time,
                    State = reason.State,
                    ErrCode = reason.ErrCode,
                    Description = toDescription == null ? reason.Description : toDescription(reason.ErrCode)
                });
            }
        }
        private void button_back_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.GoBack();
        }

        private void button_reset_click(object sender, RoutedEventArgs e)
        {
            warningService.Reset();
        }

        private void button_iotip_click(object sender, RoutedEventArgs e)
        {
            var w = new WdIOTip();
            container.BuildUp(w);

            w.Owner = FLY.ControlLibrary.COMMON.GetWindow(this);
            w.ShowDialog();
        }

        private void button_access_click(object sender, RoutedEventArgs e)
        {
            WdFlyADAccess w = new WdFlyADAccess();
            var flyAdService = container.Resolve<FLY.Thick.Base.IService.IFlyADService>();
            w.Init(flyAdService);
            w.Owner = FLY.ControlLibrary.COMMON.GetWindow(this);
            w.ShowDialog();
        }

        private void button_silence_click(object sender, RoutedEventArgs e)
        {
            warningService.Silence();
        }

        private void button_database_click(object sender, RoutedEventArgs e)
        {
            PgErrorAllTable p = container.Resolve<PgErrorAllTable>(
                    new Unity.Resolution.ParameterOverride("warning", warningService),
                    new Unity.Resolution.ParameterOverride("toDescription", toDescription)
                    );
            NavigationService.Navigate(p);
        }
    }
}
