package com.phlx.anchorcollect.rfid;

import android.util.Log;

import com.phlx.anchorcollect.App;
import com.phlx.anchorcollect.event.EventKey;
import com.phlx.anchorcollect.event.EventMsg;
import com.phlx.anchorcollect.event.EventUtils;
import com.phlx.anchorcollect.util.ConstantUtil;
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;

    /**
     * 从给定的上下文中获取 ConnectManger
     *
     * @return ConnectManger 的一个实例
     */
    public static ConnectManger from() {
        if (connectManger == null) {
            synchronized (ConnectManger.class) {
                if (connectManger == null)
                    connectManger = new ConnectManger();
            }
        }
        return connectManger;
    }

    /**
     * 唯一的构造函数
     */
    private ConnectManger() {
    }


    /**
     * 初始化串口
     *
     * @return 0-打开串口成功，其他代表打开串口失败
     */
    public int initSerialPort(String port) {
        return App.getInstance().getLinkage().open_serial(port);
    }


    /**
     * 获取R2000/MagicRf模块版本
     */
    public String getModuleVersion() {

        String returnStr = "获取版本失败！";
        //获取软件版本,返回值不做成功与否判断
        if (connectManger.getSoftwareVersion() == 0) {
            //获取硬件版本
            if (connectManger.getHardwareVersion() == 0) {
                returnStr = "硬件版本号："+ConstantUtil.SERIAL_VERSION +"  |  "+"软件版本号："+ConstantUtil.VERSION;
//                switch (eventType) {
//                    case EventKey.KEY_PORT_COMPLETE:
//                        EventUtils.post(new EventMsg(EventKey.KEY_PORT_COMPLETE));
//                        break;
//                    case EventKey.KEY_BT_COMPLETE:
//                        EventUtils.post(new EventMsg(EventKey.KEY_BT_COMPLETE));
//                        break;
//                    case EventKey.KEY_BLE_COMPLETE:
//                        EventUtils.post(new EventMsg(EventKey.KEY_BLE_COMPLETE));
//                        break;
//                }
            }
        }

        return returnStr;
    }




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

    }

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


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

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

    /**
     * 获取模rm70xx连接的模块类型
     *
     * @return 硬件版本号
     */
    public String getRm70xxModule() {
        Rfid_Value rfid_value = new Rfid_Value();
        int status = App.getInstance().getLinkage().getBoardModuleType(rfid_value);
        if (status == 0) {
            if (rfid_value.value == 0) {
                ConstantUtil.RM70XX_R2000 = true;
                ConstantUtil.RM70XX_MagicRF = false;
                ConstantUtil.RM70XX_R2000Plus = false;
                return "R2000";
            } else if (rfid_value.value == 1) {
                ConstantUtil.RM70XX_MagicRF = true;
                ConstantUtil.RM70XX_R2000 = false;
                ConstantUtil.RM70XX_R2000Plus = false;
                return "MagicRF";
            } else {
                ConstantUtil.RM70XX_MagicRF = false;
                ConstantUtil.RM70XX_R2000 = false;
                ConstantUtil.RM70XX_R2000Plus = true;
                return "R2000Plus";
            }
        } else {
            return "" + status;
        }
    }


    /**
     * 1.全局标记当前模块类型为R2000
     * 2.设置sdk解析模式为R2000
     */
    public void setModuleR2000() {
        ConstantUtil.R2000 = true;
        ConstantUtil.R2000Plus = false;
        ConstantUtil.MagicRF = false;
        ConstantUtil.RM70XX = false;
        ConstantUtil.RM600X = false;
        ConstantUtil.RM601X = false;
        ConstantUtil.RM600XPlus = false;
        App.getInstance().getLinkage().setRFModuleType(0);
    }


    /**
     * 1.全局标记当前模块类型为MagicRF
     * 2.设置sdk解析模式为MagicRF
     */
    public void setModuleMagicRF() {
        ConstantUtil.MagicRF = true;
        ConstantUtil.R2000 = false;
        ConstantUtil.R2000Plus = false;
        ConstantUtil.RM70XX = false;
        ConstantUtil.RM600X = false;
        ConstantUtil.RM601X = false;
        ConstantUtil.RM600XPlus = false;
        App.getInstance().getLinkage().setRFModuleType(1);
    }

    /**
     * 1.全局标记当前模块类型为RM70XX
     * 2.设置sdk解析模式为RM70XX
     */
    public void setModuleRm70xx() {
        ConstantUtil.RM70XX = true;
        ConstantUtil.R2000Plus = false;
        ConstantUtil.MagicRF = false;
        ConstantUtil.R2000 = false;
        ConstantUtil.RM600X = false;
        ConstantUtil.RM601X = false;
        ConstantUtil.RM600XPlus = false;
        App.getInstance().getLinkage().setRFModuleType(2);
    }

    /**
     * 1.全局标记当前模块类型为MagicRF
     * 2.设置sdk解析模式为MagicRF
     */
    public void setModuleR2000Plus() {
        ConstantUtil.R2000Plus = true;
        ConstantUtil.MagicRF = false;
        ConstantUtil.R2000 = false;
        ConstantUtil.RM70XX = false;
        ConstantUtil.RM600X = false;
        ConstantUtil.RM601X = false;
        ConstantUtil.RM600XPlus = false;
        App.getInstance().getLinkage().setRFModuleType(3);
    }


    /**
     * 1.全局标记当前模块类型为RM600X
     * 2.设置sdk解析模式为RM600X
     */
    public void setModuleRM600X() {
        ConstantUtil.RM600X = true;
        ConstantUtil.R2000Plus = false;
        ConstantUtil.MagicRF = false;
        ConstantUtil.R2000 = false;
        ConstantUtil.RM70XX = false;
        ConstantUtil.RM601X = false;
        ConstantUtil.RM600XPlus = false;
        App.getInstance().getLinkage().setRFModuleType(4);
    }

    /**
     * 1.全局标记当前模块类型为MagicRF
     * 2.设置sdk解析模式为MagicRF
     */
    public void setModuleRM601X() {
        ConstantUtil.RM601X = true;
        ConstantUtil.R2000Plus = false;
        ConstantUtil.MagicRF = false;
        ConstantUtil.R2000 = false;
        ConstantUtil.RM70XX = false;
        ConstantUtil.RM600X = false;
        ConstantUtil.RM600XPlus = false;
        App.getInstance().getLinkage().setRFModuleType(5);
    }

    /**
     * 1.全局标记当前模块类型为MagicRF
     * 2.设置sdk解析模式为MagicRF
     */
    public void setModuleRM600XPlus() {
        ConstantUtil.RM600XPlus = true;
        ConstantUtil.R2000Plus = false;
        ConstantUtil.MagicRF = false;
        ConstantUtil.R2000 = false;
        ConstantUtil.RM70XX = false;
        ConstantUtil.RM600X = false;
        ConstantUtil.RM601X = false;
        App.getInstance().getLinkage().setRFModuleType(6);
    }


    public int setLabel6C() {
        ConstantUtil.Label_6C = true;
        ConstantUtil.Label_6B = false;
        ConstantUtil.Label_GB = false;
        ConstantUtil.Label_GJB = false;
        return App.getInstance().getLinkage().setTagType(0);
    }

    public int setLabel6B() {
        ConstantUtil.Label_6B = true;
        ConstantUtil.Label_6C = false;
        ConstantUtil.Label_GB = false;
        ConstantUtil.Label_GJB = false;
        return App.getInstance().getLinkage().setTagType(1);
    }

    public int setLabelGB() {
        ConstantUtil.Label_6B = false;
        ConstantUtil.Label_6C = false;
        ConstantUtil.Label_GB = true;
        ConstantUtil.Label_GJB = false;
        return App.getInstance().getLinkage().setTagType(3);
    }


    public int setLabelGJB() {
        ConstantUtil.Label_6B = false;
        ConstantUtil.Label_6C = false;
        ConstantUtil.Label_GB = false;
        ConstantUtil.Label_GJB = true;
        return App.getInstance().getLinkage().setTagType(2);
    }


}
