using FLY.Thick.Base.Common;
using FLY.Thick.Base.IService;
using FLY.Thick.Base.UI;
using GalaSoft.MvvmLight.Command;
using System;
using System.ComponentModel;
using System.Net;
using System.Windows.Controls;
using Unity;

namespace FLY.Thick.Blowing.UI
{
    /// <summary>
    /// Page_FlyAD.xaml 的交互逻辑
    /// </summary>
    public partial class PgFlyAd : Page
    {
        PgFlyAdVm viewModel;
        public PgFlyAd()
        {
            InitializeComponent();
        }

        [InjectionMethod]
        public void Init(
            IFlyADService flyAdService)
        {
            viewModel = new PgFlyAdVm();
            viewModel.Init(flyAdService);
            this.DataContext = viewModel;
        }


    }
    public class PgFlyAdVm : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        #region 参数

        public IPEndPoint EP { get; set; }


        #endregion





        public int Version { get; set; }
        public int HardwareVersion { get; set; }


        #region Command
        public RelayCommand ApplyCmd { get; }
        #endregion

        IFlyADService flyAdService;

        public PgFlyAdVm()
        {
            ApplyCmd = new RelayCommand(Apply);
        }

        public void Init(
            IFlyADService flyAdService)
        {
            this.flyAdService = flyAdService;

            Misc.BindingOperations.SetBinding(this.flyAdService, nameof(this.flyAdService.Addr), () =>
            {
                EP = Misc.StringConverter.ToIPEndPoint(this.flyAdService.Addr);
            });

            Misc.BindingOperations.SetBinding(this.flyAdService, nameof(this.flyAdService.Verson), this, nameof(Version));
            Misc.BindingOperations.SetBinding(this.flyAdService, nameof(this.flyAdService.HardwareVersion), this, nameof(HardwareVersion));

        }
        private void Apply()
        {
            if (this.EP == null)
            {
                FLY.ControlLibrary.Window_WarningTip.Show("警告",
                   "地址不能为空",
                   TimeSpan.FromSeconds(2));
            }
            if (!WdPassword.Authorize("FlyAd"))
                return;

            flyAdService.HasCRC = false;
            flyAdService.Addr = this.EP.ToString();

            flyAdService.HardwareVersion = HardwareVersion;

            FLY.ControlLibrary.Window_Tip.Show("应用成功",
                null,
                TimeSpan.FromSeconds(2));

        }
    }
}