package com.hh.arome.services;

import android.app.Service;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

import androidx.annotation.Nullable;

import com.telpo.tps550.api.idcard.IdentityMsg;
import com.telpo.tps550.api.idcard.T2OReader;
import com.telpo.tps550.api.util.ReaderUtils;

import java.util.concurrent.CountDownLatch;

/**
 * 身份证读卡service
 */
public class IDReaderService extends Service {

    private final static String TAG = "IDReaderService";
    // 读取状态
    private boolean reading = false;
    private T2OReader t2oReader;
    private CountDownLatch countDownLatch = null;

    private LocalBinder binder = new LocalBinder();

    /**
     * 创建Binder对象，返回给客户端即Activity使用，提供数据交换的接口
     */
    public class LocalBinder extends Binder {
        // 声明一个方法，getService。（提供给客户端调用）
        public IDReaderService getService() {
            // 返回当前对象LocalService,这样我们就可在客户端端调用Service的公共方法了
            return IDReaderService.this;
        }
    }

    /**
     * 绑定服务时才会调用
     * 必须要实现的方法, 把Binder类返回给客户端
     * @param intent
     * @return
     */
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return binder;
    }

    /**
     * 解除绑定时调用
     * @return
     */
    @Override
    public boolean onUnbind(Intent intent) {
        Log.i(TAG, "Service is invoke onUnbind");
        return super.onUnbind(intent);
    }

    /**
     * 首次创建服务时，系统将调用此方法来执行一次性设置程序（在调用 onStartCommand() 或 onBind() 之前）。
     * 如果服务已在运行，则不会调用此方法。该方法只被调用一次
     */
    @Override
    public void onCreate() {
        super.onCreate();
        Log.i(TAG, "Service is invoke Created");
        t2oReader = new T2OReader();
        countDownLatch = new CountDownLatch(1);
        read();
    }

    private void read() {
        if(t2oReader.isUSBReader(this)){
            t2oReader.openReader(this);
        }
        new Thread(new Runnable() {
            @Override
            public void run() {
                reading = true;
                while(reading) {
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    IdentityMsg msg = t2oReader.checkIDCard();
                    if(msg != null){
                        Bitmap bitmap = t2oReader.decodeIDImage(msg.getHead_photo());
                        String name = msg.getName();
                        String sex = msg.getSex();
                        String nation = msg.getNation();
                        String born = msg.getBorn();
                        String address = msg.getAddress();
                        String apartment = msg.getApartment();
                        String period = msg.getPeriod();
                        String no = msg.getNo();
                        String finger = ReaderUtils.get_finger_info(IDReaderService.this, t2oReader.getIDFinger(msg));

                    }
                }
                countDownLatch.countDown();
            }
        }).start();
    }

    /**
     * 每次通过startService()方法启动Service时都会被回调。
     * @param intent
     * @param flags
     * @param startId
     * @return
     */
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i(TAG, "onStartCommand invoke");
        return super.onStartCommand(intent, flags, startId);
    }

    /**
     * 服务销毁时的回调
     */
    @Override
    public void onDestroy() {
        Log.i(TAG, "Service is invoke Destroyed");
        reading = false;
        t2oReader.closeReader();
        super.onDestroy();
    }

}
