package com.example.writeepc;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;

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

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

import java.util.ArrayList;
import java.util.List;

import cn.pda.serialport.Tools;

public class MoreActivity extends AppCompatActivity {
    private final byte[] accesspwd = Tools.HexString2Bytes("00000000");
    private UHFRManager uhfrManager;
    private KeyReceiver keyReceiver;
    private boolean aBoolean;
    private ActivityMoreBinding binding;
    private MyAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = DataBindingUtil.setContentView(this, R.layout.activity_more);
        adapter = new MyAdapter(this);
        binding.setAdapter(adapter);
        uhfrManager = UHFRManager.getInstance();
        registerReceiver();
    }

    private void registerReceiver() {
        this.keyReceiver = new KeyReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction("android.rfid.FUN_KEY");
        filter.addAction("android.intent.action.FUN_KEY");
        registerReceiver(this.keyReceiver, filter);
    }

    private void unregisterReceiver() {
        unregisterReceiver(this.keyReceiver);
    }

    @Override
    protected void onResume() {
        super.onResume();
        uhfrManager.setGen2session(true);
        uhfrManager.asyncStartReading();
    }

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

    public class KeyReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            boolean keydown = intent.getBooleanExtra("keydown", false);
            // byte[] rdata = new byte[12];
            if (keydown) {
                // short timeOut = 500;
                // Reader.READER_ERR readerErr = //uhfrManager.getTagData(1, 2, 6, rdata, accesspwd, timeOut);
                //         uhfrManager.
                // Log.e("TAG", "readTid:" + readerErr.name());
                // if (readerErr == Reader.READER_ERR.MT_OK_ERR) {
                //     String text = Tools.Bytes2HexString(rdata, 12);
                //     binding.moreEpc.setText(ASCIIUtil.hex2Str(text));
                //
                Log.e("TAG", "onReceive:" + "触发");
                // }
                List<Reader.TAGINFO> strings = uhfrManager.tagInventoryRealTime();
                if (strings == null || strings.isEmpty()) {
                    return;
                }

                List<String> list = new ArrayList<>(strings.size());
                strings.forEach(taginfo -> {

                    String epc = ASCIIUtil.hex2Str(Tools.Bytes2HexString(taginfo.EpcId, taginfo.EpcId.length));
                    Log.e("TAG", "onReceive:" + epc);
                    list.add(epc);
                });
                Log.e("TAG", "onReceive:" + list.size());
                adapter.setList(list);
                adapter.notifyDataSetChanged();

            }
        }
    }
}

