package com.phlx.anchor_collect_flutter;

import android.util.Log;

import com.uhf.linkage.Linkage;
import com.uhf.structures.Rfid_Value;

/**
 * <pre>
 *     author : lei
 *     e-mail : 18600395998@163.com
 *     time   : 2019/09/03
 *     desc   :
 *     version: 1.0
 * </pre>
 */
public class ConnectManger {

    private static ConnectManger connectManger;

    private Linkage mLinkage;

    public static String RM70XX_SERIAL_VERSION = "";
    public static String RM70XX_VERSION = "";
    public static String SERIAL_VERSION = "";
    public static String VERSION = "";

    public static boolean isSucc = false;

    /**
     * 从给定的上下文中获取 ConnectManger
     *
     * @return ConnectManger 的一个实例
     */
    public static ConnectManger from(Linkage linkage) {

        if (connectManger == null) {
            synchronized (ConnectManger.class) {
                if (connectManger == null) connectManger = new ConnectManger(linkage);
            }
        }
        return connectManger;
    }

    /**
     * 唯一的构造函数
     */
    private ConnectManger(Linkage linkage) {
        mLinkage = linkage;
    }


    /**
     * 获取RM70XX版本号
     */
    public String getRm70xxVersion() {

        if (getBoardSerialNumber() == 0) {
            if (getBoardSoftVersion() == 0) {
                if (getSoftwareVersion() == 0) {
                    if (getHardwareVersion() == 0) {
                        Log.e("lei", "getRm70xxVersion: "+String.format("设备连接:%s" + "\n软件版本:%s" + "\n硬件版本:%s" + "\n设备软件版本:%s" + "\n设备硬件版本:%s", "蓝牙已连接",
                                RM70XX_VERSION, RM70XX_SERIAL_VERSION, VERSION, SERIAL_VERSION));
                        if (!RM70XX_VERSION.equals("-2") && !RM70XX_SERIAL_VERSION.equals("-2") && !VERSION.equals("-1") && !SERIAL_VERSION.equals("-1")) {
                            isSucc = true;
                            return String.format("设备连接:%s" + "\n软件版本:%s" + "\n硬件版本:%s" + "\n设备软件版本:%s" + "\n设备硬件版本:%s", "蓝牙已连接",
                                    RM70XX_VERSION, RM70XX_SERIAL_VERSION, VERSION, SERIAL_VERSION);
                        } else {
                            isSucc = false;
                            return "设备连接失败";
                        }
                    }
                }
            }
        }
        return "设备连接失败";
    }


    /**
     * 获取模块软件版本方法
     *
     * @return 软件版本号
     */
    public int getSoftwareVersion() {
        Rfid_Value version_value = new Rfid_Value();
        byte[] versionData = mLinkage.getVersion(version_value);
        if (version_value.value == 0) {
            VERSION = new String(versionData);
        } else {
            VERSION = ""+version_value.value;
        }
        return 0;

    }

    /**
     * 获取模块硬件版本方法
     *
     * @return 硬件版本号
     */
    public int getHardwareVersion() {
        Rfid_Value serial_value = new Rfid_Value();
        byte[] serialData = mLinkage.getSerialNumber(serial_value);
        if (serial_value.value == 0) {
            SERIAL_VERSION = new String(serialData);
        } else {
            SERIAL_VERSION = ""+serial_value.value;
        }
        return 0;
    }


    /**
     * 获取rm70xx软件版本方法
     *
     * @return 软件版本号
     */
    public int getBoardSoftVersion() {
        Rfid_Value version_value = new Rfid_Value();
        byte[] versionData = mLinkage.getBoardSoftVersion(version_value);
        if (version_value.value == 0) {
            RM70XX_VERSION = new String(versionData);
        } else {
            RM70XX_VERSION = ""+version_value.value;
        }
        return 0;
    }

    /**
     * 获取模rm70xx硬件版本方法
     *
     * @return 硬件版本号
     */
    public int getBoardSerialNumber() {
        Rfid_Value serial_value = new Rfid_Value();
        byte[] serialData = mLinkage.getBoardSerialNumber(serial_value);
        if (serial_value.value == 0 && serialData != null) {
            RM70XX_SERIAL_VERSION = new String(serialData);
        } else {
            RM70XX_SERIAL_VERSION = ""+serial_value.value;
        }
        return 0;
    }


}
