package com.wdkl.app.ncs.callingbed.helper;

import com.wdkl.ncs.android.middleware.model.dto.NurseConfigDto;

import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import serialporttest.utils.SerialPortUtil;
import serialporttest.utils.StringUtils;

public class SerialPortHelper {
    private static int brightnessPercent;
    private static ArrayList<NurseConfigDto> nurseConfigs;
    private static boolean dismissLed;

    private final static Object lockObject = new Object();
    private final static ExecutorService threadPool = Executors.newSingleThreadExecutor();
    private static NurseLedRunnable ledRunnable;

    //MIC设置
    public static void setHandsMIC(boolean isHandsMIC) {
        if (isHandsMIC) {
            //打开手柄mic
            SerialPortUtil.getInstance().sendCommand(SerialPortUtil.MIC, "0", "F");
        } else {
            //打开面板mic
            SerialPortUtil.getInstance().sendCommand(SerialPortUtil.MIC, "1", "F");
        }
    }

    /**
     * 设置卫生间紧急按钮灯状态: 0关闭1打开2闪烁
     */
    public static void setSosLight(String state) {
        SerialPortUtil.getInstance().sendCommand(SerialPortUtil.ULED, state, "F");
    }

    //重置设备
    public static void resetDevice() {
        SerialPortUtil.getInstance().sendCommand(SerialPortUtil.NET_STATUS , "1", "F");
    }

    /**
     * 门灯控制开关
     *
     * @param data  0  表示灯灭   1  表示灯亮
     * @param color 0 表示灯灭  非0表示灯的颜色
     */
    public static void setDoorLight(int data, String color) {
        String command;
        if (data == 0 || color == null) {
            command = "000";
        } else {
            command = color;
        }
        SerialPortUtil.getInstance().sendCommand(SerialPortUtil.DOORLIGHT, command, "F");
    }

    /**
     * 设置护理灯
     */
    public static void setNurseLedLight(final int brightnessPercent, final String oneColor, final String twoColor, final String threeColor, final String fourColor, final String fiveColor) {
        //需要在子线程中设置
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    float p = (float) brightnessPercent / 100;
                    if (StringUtils.notEmpty(oneColor)) {
                        int rr = (int) ((StringUtils.parseFloat(StringUtils.substringByLengh(oneColor, 0, 2))) * p);
                        int gg = (int) ((StringUtils.parseFloat(StringUtils.substringByLengh(oneColor, 2, 4))) * p);
                        int bb = (int) ((StringUtils.parseFloat(StringUtils.substringByLengh(oneColor, 4, 6))) * p);
                        SerialPortUtil.getInstance().sendCommand(SerialPortUtil.NURSELIGHT + "0", beComeDoubleStr(rr) + beComeDoubleStr(gg) + beComeDoubleStr(bb), "F");
                    }
                    Thread.sleep(300);
                    if (StringUtils.notEmpty(twoColor)) {
                        int rr = (int) ((StringUtils.parseFloat(StringUtils.substringByLengh(twoColor, 0, 2))) * p);
                        int gg = (int) ((StringUtils.parseFloat(StringUtils.substringByLengh(twoColor, 2, 4))) * p);
                        int bb = (int) ((StringUtils.parseFloat(StringUtils.substringByLengh(twoColor, 4, 6))) * p);
                        SerialPortUtil.getInstance().sendCommand(SerialPortUtil.NURSELIGHT + "1", beComeDoubleStr(rr) + beComeDoubleStr(gg) + beComeDoubleStr(bb), "F");
                    }
                    Thread.sleep(300);
                    if (StringUtils.notEmpty(threeColor)) {
                        int rr = (int) ((StringUtils.parseFloat(StringUtils.substringByLengh(threeColor, 0, 2))) * p);
                        int gg = (int) ((StringUtils.parseFloat(StringUtils.substringByLengh(threeColor, 2, 4))) * p);
                        int bb = (int) ((StringUtils.parseFloat(StringUtils.substringByLengh(threeColor, 4, 6))) * p);
                        SerialPortUtil.getInstance().sendCommand(SerialPortUtil.NURSELIGHT + "2", beComeDoubleStr(rr) + beComeDoubleStr(gg) + beComeDoubleStr(bb), "F");
                    }
                    Thread.sleep(300);
                    if (StringUtils.notEmpty(fourColor)) {
                        int rr = (int) ((StringUtils.parseFloat(StringUtils.substringByLengh(fourColor, 0, 2))) * p);
                        int gg = (int) ((StringUtils.parseFloat(StringUtils.substringByLengh(fourColor, 2, 4))) * p);
                        int bb = (int) ((StringUtils.parseFloat(StringUtils.substringByLengh(fourColor, 4, 6))) * p);
                        SerialPortUtil.getInstance().sendCommand(SerialPortUtil.NURSELIGHT + "3", beComeDoubleStr(rr) + beComeDoubleStr(gg) + beComeDoubleStr(bb), "F");
                    }
                    Thread.sleep(300);
                    if (StringUtils.notEmpty(fiveColor)) {
                        int rr = (int) ((StringUtils.parseFloat(StringUtils.substringByLengh(fiveColor, 0, 2))) * p);
                        int gg = (int) ((StringUtils.parseFloat(StringUtils.substringByLengh(fiveColor, 2, 4))) * p);
                        int bb = (int) ((StringUtils.parseFloat(StringUtils.substringByLengh(fiveColor, 4, 6))) * p);
                        SerialPortUtil.getInstance().sendCommand(SerialPortUtil.NURSELIGHT + "4", beComeDoubleStr(rr) + beComeDoubleStr(gg) + beComeDoubleStr(bb), "F");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    /**
     * 设置护理灯2
     */
    public static synchronized void setNurseLedLight2(final int brightness, final ArrayList<NurseConfigDto> configs) {
        synchronized (lockObject) {
            brightnessPercent = brightness;
            nurseConfigs = configs;
            dismissLed = false;

            updateNurseLed();
        }
    }

    /**
     * 熄灭护理灯
     */
    public static synchronized void dismissNurseLedLight() {
        synchronized (lockObject) {
            dismissLed = true;

            updateNurseLed();
        }
    }

    private static String beComeDoubleStr(int b) {
        if (b >= 0 && b < 10) {
            return "0" + b;
        } else if (b >= 10 && b < 100) {
            return "" + b;
        } else {
            return "99";
        }
    }

    private static void updateNurseLed() {
        if (ledRunnable == null) {
            ledRunnable = new NurseLedRunnable();
        }
        threadPool.execute(ledRunnable);
    }

    public static class NurseLedRunnable implements Runnable {
        @Override
        public void run() {
            try {
                if (dismissLed) {
                    for (int i = 0; i < 5; i++) {
                        SerialPortUtil.getInstance().sendCommand(SerialPortUtil.NURSELIGHT + i, "000000", "F");
                        Thread.sleep(300);
                    }
                } else {
                    if (nurseConfigs == null) {
                        return;
                    }

                    float p = (float) brightnessPercent / 100;
                    int size = nurseConfigs.size();
                    //只有5个护理灯，超出无效
                    if (size > 5) {
                        size = 5;
                    }
                    for (int i = 0; i < size; i++) {
                        String color = nurseConfigs.get(i).getNurseColorRbg();
                        if (StringUtils.notEmpty(color)) {
                            int rr = (int) ((StringUtils.parseHex2Int(StringUtils.substringByLengh(color, 0, 2))) * p);
                            int gg = (int) ((StringUtils.parseHex2Int(StringUtils.substringByLengh(color, 2, 4))) * p);
                            int bb = (int) ((StringUtils.parseHex2Int(StringUtils.substringByLengh(color, 4, 6))) * p);
                            SerialPortUtil.getInstance().sendCommand(SerialPortUtil.NURSELIGHT + i, beComeDoubleStr(rr) + beComeDoubleStr(gg) + beComeDoubleStr(bb), "F");
                        }
                        Thread.sleep(300);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
