using log4net;
using MasaBlazorApp3.Pojo;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using zkemkeeper;
using Microsoft.Extensions.Options;
using MasaBlazorApp3.Pojo.Config;

namespace MasaBlazorApp3.Finger
{
    public class FingerprintUtil
    {

        private readonly ILog logger = LogManager.GetLogger(typeof(FingerprintUtil));
        private readonly FingerPojo _options;

        public CZKEMClass axCZKEM1 = new CZKEMClass();
        public bool bIsConnected = false;

        private int fingerPort = 4370;
        private int machineNumber = 1;
        private int machineType = 2;

        public FingerprintUtil(IOptions<FingerPojo> options)
        {
            _options = options.Value;
            logger.Info($"进入构造器，开始连接指纹机");
           // ConnectionMain();
        }

        public async Task<bool> ConnectionMain()
        {
            return await Task<bool>.Run(() =>
            {

                bIsConnected = axCZKEM1.Connect_Net(_options.ip, fingerPort);
                logger.Info($"连接指纹机,IP:{_options.ip},端口:{fingerPort},机器号:{machineNumber},连接结果:{bIsConnected}");
                if (bIsConnected)
                {
                    if (axCZKEM1.RegEvent(machineNumber, 65535))
                    {
                        this.axCZKEM1.OnAttTransactionEx += new zkemkeeper._IZKEMEvents_OnAttTransactionExEventHandler(axCZKEM1_OnAttTransactionEx);
                        //this.axCZKEM1.OnEnrollFinger += new zkemkeeper._IZKEMEvents_OnEnrollFingerEventHandler(axCZKEM1_OnEnrollFinger);
                        this.axCZKEM1.OnEnrollFingerEx += new zkemkeeper._IZKEMEvents_OnEnrollFingerExEventHandler(axCZKEM1_OnEnrollFingerEx);

                    }
                }
                return bIsConnected;
            });


        }

        //If your fingerprint(or your card) passes the verification,this event will be triggered
        private void axCZKEM1_OnAttTransactionEx(string sEnrollNumber, int iIsInValid, int iAttState, int iVerifyMethod, int iYear, int iMonth, int iDay, int iHour, int iMinute, int iSecond, int iWorkCode)
        {

            logger.Info($"触发用户验证通过事件，用户id:{sEnrollNumber}验证方式:{iVerifyMethod}");
        }

        //When you are enrolling your finger,this event will be triggered.
        private void axCZKEM1_OnEnrollFinger(int iEnrollNumber, int iFingerIndex, int iActionResult, int iTemplateLength)
        {

            axCZKEM1.StartIdentify();
            axCZKEM1.RefreshData(1);

            logger.Info($"触发用户登记指纹事件，用户id:{iEnrollNumber}指纹索引:{iFingerIndex}登记结果:{(iActionResult == 0)}");

        }

        private void axCZKEM1_OnEnrollFingerEx(string iEnrollNumber, int iFingerIndex, int iActionResult, int iTemplateLength)
        {

            axCZKEM1.StartIdentify();
            axCZKEM1.RefreshData(1);
            logger.Info($"触发用户登记指纹事件1，用户id:{iEnrollNumber}指纹索引:{iFingerIndex}登记结果:{(iActionResult == 0)}");

        }

        /**
         * 删除用户
         */
        public bool DelUser(int Id)
        {
            bool result = false;
            axCZKEM1.EnableDevice(machineNumber, false);
            if (machineType == 1)
            {
                result = axCZKEM1.DeleteEnrollData(machineNumber, Id, machineNumber, 12);
            }
            else
            {
                result = axCZKEM1.SSR_DeleteEnrollData(machineNumber, Id.ToString(), 12);
            }

            axCZKEM1.RefreshData(machineNumber);
            axCZKEM1.EnableDevice(machineNumber, true);
            return result;
        }

        /**
         * 添加或修改用户
         */
        public bool SaveUser(User User)
        {
            bool result = false;
            //axCZKEM1.SetStrCardNumber(User.UserBarcode);
            if (machineType == 1)
            {
                result = axCZKEM1.SetUserInfo(machineNumber, User.Id, User.NickName, "123456", 0, true);
            }
            else
            {
                result = axCZKEM1.SSR_SetUserInfo(machineNumber, User.Id.ToString(), User.NickName, "123456", 0, true);
            }
            return result;
        }

        /**
         * 添加或修改用户指纹
         */
        public bool SaveFingerprint(int Id, int FingerIndex)
        {
            bool result = false;
            // 取消其他操作
            bool res2 = axCZKEM1.CancelOperation();
            // 删除源指纹
            bool res = axCZKEM1.SSR_DelUserTmp(machineNumber, Id.ToString(), FingerIndex);
            // 添加新指纹
            result = axCZKEM1.StartEnrollEx(Id.ToString(), FingerIndex, 3);
            return result;
        }
    }
}
