package com.example.jinyu_rfid.rfid;

import android.util.Log;

import com.example.jinyu_rfid.callback.DataReturnCall;
import com.handheld.uhfr.UHFRManager;
import com.uhf.api.cls.Reader;

import java.util.List;

import cn.pda.serialport.Tools;

/**
 * @author wanghao
 * @date 2024/5/16  15:12
 */
public class C5106Device implements RFIDModel {
    private UHFRManager uhfrManager;
    private DataReturnCall call;
    public byte[] accessBytes = Tools.HexString2Bytes("00000000");

    public C5106Device(DataReturnCall call) {
        uhfrManager = UHFRManager.getInstance();
        this.call = call;
    }


    @Override
    public void sanEpc(int len) {
        byte[] rdata = new byte[len * 2];
        Reader.READER_ERR readerErr = uhfrManager.getTagData(1, 2, len, rdata, accessBytes, (short) 200);
        call.readEpcCodeInfo(Tools.Bytes2HexString(rdata, rdata.length), readerErr == Reader.READER_ERR.MT_OK_ERR, readerErr.toString());
    }


    public boolean writeUser() {
        return false;
    }

    /**
     * int mbank, 写入标签的区域，0:RESERVED区，1:EPC区，2:TID区，3:USER区
     * int startaddress, 写入标签的起始地址，单位word
     * byte[] data, 写入的数据
     * int datalen, 写入标签数据的长度，单位word
     * byte[] password, 访问密码，4byte数据，
     * short timeout，读取的超时时间，单位：毫秒
     ————————————————————————————————————————————————————
     * byte[] fdata, 过滤的数据
     * int fbank, 过滤数据的区域 1 :EPC区,2: TID区 ,3: USER区
     * int fstartaddr, 数据开始地址，单位word
     * boolean matching 是否匹配，true:写与过滤数据相匹配的标签,false:写与过滤数据不相匹配的标签
     */
    @Override
    public boolean writeUser(String writeStr,String tagEpc) {
        var bytes = Tools.HexString2Bytes(writeStr);
        var bytesF = Tools.HexString2Bytes(tagEpc);
        Reader.READER_ERR readerErr = uhfrManager.writeTagDataByFilter(
                (char) 3, 0, bytes, bytes.length, accessBytes, (short) 200,
                bytesF ,1,2,true);
        Log.e("TAG", "writeUser:" + readerErr.toString());
        return readerErr == Reader.READER_ERR.MT_OK_ERR;
    }
}
