using com.fly.secute;
using CommunityToolkit.Mvvm.Input;
using Flyad7_WPF.Converters;
using Flyad7_WPF.Secute;
using FlyADBase;
using FObjBase;
using System;
using System.Collections.Generic;
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.Shapes;
using Unity;

namespace Flyad7_WPF
{
    /// <summary>
    /// WdCreateAccess.xaml 的交互逻辑
    /// </summary>
    public partial class WdCreateAccess : Window
    {
        WdCreateAccessVm viewModel;
        public WdCreateAccess()
        {
            InitializeComponent();

        }
        [InjectionMethod]
        public void Init(FlyAD7 flyad, FlyADClientUI flyADClientUI) 
        {

            viewModel = new WdCreateAccessVm();
            viewModel.Init(flyad, flyADClientUI);
            this.DataContext = viewModel;
        }

    }
    public class WdCreateAccessVm : INotifyPropertyChanged
    {
        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 int Hours { get; set; } = 8000;
        public string Message { get; set; }
        public bool IsConnected { get; private set; }
        public int SuccessCnt { get; private set; }
        public bool IsEnableReConnect => !IsConnected;


        public RelayCommand CreateAccessCmd { get; private set; }
        public RelayCommand CheckAccessCmd { get; private set; }

        public RelayCommand SetAccessCmd { get; private set; }

        public RelayCommand InitCodeCmd { get; private set; }
        public RelayCommand InitCodeAutoCmd { get; private set; }
        public RelayCommand ReConnectCmd { get; private set; }


        

        FlyAD7 flyad;
        FlyADClientUI flyADClientUI;
        CreateAccessHelper createA;
        public WdCreateAccessVm() {
            CreateAccessCmd = new RelayCommand(CreateAccess);
            CheckAccessCmd = new RelayCommand(CheckAccess);
            SetAccessCmd = new RelayCommand(SetAccess);
            InitCodeCmd = new RelayCommand(InitCode);
            ReConnectCmd = new RelayCommand(ReConnect);
            InitCodeAutoCmd = new RelayCommand(InitCodeAuto);
 
            createA = new CreateAccessHelper();
        }



        public void Init(FlyAD7 flyad, FlyADClientUI flyADClientUI) {
            this.flyad = flyad;
            this.flyADClientUI = flyADClientUI;
            Code = flyad.Code.ToArray();
            Access = flyad.Access.ToArray();

            Misc.BindingOperations.SetBinding(flyad, nameof(flyad.IsConnected), this, nameof(IsConnected));
            Misc.BindingOperations.SetBinding(flyad, nameof(flyad.Code), this, () => {
                Code = flyad.Code.ToArray();
            });
            Misc.BindingOperations.SetBinding(flyad, nameof(flyad.Access), this, () => {
                Access = flyad.Access.ToArray();
            });

            flyad.PropertyChanged += Flyad_PropertyChanged;
        }

        private void Flyad_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {

        }

        private void CheckAccess() 
        {
            bool ret = createA.CheckAccess(Code, Access, out int hours, out string message);
            if (ret)
            {
                Hours = hours;
                MessageBox.Show(message);
            }
            else {
                MessageBox.Show(message, "异常", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void CreateAccess()
        {
            
            bool ret = createA.CreateAccess(Code, Hours, out byte[] access, out string message);
            if (ret)
            {
                Access = access;

                Message = "生成授权码 成功";
                MessageBox.Show(Message);
            }
            else
            {
                Message = $"生成授权码失败 {message}";
                MessageBox.Show(Message, "异常", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }


        private void SetAccess()
        {
            bool ret = false;
            if (Code != null && Code.Count() == flyad.Code.Count()) 
            {
                for (int i = 0; i < Code.Length; i++)
                {
                    if (flyad.Code[i] != Code[i])
                    {
                        break;
                    }
                }
                ret = true;
            }

            
            if (!ret) {

                Message = "异常!!!序列码与AD盒不相同";
                MessageBox.Show(Message, "异常", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }



            ret = createA.CheckAccess(Code, Access, out int hours, out string message);
            Hours = hours;

            if (!ret)
            {
                
                Message = $"授权码异常!!!{message}";
                MessageBox.Show(message, "授权码异常", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            Message = $"正在写入授权码中。。。。。。。。。。。。。。。。。。。。。。。";
            flyADClientUI.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:
                                {
                                    Message = "写入授权码成功";
                                    MessageBox.Show(Message);
                                    SuccessCnt++;
                                }
                                break;
                            case AREA_ERR.DUP_ACCESS:
                                {
                                    Message = "设置失败！！！！！重复使用码";
                                    MessageBox.Show(Message, "设置失败", MessageBoxButton.OK, MessageBoxImage.Error);
                                }
                                break;
                            case AREA_ERR.ERR_ACCESS:
                                {
                                    Message = "设置失败！！！！！无效使用码";
                                    MessageBox.Show(Message, "设置失败", MessageBoxButton.OK, MessageBoxImage.Error);
                                }
                                break;
                        }
                    }), null);

            
        }

        void initCode_step01() 
        {
            flyADClientUI.InitArea(new AsyncCBHandler(
            delegate (object AsyncState, object retData)
            {
                AccessInfo a = (AccessInfo)retData;
                Access = a.access;
                Code = a.code;
                Hours = a.surplus;
                switch (a.ret)
                {
                    case AREA_ERR.NO_ERR:
                        {
                            Message = "初始化成功";
                            //MessageBox.Show(Message);

                            initCode_step02();
                        }
                        break;
                    case AREA_ERR.DUP_ACCESS:
                        {
                            Message = "设置失败！！！！！重复使用码";
                            MessageBox.Show(Message, "设置失败", MessageBoxButton.OK, MessageBoxImage.Error);
                        }
                        break;
                    case AREA_ERR.ERR_ACCESS:
                        {
                            Message = "设置失败！！！！！无效使用码";
                            MessageBox.Show(Message, "设置失败", MessageBoxButton.OK, MessageBoxImage.Error);
                        }
                        break;
                }
            }), null);
        }
        void initCode_step02()
        {
            bool ret = createA.CreateAccess(Code, Hours, out byte[] access, out string message);
            if (ret)
            {
                Access = access;

                Message = "生成授权码 成功";
                //MessageBox.Show(Message);

                initCode_step03();
            }
            else
            {
                Message = $"生成授权码失败 {message}";
                MessageBox.Show(Message, "异常", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        void initCode_step03()
        {
            SetAccess();
        }
        private void InitCode()
        {
            Message = $"正在初始化。。。。。。。。。。。。。。。。。。。。。。。";
            flyADClientUI.InitArea(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:
                        {
                            Message = "初始化成功";
                            MessageBox.Show(Message);
                        }
                        break;
                    case AREA_ERR.DUP_ACCESS:
                        {
                            Message = "设置失败！！！！！重复使用码";
                            MessageBox.Show(Message, "设置失败", MessageBoxButton.OK, MessageBoxImage.Error);
                        }
                        break;
                    case AREA_ERR.ERR_ACCESS:
                        {
                            Message = "设置失败！！！！！无效使用码";
                            MessageBox.Show(Message, "设置失败", MessageBoxButton.OK, MessageBoxImage.Error);
                        }
                        break;
                }
            }), null);
        }
        private void InitCodeAuto()
        {
            Message = $"正在初始化。。。。。。。。。。。。。。。。。。。。。。。";
            initCode_step01();
        }

        private void ReConnect()
        {
            flyad.ReConnect();
        }
    }

}
