package com.phlx.anchorcollect.rfid;


import android.util.Log;

import com.phlx.anchorcollect.App;
import com.phlx.anchorcollect.Configs;
import com.uhf.structures.Rfid_Value;

import java.util.ArrayList;
import java.util.List;

import me.goldze.mvvmhabit.utils.SPUtils;
import me.goldze.mvvmhabit.utils.StringUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;

public class ModuleManager {

    private static String serialPort = "/dev/ttyMT1";

    final static String RM_SD100T_MT1 = "RM_SD100T_MT1";
    final static String RM_SD1006762_S0 = "RM_SD1006762_S0";
    final static String RM_SD1006762_S1 = "RM_SD1006762_S1";

    final static List<String> powerList = new ArrayList<String>() {
        {
            add(RM_SD100T_MT1);
            add(RM_SD1006762_S0);
            add(RM_SD1006762_S1);
        }

    };

    public static boolean initPower() {

        String powerOnType = SPUtils.getInstance().getString(Configs.SP_POWER_ON_TYPE, "");
//        powerOnType = RM_SD100T_MT1;
        if (!StringUtils.isEmpty(powerOnType)) {
            return powerOn(powerOnType);
        } else {//没存类型就便利所有上电
            for (int i = 0; i < powerList.size(); i++) {
                if (powerOn(powerList.get(i))) {
                    SPUtils.getInstance().put(Configs.SP_POWER_ON_TYPE, powerList.get(i));
                    return true;
                }
            }
        }
        return false;
    }

    //上电
    public static boolean powerOn(String powerOnType) {

        String portStr = powerOnType.substring(powerOnType.lastIndexOf("_") + 1, powerOnType.length());
        serialPort = "/dev/tty" + portStr;
        boolean powerOnStatus = false;
        if (App.getInstance().getLinkage().open_serial(serialPort) == 0) {
            DeviceControl DevCtrl = new DeviceControl();

            switch (powerOnType) {
                case RM_SD100T_MT1:
                    DevCtrl.PowerOn_pad_RM_SD100T_MT1();
                    break;
                case RM_SD1006762_S0:
                    DevCtrl.PowerOn_pad_RM_SD1006762_S0();
                    break;
                case RM_SD1006762_S1:
                    DevCtrl.PowerOn_pad_RM_SD1006762_S1();
                    break;
            }
            powerOnStatus = openCom(powerOnType);
        }
        Log.e("rfid","serialPort:"+serialPort+"|"+powerOnStatus);
        return powerOnStatus;
    }

    //设置模块类型
    public static boolean openCom(String powerOnType) {

        boolean openComStatus = false;
        Rfid_Value rf = new Rfid_Value();
        int moduleType = SPUtils.getInstance().getInt(Configs.SP_MODULE_TYPE, -1);
        if (moduleType == -1) {//没有存就便利所有

            rf = new Rfid_Value();
            App.getInstance().getLinkage().setRFModuleType(1);//R2000 0,MagicRF 1,RM70xx 2, R2000plus 3
            App.getInstance().getLinkage().getVersion(rf);
            if (rf.value == 0) {
                SPUtils.getInstance().put(Configs.SP_MODULE_TYPE, 1);
                SPUtils.getInstance().put(Configs.SP_POWER_ON_TYPE, powerOnType);
                return true;
            }
            Log.e("rfid","getVersion:MagicRF｜"+rf.value);

            App.getInstance().getLinkage().setRFModuleType(0);//R2000 0,MagicRF 1,RM70xx 2, R2000plus 3
            App.getInstance().getLinkage().getVersion(rf);
            if (rf.value == 0) {
                SPUtils.getInstance().put(Configs.SP_MODULE_TYPE, 0);
                SPUtils.getInstance().put(Configs.SP_POWER_ON_TYPE, powerOnType);
                return true;
            }
            Log.e("rfid","getVersion:R2000｜"+rf.value);

            rf = new Rfid_Value();
            App.getInstance().getLinkage().setRFModuleType(3);//R2000 0,MagicRF 1,RM70xx 2, R2000plus 3
            App.getInstance().getLinkage().getVersion(rf);
            if (rf.value == 0) {
                SPUtils.getInstance().put(Configs.SP_MODULE_TYPE, 3);
                SPUtils.getInstance().put(Configs.SP_POWER_ON_TYPE, powerOnType);
                return true;
            }
            Log.e("rfid","getVersion:R2000plus｜"+rf.value);
            //失败后要关闭串口
            App.getInstance().getLinkage().close_serial();

        } else {
            App.getInstance().getLinkage().setRFModuleType(moduleType);//R2000 0,MagicRF 1,RM70xx 2, R2000plus 3
            App.getInstance().getLinkage().getVersion(rf);
            openComStatus = rf.value == 0;
        }
        return openComStatus;
    }


    public static void destroyLibSO() {
        App.getInstance().getLinkage().close_serial();
        powerOff();
        App.getInstance().getLinkage().deinitRFID();
    }


    public static void powerOff() {

        String powerOnType = SPUtils.getInstance().getString(Configs.SP_POWER_ON_TYPE, "");
        DeviceControl DevCtrl = new DeviceControl();

        switch (powerOnType) {

            case RM_SD100T_MT1:
                DevCtrl.PowerOff_pad_RM_SD100T_MT1();
                break;
            case RM_SD1006762_S0:
                DevCtrl.PowerOff_pad_RM_SD1006762_S0();
                break;
            case RM_SD1006762_S1:
                DevCtrl.PowerOff_pad_RM_SD1006762_S1();
                break;

            default://如果没有存类型，就关闭所有类型
                DevCtrl.PowerOff_pad_RM_SD100T_MT1();
                DevCtrl.PowerOff_pad_RM_SD1006762_S0();
                DevCtrl.PowerOff_pad_RM_SD1006762_S1();
                break;

        }
    }

}
