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;

namespace ThickTcpUiInWindow
{
    /// <summary>
    /// Window_FlyADAccess.xaml 的交互逻辑
    /// </summary>
    public partial class Window_FlyADAccess : FLY.ControlLibrary.WindowBigClose,INotifyPropertyChanged
    {
        FlyADServiceClient mFlyADService = null;
        byte[] access = new byte[8];
        byte[] code = new byte[7];
        public byte[] Access {
            get {
                return access;
            }
            set {
                access = value;
                NotifyPropertyChanged("Access");
            }
        }
        public byte[] Code 
        {
            get
            {
                return code;
            }
            set {
                code = value;
                NotifyPropertyChanged("Code");
            }
        }
        public Window_FlyADAccess()
        {
            InitializeComponent();
            
        }


        private void button_apply_Click(object sender, RoutedEventArgs e)
        {
            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));
        }

        public void Init(IPEndPoint serverep) 
        {
            mFlyADService = new FlyADServiceClient(FLY.Thick.Base.OBJ_INTERFACE.OBJ_INTERFACE_ID.FLYAD_ID);
            FObjBase.FObjSys.Current.Connect_to_Another_OBJSys(
                serverep, mFlyADService.ID);
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.DataContext = this;
            mFlyADService.GetAccessInfo(
                new AsyncCBHandler(
                    delegate(object AsyncState, object retData)
                    {
                        AccessInfo a = (AccessInfo)retData;
                        Access = a.access;
                        Code = a.code;
                    }), null);
        }

        private void Window_Unloaded(object sender, RoutedEventArgs e)
        {
            mFlyADService.Dispose();
        }


        #region INotifyPropertyChanged 成员

        public event PropertyChangedEventHandler PropertyChanged;
        protected void NotifyPropertyChanged(string propertyname)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyname));
            }
        }
        #endregion
    }
    public class BytesConverter : IValueConverter
    {
        #region IValueConverter 成员

        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;
        }

        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
    }
}
