package com.example.beijing_daxing;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ObservableBoolean;
import androidx.databinding.ViewDataBinding;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Toast;

import com.example.beijing_daxing.adapter.StoreCheckAdapter;
import com.example.beijing_daxing.base.BaseActivity;
import com.example.beijing_daxing.been.RFIDLocation;
import com.example.beijing_daxing.been.Stock;
import com.example.beijing_daxing.databinding.ActivityOffLineCheckBinding;
import com.uhf.api.cls.Reader;

import org.litepal.LitePal;

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

public class OffLineCheckActivity extends BaseActivity {
    private StoreCheckAdapter checkAdapter;
    private ObservableBoolean checkState;
    private List<Stock> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityOffLineCheckBinding binding;
        binding = DataBindingUtil.setContentView(this, R.layout.activity_off_line_check);
        checkState = new ObservableBoolean();
        binding.setCheckState(checkState);
      /*  var all = LitePal.findAll(RFIDLocation.class);
        if (all == null || all.isEmpty()) {
            Toast.makeText(this, "", Toast.LENGTH_SHORT).show();
            return;
        }*/
        list = new ArrayList<>();
        // all.forEach(t -> {
        //     Stock stock = new Stock();
        //     stock.setLocationCode(t.getLocationCode());
        //     stock.setEpcCode(t.getEpcCode());
        //     list.add(stock);
        // });
        checkAdapter=new StoreCheckAdapter(this);
        checkAdapter.setList(list);
        checkAdapter = new StoreCheckAdapter(this);
        binding.setAdapter(checkAdapter);
    }

    @Override
    protected void sanRfid(List<Reader.TAGINFO> epcs) {

    }

    public void offCheckRead(View view) {
        var b = checkState.get();
        if (b) {
            handler.removeCallbacks(runnable);
        } else {
            handler.postDelayed(runnable, 0);
        }
        checkState.set(!b);
    }

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
        }
    };
    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            // sendBroadcast(broadcastIntent);
            sanEpc();
            handler.postDelayed(runnable, 1000);
        }
    };
}