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.Shapes;

using System.ComponentModel;

using System.Net;

using FLY.Thick.Base.Common;
using FLY.Thick.Base.Client;
using FLY.Thick.Base.IService;
using FObjBase;
using GalaSoft.MvvmLight.Command;

namespace FLY.Thick.Base.UI
{
    /// <summary>
    /// WdFlyADAccess.xaml 的交互逻辑
    /// </summary>
    public partial class WdFlyADAccess : FLY.ControlLibrary.WindowBigClose
    {
        WdFlyADAccessVm viewModel;
        /// <summary>
        /// 
        /// </summary>
        public WdFlyADAccess()
        {
            InitializeComponent();
            viewModel = new WdFlyADAccessVm();
            this.DataContext = viewModel;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="flyADService"></param>
        public void Init(IFlyADService flyADService)
        {
            viewModel.Init(flyADService);
        }

    }

    /// <summary>
    /// WdFlyADAccess 的 ViewModel
    /// </summary>
    public class WdFlyADAccessVm : INotifyPropertyChanged
    {
        IFlyADService mFlyADService = null;



        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// 使用码
        /// </summary>
        [PropertyChanged.DoNotCheckEquality]
        public byte[] Access { get; set; } = new byte[8];

        /// <summary>
        /// 序列码
        /// </summary>
        [PropertyChanged.DoNotCheckEquality]
        public byte[] Code { get; set; } = new byte[7];

        public RelayCommand ApplyCmd { get; private set; }


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

        /// <summary>
        /// 
        /// </summary>
        /// <param name="flyADService"></param>
        public void Init(IFlyADService flyADService)
        {
            mFlyADService = flyADService;

            mFlyADService.GetAccessInfo(
                new AsyncCBHandler(
                    delegate (object AsyncState, object retData)
                    {
                        AccessInfo a = (AccessInfo)retData;
                        Access = a.access;
                        Code = a.code;
                    }), null);
        }

        void Apply()
        {
            if ((Access == null) || (Access.Length != 8))
            {
                FLY.ControlLibrary.Window_Tip.Show("错误", "长度不对，需要 8位 byte!!",
                    TimeSpan.FromSeconds(2));
                return;
            }
            if (mFlyADService == null)
                return;

            mFlyADService.SetAccess(
                Access,
                new AsyncCBHandler(
                    delegate (object AsyncState, object retData)
                    {
                        AccessInfo a = (AccessInfo)retData;
                        Access = a.access;
                        Code = a.code;
                        switch (a.ret)
                        {
                            case AREA_ERR.NO_ERR:
                                {
                                    FLY.ControlLibrary.Window_Tip.Show("成功", "设置成功！！",
                                        TimeSpan.FromSeconds(2));
                                }
                                break;
                            case AREA_ERR.DUP_ACCESS:
                                {
                                    FLY.ControlLibrary.Window_Tip.Show("错误", "授权码重复！！",
                                        TimeSpan.FromSeconds(2));
                                }
                                break;
                            case AREA_ERR.ERR_ACCESS:
                                {
                                    FLY.ControlLibrary.Window_Tip.Show("错误", "授权码不正确！！",
                                        TimeSpan.FromSeconds(2));
                                }
                                break;
                        }
                    }), null);
            FLY.ControlLibrary.Window_Tip.Show("通知", "请等待。。。。。。",
                TimeSpan.FromSeconds(2));
        }
    }
    /// <summary>
    /// 
    /// </summary>
    public class BytesConverter : IValueConverter
    {
        #region IValueConverter 成员

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
                return "";

            byte[] bs = (byte[])value;
            string str = "";
            for (int i = 0; i < bs.Length; i++)
                str += bs[i].ToString("X2");
            return str;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string str = (string)value;
            string h = "";
            List<byte> bs = new List<byte>();
            for (int i = 0; i < str.Length; i++)
            {
                if (str[i] != ' ')
                    h += str[i];

                if (((str[i] == ' ') && (h.Length != 0)) ||
                    (h.Length >= 2))
                {
                    byte b = 0;
                    if (byte.TryParse(h, System.Globalization.NumberStyles.AllowHexSpecifier, null, out b))
                    {
                        bs.Add(b);
                    }
                    else
                    {
                        break;
                    }
                    h = "";
                }
            }

            return bs.ToArray();
        }

        #endregion
    }
}
