package com.example.writeepc;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.example.writeepc.databinding.ActivityMainBinding;
import com.handheld.uhfr.UHFRManager;
import com.uhf.api.cls.Reader;

import java.util.List;

import cn.pda.serialport.Tools;

public class MainActivity extends AppCompatActivity {
    private UHFRManager uhfrManager;
    private InfoData infoData;
    private byte[] accesspwd = Tools.HexString2Bytes("00000000");
    private short timeOut=1000;
    private ActivityMainBinding binding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        uhfrManager = UHFRManager.getInstance();
        // uhfrManager.asyncStartReading();
        uhfrManager.setGen2session(false);
        infoData = new InfoData();
        // infoData.setPower(17);
        binding.selectPower.setSelection(17);
        binding.setData(infoData);
        binding.epcView.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                infoData.setWriteEpc(ASCIIUtil.str2Hex(s.toString()));
            }
        });
    }


    public void setPower(View view) {
        int power = Integer.parseInt(binding.selectPower.getSelectedItem().toString());
        // if (power == 0) return;
        Reader.READER_ERR readerErr = uhfrManager.setPower(power, power);
        Toast.makeText(this, readerErr.name(), Toast.LENGTH_SHORT).show();
    }


    // 1- 读取TID
    public void readTid(View view) {
        byte[] rdata = new byte[12];
        Reader.READER_ERR readerErr = uhfrManager.getTagData(2, 0, 6, rdata, accesspwd, timeOut);
        Log.e("TAG", "readTid:" + readerErr.name());
        if (readerErr == Reader.READER_ERR.MT_OK_ERR) {
            infoData.setReadTId(Tools.Bytes2HexString(rdata, 12));
            infoData.setTidBytes(rdata);
            // infoData.setEpc(null);
            infoData.setReadAscii(null);
        } else {
            infoData.setReadTId(null);
            infoData.setTidBytes(null);
            Toast.makeText(this, "读取失败", Toast.LENGTH_SHORT).show();
        }


    }

    // 2-写入
    public void writeEpc(View view) {
        // infoData.setReadEpc(null);
        // infoData.setReadAscii(null);
        // Reader.READER_ERR readerErr = uhfrManager.writeTagEPC(Tools.HexString2Bytes(infoData.getWriteEpc()),
        char tagI='1';
        String writeEpc = infoData.getWriteEpc();
        if (writeEpc==null||writeEpc.isEmpty()) {
            return;
        }
        byte[] data = Tools.HexString2Bytes(writeEpc);

        Reader.READER_ERR readerErr = uhfrManager.writeTagDataByFilter((char) 1,2,
                data,data.length,
                accesspwd, timeOut,
                infoData.getTidBytes(), 2, 0, true);
        if (readerErr == Reader.READER_ERR.MT_OK_ERR) {
            Toast.makeText(this, "写入成功", Toast.LENGTH_SHORT).show();
            infoData.setReadAscii(null);
            // infoData.setReadEpc(null);
            return;
        }
        Toast.makeText(this, "错误信息为："+readerErr.name(), Toast.LENGTH_SHORT).show();

    }

    public void readEpc(View view) {
        byte[] rdata  = uhfrManager.getTagDataByFilter(1,2,6,accesspwd,timeOut,
                infoData.getTidBytes(), 2,0,true);
        if (rdata!=null) {
            String epc=Tools.Bytes2HexString(rdata,rdata.length);
            infoData.setReadAscii(epc);
            String readEpc = ASCIIUtil.hex2Str(epc);
            infoData.setReadEpc(readEpc);
            if (readEpc.equals(infoData.getWriteEpc())){
                Toast.makeText(this, "读取 写入一致", Toast.LENGTH_SHORT).show();
            }
        }else {
            infoData.setReadAscii(null);
            // infoData.setReadEpc(null);
        }
        /*List<Reader.TAGINFO> strings = uhfrManager.tagInventoryRealTime();
        if (strings!=null && !strings.isEmpty()){
            Reader.TAGINFO taginfo = strings.get(0);
            byte[] epcId = taginfo.EpcId;
            String epc= Tools.Bytes2HexString(epcId,epcId.length);
            infoData.setReadAscii(epc);
            infoData.setReadEpc(ASCIIUtil.hex2Str(epc));

            // infoData.settId(taginfo);

        }*/
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // uhfrManager.asyncStopReading();
        uhfrManager.close();
        uhfrManager = null;
    }
}