//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.unis.crk.hardware.util;

import com.rfid.BaseReader;
import com.rfid.ReaderParameter;
import com.rfid.TagCallback;
import lombok.Data;

@Data
public class CReaderCopy {
    private BaseReader reader = null;
    private ReaderParameter param = new ReaderParameter();
    private volatile boolean mWorking = true;
    private volatile Thread mThread = null;
    private byte[] pOUcharIDList = new byte[25600];
    private volatile int NoCardCOunt = 0;
    private String ipAddr = "192.168.0.250";
    private int Port = 27011;
    private int logswitch = 0;

    public  CReaderCopy(String ipAddr, int Port, int logswitch) {
        this.ipAddr = ipAddr;
        this.Port = Port;
        this.logswitch = logswitch;
        this.param.SetAddress((byte)-1);
        this.param.SetScanTime(10);
        this.param.SetSession(1);
        this.param.SetQValue(4);
        this.param.SetTidPtr(0);
        this.param.SetTidLen(0);
        this.param.SetAntenna(1);
        this.reader = new BaseReader(ipAddr);
    }

    public int Connect() {
        return this.reader.Connect(this.ipAddr, this.Port, this.logswitch);
    }

    public int DisConnect() {
        return this.reader.DisConnect();
    }

    public void SetInventoryPatameter(ReaderParameter param) {
        this.param = param;
    }

    public ReaderParameter GetInventoryPatameter() {
        return this.param;
    }

    public int GetUHFInformation(byte[] Version, byte[] Power, byte[] band, byte[] MaxFre, byte[] MinFre, byte[] BeepEn, byte[] Ant) {
        byte[] ReaderType = new byte[1];
        byte[] TrType = new byte[1];
        byte[] ScanTime = new byte[1];
        byte[] OutputRep = new byte[1];
        byte[] CheckAnt = new byte[1];
        byte[] ComAddr = new byte[]{-1};
        int result = this.reader.GetReaderInformation(ComAddr, Version, ReaderType, TrType, band, MaxFre, MinFre, Power, ScanTime, Ant, BeepEn, OutputRep, CheckAnt);
        if (result == 0) {
            this.param.SetAddress(ComAddr[0]);
            this.param.SetAntenna(Ant[0] & 255);
        }

        return result;
    }

    public int SetRfPower(int Power) {
        return this.reader.SetRfPower(this.param.GetAddress(), (byte)Power);
    }

    public int SetRegion(int band, int maxfre, int minfre) {
        return this.reader.SetRegion(this.param.GetAddress(), band, maxfre, minfre);
    }

    public int SetAntenna(byte AntCfg) {
        int result = this.reader.SetAntennaMultiplexing(this.param.GetAddress(), AntCfg);
        if (result == 0) {
            this.param.SetAntenna(AntCfg & 255);
        }

        return result;
    }

    public int SetBeepNotification(int BeepEn) {
        return this.reader.SetBeepNotification(this.param.GetAddress(), (byte)BeepEn);
    }

    public int SetRfPowerByAnt(byte[] Power) {
        return this.reader.SetRfPowerByAnt(this.param.GetAddress(), Power);
    }

    public int GetRfPowerByAnt(byte[] Power) {
        return this.reader.GetRfPowerByAnt(this.param.GetAddress(), Power);
    }

    public int ConfigDRM(byte[] DRM) {
        return this.reader.ConfigDRM(this.param.GetAddress(), DRM);
    }

    public int SetRelay(byte RelayTime) {
        return this.reader.SetRelay(this.param.GetAddress(), RelayTime);
    }

    public int SetGPIO(byte GPIO) {
        return this.reader.SetGPIO(this.param.GetAddress(), GPIO);
    }

    public int GetGPIOStatus(byte[] OutputPin) {
        return this.reader.GetGPIOStatus(this.param.GetAddress(), OutputPin);
    }

    public String GetSerialNo() {
        byte[] btArr = new byte[4];
        int result = this.reader.GetSerialNo(this.param.GetAddress(), btArr);
        if (result == 0) {
            String temp = this.reader.bytesToHexString(btArr, 0, btArr.length);
            return temp;
        } else {
            return "";
        }
    }

    public int MeasureReturnLoss(byte[] TestFreq, byte Ant, byte[] ReturnLoss) {
        return this.reader.MeasureReturnLoss(this.param.GetAddress(), TestFreq, Ant, ReturnLoss);
    }

    public int SetWritePower(byte WritePower) {
        return this.reader.SetWritePower(this.param.GetAddress(), WritePower);
    }

    public int GetWritePower(byte[] WritePower) {
        return this.reader.GetWritePower(this.param.GetAddress(), WritePower);
    }

    public String ReadDataByEPC(String EPCStr, byte Mem, byte WordPtr, byte Num, byte[] Password) {
        if (EPCStr.length() % 4 != 0) {
            return "FF";
        } else {
            byte ENum = (byte)(EPCStr.length() / 4);
            byte[] EPC = this.reader.hexStringToBytes(EPCStr);
            byte MaskMem = 0;
            byte[] MaskAdr = new byte[2];
            byte MaskLen = 0;
            byte[] MaskData = new byte[12];
            byte MaskFlag = 0;
            byte[] Data = new byte[Num * 2];
            byte[] Errorcode = new byte[1];
            int result = this.reader.ReadData_G2(this.param.GetAddress(), ENum, EPC, Mem, WordPtr, Num, Password, MaskMem, MaskAdr, MaskLen, MaskData, MaskFlag, Data, Errorcode);
            return result == 0 ? this.reader.bytesToHexString(Data, 0, Data.length) : "";
        }
    }

    public String ReadDataByTID(String TIDStr, byte Mem, byte WordPtr, byte Num, byte[] Password) {
        if (TIDStr.length() % 4 != 0) {
            return "FF";
        } else {
            byte ENum = -1;
            byte[] EPC = new byte[12];
            byte[] TID = this.reader.hexStringToBytes(TIDStr);
            byte MaskMem = 2;
            byte[] MaskAdr = new byte[2];
            MaskAdr[0] = MaskAdr[1] = 0;
            byte MaskLen = (byte)(TIDStr.length() * 4);
            byte[] MaskData = new byte[TIDStr.length()];
            System.arraycopy(TID, 0, MaskData, 0, TID.length);
            byte MaskFlag = 1;
            byte[] Data = new byte[Num * 2];
            byte[] Errorcode = new byte[1];
            int result = this.reader.ReadData_G2(this.param.GetAddress(), ENum, EPC, Mem, WordPtr, Num, Password, MaskMem, MaskAdr, MaskLen, MaskData, MaskFlag, Data, Errorcode);
            return result == 0 ? this.reader.bytesToHexString(Data, 0, Data.length) : "";
        }
    }

    public int WriteDataByEPC(String EPCStr, byte Mem, byte WordPtr, byte[] Password, String wdata) {
        if (EPCStr.length() % 4 != 0) {
            return 255;
        } else if (wdata.length() % 4 != 0) {
            return 255;
        } else {
            byte ENum = (byte)(EPCStr.length() / 4);
            byte WNum = (byte)(wdata.length() / 4);
            byte[] EPC = this.reader.hexStringToBytes(EPCStr);
            byte[] data = this.reader.hexStringToBytes(wdata);
            byte MaskMem = 0;
            byte[] MaskAdr = new byte[2];
            byte MaskLen = 0;
            byte[] MaskData = new byte[12];
            byte MaskFlag = 0;
            byte[] Errorcode = new byte[1];
            return this.reader.WriteData_G2(this.param.GetAddress(), WNum, ENum, EPC, Mem, WordPtr, data, Password, MaskMem, MaskAdr, MaskLen, MaskData, MaskFlag, Errorcode);
        }
    }

    public int WriteDataByTID(String TIDStr, byte Mem, byte WordPtr, byte[] Password, String wdata) {
        if (TIDStr.length() % 4 != 0) {
            return 255;
        } else if (wdata.length() % 4 != 0) {
            return 255;
        } else {
            byte ENum = -1;
            byte WNum = (byte)(wdata.length() / 4);
            byte[] EPC = new byte[12];
            byte[] data = this.reader.hexStringToBytes(wdata);
            byte[] TID = this.reader.hexStringToBytes(TIDStr);
            byte MaskMem = 2;
            byte[] MaskAdr = new byte[2];
            MaskAdr[0] = MaskAdr[1] = 0;
            byte MaskLen = (byte)(TIDStr.length() * 4);
            byte[] MaskData = new byte[TIDStr.length()];
            System.arraycopy(TID, 0, MaskData, 0, TID.length);
            byte MaskFlag = 1;
            byte[] Errorcode = new byte[1];
            return this.reader.WriteData_G2(this.param.GetAddress(), WNum, ENum, EPC, Mem, WordPtr, data, Password, MaskMem, MaskAdr, MaskLen, MaskData, MaskFlag, Errorcode);
        }
    }

    public int WriteEPC(String EPCStr, byte[] Password) {
        if (EPCStr.length() % 4 != 0) {
            return 255;
        } else {
            byte WNum = (byte)(EPCStr.length() / 4);
            byte[] Errorcode = new byte[1];
            byte[] data = this.reader.hexStringToBytes(EPCStr);
            return this.reader.WriteEPC_G2(this.param.GetAddress(), WNum, Password, data, Errorcode);
        }
    }

    public int WriteEPCByTID(String TIDStr, String EPCStr, byte[] Password) {
        if (TIDStr.length() % 4 != 0) {
            return 255;
        } else if (EPCStr.length() % 4 != 0) {
            return 255;
        } else {
            byte ENum = -1;
            byte WNum = (byte)(EPCStr.length() / 4);
            byte[] EPC = new byte[12];
            String PCStr = "";
            switch(WNum) {
                case 1:
                    PCStr = "0800";
                    break;
                case 2:
                    PCStr = "1000";
                    break;
                case 3:
                    PCStr = "1800";
                    break;
                case 4:
                    PCStr = "2000";
                    break;
                case 5:
                    PCStr = "2800";
                    break;
                case 6:
                    PCStr = "3000";
                    break;
                case 7:
                    PCStr = "3800";
                    break;
                case 8:
                    PCStr = "4000";
                    break;
                case 9:
                    PCStr = "4800";
                    break;
                case 10:
                    PCStr = "5000";
                    break;
                case 11:
                    PCStr = "5800";
                    break;
                case 12:
                    PCStr = "6000";
                    break;
                case 13:
                    PCStr = "6800";
                    break;
                case 14:
                    PCStr = "7000";
                    break;
                case 15:
                    PCStr = "7800";
                    break;
                case 16:
                    PCStr = "8000";
            }

            String wdata = PCStr + EPCStr;
            ++WNum;
            byte[] data = this.reader.hexStringToBytes(wdata);
            byte[] TID = this.reader.hexStringToBytes(TIDStr);
            byte MaskMem = 2;
            byte[] MaskAdr = new byte[2];
            MaskAdr[0] = MaskAdr[1] = 0;
            byte MaskLen = (byte)(TIDStr.length() * 4);
            byte[] MaskData = new byte[TIDStr.length()];
            System.arraycopy(TID, 0, MaskData, 0, TID.length);
            byte MaskFlag = 1;
            byte[] Errorcode = new byte[1];
            byte Mem = 1;
            byte WordPtr = 1;
            return this.reader.WriteData_G2(this.param.GetAddress(), WNum, ENum, EPC, Mem, WordPtr, data, Password, MaskMem, MaskAdr, MaskLen, MaskData, MaskFlag, Errorcode);
        }
    }

    public int Lock(String EPCStr, byte select, byte setprotect, String PasswordStr) {
        if (EPCStr.length() % 4 != 0) {
            return 255;
        } else if (PasswordStr.length() != 8) {
            return 255;
        } else {
            byte ENum = (byte)(EPCStr.length() / 4);
            byte[] EPC = this.reader.hexStringToBytes(EPCStr);
            byte[] Password = this.reader.hexStringToBytes(PasswordStr);
            byte[] Errorcode = new byte[1];
            return this.reader.Lock_G2(this.param.GetAddress(), ENum, EPC, select, setprotect, Password, Errorcode);
        }
    }

    public int Kill(String EPCStr, String PasswordStr) {
        if (EPCStr.length() % 4 != 0) {
            return 255;
        } else if (PasswordStr.length() != 8) {
            return 255;
        } else {
            byte ENum = (byte)(EPCStr.length() / 4);
            byte[] EPC = this.reader.hexStringToBytes(EPCStr);
            byte[] Password = this.reader.hexStringToBytes(PasswordStr);
            byte[] Errorcode = new byte[1];
            return this.reader.Kill_G2(this.param.GetAddress(), ENum, EPC, Password, Errorcode);
        }
    }

    public void SetCallBack(TagCallback callback) {
        this.reader.SetCallBack(callback);
    }

    public int StartRead() {
        if (this.mThread == null) {
            this.mWorking = true;
            this.mThread = new Thread(new Runnable() {
                public void run() {
                    byte Target = 0;

                    while(CReaderCopy.this.mWorking) {
                        byte Ant = 1;

                        for(int antindex = 0; antindex < 4 && CReaderCopy.this.mWorking; ++antindex) {
                            switch(antindex) {
                                case 0:
                                    if ((CReaderCopy.this.param.GetAntenna() & 1) == 1) {
                                        Ant = -128;
                                    }
                                    break;
                                case 1:
                                    if ((CReaderCopy.this.param.GetAntenna() & 2) == 2) {
                                        Ant = -127;
                                    }
                                    break;
                                case 2:
                                    if ((CReaderCopy.this.param.GetAntenna() & 4) == 4) {
                                        Ant = -126;
                                    }
                                    break;
                                case 3:
                                    if ((CReaderCopy.this.param.GetAntenna() & 8) == 8) {
                                        Ant = -125;
                                    }
                            }

                            int[] pOUcharTagNum = new int[1];
                            int[] pListLen = new int[1];
                            pOUcharTagNum[0] = pListLen[0] = 0;
                            if (CReaderCopy.this.param.GetSession() == 0 || CReaderCopy.this.param.GetSession() == 1) {
                                Target = 0;
                                CReaderCopy.this.NoCardCOunt = 0;
                            }

                            CReaderCopy.this.reader.Inventory_G2(CReaderCopy.this.param.GetAddress(), (byte)CReaderCopy.this.param.GetQValue(), (byte)CReaderCopy.this.param.GetSession(), (byte)CReaderCopy.this.param.GetTidPtr(), (byte)CReaderCopy.this.param.GetTidLen(), Target, Ant, (byte)CReaderCopy.this.param.GetScanTime(), CReaderCopy.this.pOUcharIDList, pOUcharTagNum, pListLen);
                            if (pOUcharTagNum[0] == 0) {
                                if (CReaderCopy.this.param.GetSession() > 1) {
                                    CReaderCopy var10000 = CReaderCopy.this;
                                    var10000.NoCardCOunt = var10000.NoCardCOunt + 1;
                                    if (CReaderCopy.this.NoCardCOunt > 7) {
                                        Target = (byte)(1 - Target);
                                        CReaderCopy.this.NoCardCOunt = 0;
                                    }
                                }
                            } else {
                                CReaderCopy.this.NoCardCOunt = 0;
                            }
                        }
                    }

                    CReaderCopy.this.mThread = null;
                }
            });
            this.mThread.start();
            return 0;
        } else {
            return 255;
        }
    }

    public void StopRead() {
        if (this.mThread != null) {
            this.mWorking = false;
        }

    }
}
