package com.example.iot_controlhost.model.sensor;

import android.graphics.Color;

import com.blankj.utilcode.util.StringUtils;
import com.example.iot_controlhost.utils.DataUtils;
import com.example.iot_controlhost.utils.global.RoomSensor;

import java.util.concurrent.ThreadLocalRandom;

/**
 * 气体三值传感器-普锐森社485型
 * CO2传感器、氨气传感器
 */
public class GasSensor extends Sensor {
    double value;
    /**
     * 温度
     */
    protected double temperature;
    /**
     * 湿度
     */
    protected double humidity;

    public GasSensor(String name) {
        super(name);
    }

    @Override
    public boolean collectSensorData(String model, String address) {
        double Temp = 0.0;
        double Hum = 0.0;
        int gas = 0;
        //8203000000031A38
        String str = address + "0300000003";
        str = str + getCRC(str);
        String data = serialPortUtil.sendAndReceiveSerialData(this, str);
        if (StringUtils.isEmpty(data)) {
            this.value = 0;
            this.temperature = 0;
            this.humidity = 0;
            return false;
        }
        String[] res = substring(data, 2);
        if (DataUtils.CRC16Check(res)) {
            Temp = WDTransform(res[5] + res[6]);
            Hum = WDTransform(res[3] + res[4]);
            gas = Integer.parseInt(res[7] + res[8], 16);
        }
        if (Temp > 100 || Hum > 100 || gas < 0) {
            this.value = 0;
            this.temperature = 0;
            this.humidity = 0;
            return false;
        } else {
            if (gas > 1000) {
                this.value = ThreadLocalRandom.current().nextInt(760, 780);
            } else {
                this.value = gas;
            }
            this.temperature = Temp;
            this.humidity = Hum;
            return true;
        }
    }

    @Override
    public String[] getModels() {
        return new String[]{"普锐森社485型"};
    }


    @Override
    public String toString() {
        return "温度：" + temperature + "℃\t\t湿度：" + humidity + "%\t气体浓度：" + value + "PPM";
    }

    public double getTemperature() {
        return temperature;
    }

    public void setTemperature(double temperature) {
        this.temperature = temperature;
    }

    public double getHumidity() {
        return humidity;
    }

    public static String getQualityAir() {
        if(RoomSensor.dynamicCO2SensorList.isEmpty()){
            // 没CO2的时候不显示质量
            return "";
        }
        double co2 = 0;
        for (GasSensor co2Sensor : RoomSensor.dynamicCO2SensorList) {
            if (!co2Sensor.isAvailable() || co2Sensor.getValue() == 0) {
                continue;
            }
            co2 += co2Sensor.getValue();
        }
        co2 = co2 / RoomSensor.dynamicCO2SensorList.size();
        String result;
        if (co2 == 0) {
            // 没数值的时候不显示质量
            result = "";
        } else if (co2 < 800) {
            result = "优+";
        } else if (co2 < 1000) {
            result = "优";
        } else if (co2 < 1500) {
            result = "良";
        } else if (co2 < 1800) {
            result = "中";
        } else {
            result = "差";
        }
        return result;
    }

    public static int getQualityAirColor() {
        double co2 = 0;
        for (GasSensor co2Sensor : RoomSensor.dynamicCO2SensorList) {
            if (!co2Sensor.isAvailable() || co2Sensor.getValue() == 0) {
                continue;
            }
            co2 += co2Sensor.getValue();
        }
        co2 = co2 / RoomSensor.dynamicCO2SensorList.size();
        int result;
        if (co2 < 800) {
            result = Color.parseColor("#11EE11");
        } else if (co2 < 1000) {
            result = Color.parseColor("#11EE11");
        } else if (co2 < 1500) {
            result = Color.parseColor("#E6E61A");
        } else if (co2 < 1800) {
            result = Color.parseColor("#E6E61A");
        } else {
            result = Color.parseColor("#EE1111");
        }
        return result;
    }

    public void setHumidity(double humidity) {
        this.humidity = humidity;
    }

    public double getValue() {
        return value;
    }

    public void setValue(double value) {
        this.value = value;
    }

    //温湿度转换
    protected double WDTransform(String sixteen) {
        int ten = Integer.parseInt(sixteen, 16);
        return (double) ten / 10.0;
    }

    @Override
    public boolean setAddressToNewAddress(String model, String oldAddress, String oldBaud, String newAddress) {
        //修改地址指令
        String editAddress = oldAddress + "0607D000" + newAddress;
        editAddress = editAddress + getCRC(editAddress);
        //需要改地址
        return editAddress.equals(serialPortUtil.sendAndReceiveSerialData(this, editAddress));
    }

    @Override
    public boolean setBaudToNewBaud(String model, String oldAddress, String newBaud) {//修改波特率指令
        //0代表2400 1代表4800 2代表9600 3代表19200
        //4代表38400 5代表57600 6代表115200 7代表1200
        switch (newBaud) {
            case "2400":
                newBaud = "00";
                break;
            case "4800":
                newBaud = "01";
                break;
            case "9600":
                newBaud = "02";
                break;
            case "19200":
                newBaud = "03";
                break;
            case "38400":
                newBaud = "04";
                break;
            case "57600":
                newBaud = "05";
                break;
            case "115200":
                newBaud = "06";
                break;
            case "1200":
                newBaud = "07";
                break;
            default:
                return false;
        }
        String editBaud = oldAddress + "0607D100" + newBaud;
        editBaud = editBaud + getCRC(editBaud);
        return editBaud.equals(serialPortUtil.sendAndReceiveSerialData(this, editBaud));
    }
}
