package com.alanwyf.huashi100u;

/**
 * Created by alan on 2017/8/7.
 */

import android.content.ComponentName;

import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;

import android.app.Service;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;

import com.facebook.react.bridge.WritableMap;
import com.facebook.react.bridge.WritableNativeMap;
import com.huashi.otg.sdk.HsOtgService;
import com.huashi.otg.sdk.HSInterface;

public class HsOtgModule extends ReactContextBaseJavaModule {
    MyConn conn;
    HSInterface HSinterface;
    Intent service;
    Promise mPromise;
    private static final String TAG = "test";

    public HsOtgModule (ReactApplicationContext reactApplicationContext){
        super(reactApplicationContext);
    }

    @Override
    public String getName() {
        return "HsOtg";
    }

    @ReactMethod
    public void init(Promise promise) {
        mPromise = promise;
        service = new Intent(getReactApplicationContext(), HsOtgService.class);
        conn = new MyConn();
        boolean bindResult = getReactApplicationContext().bindService(service, conn, Service.BIND_AUTO_CREATE);
        if(!bindResult){
            mPromise.reject("-1", "init failed");
        }
    }

    @ReactMethod
    public void auth(Promise promise) {
        if (HSinterface == null){
            promise.reject("-1", "init failed");
            return;
        }
        int ret = HSinterface.Authenticate();
        WritableMap result = new WritableNativeMap();
        if (ret == 1){
            result.putString("code", ret+"");
            result.putString("msg", "success");
            promise.resolve(result);
        }else if (ret == 2){
            result.putString("code", ret+"");
            result.putString("msg", "auth failed");
            promise.resolve(result);
        }else if (ret == 0){
            promise.reject("0", "connect failed");
        }
    }

    @ReactMethod
    public void read(Promise promise) {
        if (HSinterface == null){
            promise.reject("-1", "init failed");
            return;
        }
        int ret = HSinterface.ReadCard();
        if (ret == 1){
            //成功
            byte[] fp = new byte[1024];
            fp = HsOtgService.ic.getFpDate();
            String m_FristPFInfo = "";
            String m_SecondPFInfo = "";
            WritableMap result = new WritableNativeMap();
            result.putString("code", ret+"");
            if (fp[4] == (byte)0x01) {
                m_FristPFInfo = GetFPcode(fp[5])+","+fp[6];
//                m_FristPFInfo = String.format("指纹  信息:第一枚指纹注册成功。指位:%s。指纹质量:%d \n", GetFPcode(fp[5]), fp[6]);
            } else {
                m_FristPFInfo = "";
            }
            if (fp[512 + 4] == (byte)0x01) {
                m_SecondPFInfo = GetFPcode(fp[512 + 5])+","+fp[512 + 6];
//                m_SecondPFInfo = String.format("指纹  信息:第二枚指纹注册成功。指位:%s。指纹质量:%d \n", GetFPcode(fp[512 + 5]),
//                        fp[512 + 6]);
            } else {
                m_SecondPFInfo = "";
            }

            result.putString("firstFP", m_FristPFInfo);
            result.putString("secondFP", m_SecondPFInfo);
            result.putString("name", HsOtgService.ic.getPeopleName());
            result.putString("sex", HsOtgService.ic.getSex());
            result.putString("people", HsOtgService.ic.getPeople());
            result.putString("birthday", HsOtgService.ic.getBirthDay().toString());
            result.putString("address", HsOtgService.ic.getAddr());
            result.putString("id", HsOtgService.ic.getIDCard());
            result.putString("department", HsOtgService.ic.getDepartment());
            result.putString("startDate", HsOtgService.ic.getStrartDate());
            result.putString("endDate", HsOtgService.ic.getEndDate());
            promise.resolve(result);
        }else {
            //失败
            promise.reject("0", "read failed");
        }
    }

    class MyConn implements ServiceConnection {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            HSinterface = (HSInterface) service;
            int i = 2;
            while (i > 0) {
                i--;
                int ret = HSinterface.init();
                if (ret == 1) {
                    i = 0;
                    WritableMap result = new WritableNativeMap();
                    String sam = HSinterface.GetSAM();
                    result.putString("code", "1");
                    result.putString("sam", sam);
                    mPromise.resolve(result);
                    return;
                } else {
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                        mPromise.reject("0", "connect failed");
                    }
                    continue;
                }
            }
            mPromise.reject("0", "connect failed");
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            conn = null;
            HSinterface = null;
            mPromise.reject("0", "connect failed");
        }
    }

    /**
     * 指纹 指位代码
     *
     * @param FPcode
     * @return
     */
    String GetFPcode(int FPcode) {
        switch (FPcode) {
            case 11:
                return "右手拇指";
            case 12:
                return "右手食指";
            case 13:
                return "右手中指";
            case 14:
                return "右手环指";
            case 15:
                return "右手小指";
            case 16:
                return "左手拇指";
            case 17:
                return "左手食指";
            case 18:
                return "左手中指";
            case 19:
                return "左手环指";
            case 20:
                return "左手小指";
            case 97:
                return "右手不确定指位";
            case 98:
                return "左手不确定指位";
            case 99:
                return "其他不确定指位";
            default:
                return "未知";
        }
    }
}