using FLY.OBJComponents.Common;
using FLY.OBJComponents.Server;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
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 FLY.Thick.FilmCasting.UI.Server
{
    /// <summary>
    /// PgAllErrNos.xaml 的交互逻辑
    /// </summary>
    public partial class UcAllErrNos : UserControl
    {
        UcAllErrNosVm viewModel;
        public UcAllErrNos()
        {
            InitializeComponent();
        }

        [Unity.InjectionMethod]
        public void Init(List<ERRNO> errnos, WarningSystem2 warningSystem)
        {
            viewModel = new UcAllErrNosVm();
            viewModel.Init(errnos, warningSystem);
            this.DataContext = viewModel;
        }
    }
    public class UcAllErrNosVm : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public ObservableCollection<ERRNOView> ErrNos { get; private set; }

        WarningSystem2 warningSystem;
        public void Init(List<ERRNO> errnos, WarningSystem2 warningSystem)
        {
            this.warningSystem = warningSystem;
            ErrNos = new ObservableCollection<ERRNOView>(errnos.Select(errno => new ERRNOView() { ErrCode = errno.Code, Description = errno.Description }));

            updateIsError();
            warningSystem.PropertyChanged += WarningSystem_PropertyChanged;
        }

        private void WarningSystem_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == nameof(warningSystem.ReasonList))
            {
                updateIsError();
            }
        }
        void updateIsError()
        {
            if (warningSystem.ReasonList != null && warningSystem.ReasonList.Count() > 0)
            {
                for (int i = 0; i < ErrNos.Count(); i++)
                {
                    var errNo = ErrNos[i];
                    if (warningSystem.ReasonList.Any(f => f.ErrCode == errNo.ErrCode))
                        errNo.IsError = true;
                    else
                        errNo.IsError = false;
                }
            }
            else
            {
                for (int i = 0; i < ErrNos.Count(); i++)
                {
                    var errNo = ErrNos[i];
                    errNo.IsError = false;
                }
            }

        }
    }
    public class ERRNOView : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public int ErrCode { get; set; }
        public string Description { get; set; }

        public bool IsError { get; set; }
    }
}
