using CommunityToolkit.Mvvm.Input;
using FLY.Thick.Base.Common;
using FLY.Thick.Base.IService;
using FObjBase;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using System.Windows.Data;

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))
            {
                string tit = (string)Application.Current.TryFindResource("str.WdFlyADAccess.ApplyFailed");
                string msg = (string)Application.Current.TryFindResource("str.WdFlyADAccess.LenMustBe8");
                FLY.ControlLibrary.Window_Tip.Show(tit, msg, 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:
                                {
                                    string tit = (string)Application.Current.TryFindResource("str.WdFlyADAccess.ApplySuccessfully");
                                    FLY.ControlLibrary.Window_Tip.Show(tit,null,TimeSpan.FromSeconds(2));
                                }
                                break;
                            case AREA_ERR.DUP_ACCESS:
                                {
                                    string tit = (string)Application.Current.TryFindResource("str.WdFlyADAccess.ApplyFailed");
                                    string msg = (string)Application.Current.TryFindResource("str.WdFlyADAccess.AuthCodeDuplication");
                                    FLY.ControlLibrary.Window_Tip.Show(tit, msg, TimeSpan.FromSeconds(2));
                                }
                                break;
                            case AREA_ERR.ERR_ACCESS:
                                {
                                    string tit = (string)Application.Current.TryFindResource("str.WdFlyADAccess.ApplyFailed");
                                    string msg = (string)Application.Current.TryFindResource("str.WdFlyADAccess.AuthCodeIncorrect");
                                    FLY.ControlLibrary.Window_Tip.Show(tit, msg, TimeSpan.FromSeconds(2));
                                }
                                break;
                        }
                    }), null);
            {
                string msg = (string)Application.Current.TryFindResource("str.WdFlyADAccess.Wait");
                FLY.ControlLibrary.Window_Tip.Show(null, msg, 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
    }
}
