package com.ivrjack.ru01.demo;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.widget.Toast;

import com.ivrjack.ru01.IvrJackService;

/**
 * Created by ChenRong02 on 2016-04-29.
 */
public class TagScanActivity extends Activity {

    private IvrJackService service;
    private ProgressDialog dialog;
    private View viewTagScan;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tag_scan);
        service = MainActivity.service;

        viewTagScan = findViewById(R.id.tag_scan);
        viewTagScan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onTagScan(v);
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    private void showToast(String message) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }

    public void onTagScan(View v) {
        dialog = ProgressDialog.show(this, "", "Reading tag...", true);
        new Thread() {
            @Override
            public void run() {
                final IvrJackService.TagEpcAndTid tagEpcAndTid = new IvrJackService.TagEpcAndTid();
                final int ret1 = service.readEpcAndTid((byte)0, tagEpcAndTid);
                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {
                        if(ret1 == 0){
                            final int ret2 = service.selectTag((byte)0, tagEpcAndTid.epc);
                            new Handler(Looper.getMainLooper()).post(new Runnable() {
                                @Override
                                public void run() {
                                    if(ret2 == 0){
                                        final IvrJackService.TagBlock tagBlock = new IvrJackService.TagBlock();
                                        final int ret3 = service.readTag((byte)0, (short) 32, (byte)1, tagBlock);
                                        new Handler(Looper.getMainLooper()).post(new Runnable() {
                                            @Override
                                            public void run() {
                                                if(ret3 == 0){
                                                    byte[] data = tagBlock.data;
                                                    if (data[0] == (byte)0x00 && data[1] == (byte)0x40) {
                                                        showTamper();
                                                    }else if((data[0] == (byte)0x40 || data[0] == (byte)0x80 || data[0] == (byte)0xC0)
                                                            && data[1] == (byte)0x40){
                                                        showNoTamper();
                                                    }else{
                                                        showToast("Read tag failed: " + "none supported");
                                                    }
                                                }else{
                                                    showToast("Read tag failed: " + ret3);
                                                }
                                            }
                                        });
                                    }else{
                                        showToast("select tag failed:" + ret2);
                                    }
                                }
                            });
                        }else{
                            showToast("Find tag failed: " + ret1);
                        }
                        dialog.dismiss();
                    }
                });

                // 连续读4次，只要有3次读到0040即可判断已拆封
//                IvrJackService.TagBlock tagBlock = new IvrJackService.TagBlock();
//                int tamperCount = 0;
//                int ret = 0;
//                for (int i=0; i<4; i++) {
//                    //读单地址
//                    ret = service.readTag((byte)0, (byte)32, (byte)1, tagBlock);
//                    //读双地址
////                    ret = service.readTag((byte)0, new byte[]{(byte)0, (byte)32}, (byte)1, tagBlock);
//                    if (ret != 0) break;
//                    byte[] data = tagBlock.data;
//                    if (data[0] == (byte)0x00 && data[1] == (byte)0x40) {
//                        tamperCount++;
//                    }
//                    if (tamperCount == 3) break;
//                }
//                final int tamperCount2 = tamperCount;
//                final int ret2 = ret;
//                runOnUiThread(new Runnable() {
//                    @Override
//                    public void run() {
//                        if (ret2 != 0) {
//                            showToast("Read tag failed: " + ret2);
//                        } else {
//                            if (tamperCount2 == 3) {
//                                showTamper();
//                            } else {
//                                showNoTamper();
//                            }
//                        }
//                        dialog.dismiss();
//                    }
//                });

//                final IvrJackService.TagBlock tagBlock = new IvrJackService.TagBlock();
//                final int ret = service.readTag((byte)0, (byte)32, (byte)1, tagBlock);
//                runOnUiThread(new Runnable() {
//                    @Override
//                    public void run() {
//                        if (ret != 0) {
//                            showToast("Read tag failed: " + ret);
//                        } else {
//                            byte[] data = tagBlock.data;
//                            if (data.lengthArr == 2 && data[0] == (byte)0x00 && data[1] == (byte)0x40) {
//                                showTamper();
//                            } else {
//                                showNoTamper();
//                            }
//                        }
//                        dialog.dismiss();
//                    }
//                });
            }
        }.start();
    }

    public void onContinue(View v){
        viewTagScan.setBackgroundResource(R.drawable.scan_no_logo);
        viewTagScan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onTagScan(v);
            }
        });
    }

    public void showNoTamper() {
        viewTagScan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onContinue(v);
            }
        });
        viewTagScan.setBackgroundResource(R.drawable.xm05);
    }

    public void showTamper() {
        viewTagScan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onContinue(v);
            }
        });
        viewTagScan.setBackgroundResource(R.drawable.xm06);
    }

}
