package com.yfyy.yingtan.util;

import com.yfyy.yingtan.ApplicationCons;
import com.yfyy.yingtan.DeviceCons;
import com.yfyy.yingtan.MainApplication;
import com.yfyy.yingtan.bean.DevInfoBean;
import com.yfyy.yingtan.bean.request.LdReqBean;
import com.yfyy.yingtan.bean.request.NonLoraDataReqBean;
import com.yfyy.yingtan.bean.request.SyncReqBean;
import com.yfyy.yingtan.bean.request.ZsReqBean;
import com.yfyy.yingtan.bean.response.DataBean;
import com.yfyy.yingtan.bean.response.DevSubBean;
import com.yfyy.yingtan.bean.response.ExtBean;

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

/**
 * 数据处理工具类
 */
public class DataUtil {

    public static void handleAndReport(DevSubBean devSubBean){
        ExtBean extBean = devSubBean.getExt();
        String originalId = extBean.getStationID();

        String SoilTemperature = "0";// 土壤温湿度
        String SoilHumidity = "0";// 土壤温湿度
        String WindSpeed = "0";// 风速
        String WindDirection = "0";// 风向
        String illuminance = "0";// 光照度
        String RainAdd = "0";// 降雨量
        String pH = "0";// 土壤PH

        String Etemperature = "0";// 气温
        String Ehumidity = "0";// 气湿
        String AirPressure = "0";// 气压
        String Noisepollution = "0";// 噪声

        List<DataBean> dataBeanList = extBean.getData();

        if(dataBeanList != null && dataBeanList.size() > 0){
            for (DataBean dataBean : dataBeanList) {
                switch (dataBean.geteName()) {
                    case "SoilTemperature":
                        SoilTemperature = dataBean.geteValue();
                        break;
                    case "SoilHumidity":
                        SoilHumidity = dataBean.geteValue();
                        break;
                    case "WindSpeed":
                        WindSpeed = dataBean.geteValue();
                        break;
                    case "WindDirection":
//                        WindDirection = dataBean.geteValue();
                        WindDirection = getRealWindDirect(dataBean.geteValue());
                        break;
                    case "illuminance":
                        illuminance = dataBean.geteValue();
                        break;
                    case "RainAdd":
                        RainAdd = dataBean.geteValue();
                        break;
                    case "pH":
                        pH = dataBean.geteValue();
                        break;
                    case "Etemperature":
                        Etemperature = dataBean.geteValue();
                        break;
                    case "Ehumidity":
                        Ehumidity = dataBean.geteValue();
                        break;
                    case "AirPressure":
                        AirPressure = dataBean.geteValue();
                        break;
                    case "Noisepollution":
                        Noisepollution = dataBean.geteValue();
                        break;
                }
            }

            if(MainApplication.ZS_Device_Name_List.contains(originalId)){
                onlineAndReportProperty_zs(originalId, Etemperature, Ehumidity, AirPressure, Noisepollution, WindSpeed, WindDirection);
            }else if(MainApplication.LD_Device_Name_List.contains(originalId)){
                onlineAndReportProperty_ld(originalId, SoilTemperature, SoilHumidity, WindSpeed, WindDirection, illuminance, RainAdd, pH);
            }
        }
    }

    // 计算风向
    //1：正北方
    //2：北东北方
    //3：东北方
    //4：东东北方
    //5：正东方
    //6：东东南方
    //7：东南方
    //8：南东南方
    //9：正南方
    //10：南西南方
    //11：西南方
    //12：西西南方
    //13：正西方
    //14：西西北风
    //15：西北方
    //16：北西北方"
    private static String getRealWindDirect(String geteValue) {
        if(geteValue == null){
            return "1";
        }
        float valueF = Float.parseFloat(geteValue);
        if(valueF < 11.5){
            return "1";
        }else if(valueF < 34){
            return "2";
        }else if(valueF < 56){
            return "3";
        }else if(valueF < 79){
            return "4";
        }else if(valueF < 101){
            return "5";
        }else if(valueF < 124){
            return "6";
        }else if(valueF < 146){
            return "7";
        }else if(valueF < 169){
            return "8";
        }else if(valueF < 191){
            return "9";
        }else if(valueF < 214){
            return "10";
        }else if(valueF < 236){
            return "11";
        }else if(valueF < 259){
            return "12";
        }else if(valueF < 281){
            return "13";
        }else if(valueF < 304){
            return "14";
        }else if(valueF < 326){
            return "15";
        }else if(valueF < 349){
            return "16";
        }else {
            return "1";
        }
    }

    // 上线+上报属性-噪声设备
    private static void onlineAndReportProperty_zs(String originalId, String Etemperature, String Ehumidity,
                                                   String AirPressure, String Noisepollution, String WindSpeed, String WindDirection){
        List<SyncReqBean> syncReqBeanList = new ArrayList<>();
        SyncReqBean syncReqBean = getBean(originalId, "ZS");
        syncReqBeanList.add(syncReqBean);

        ZsReqBean zsReqBean = new ZsReqBean();
        zsReqBean.setDevice_id(syncReqBean.getSensorId());
        zsReqBean.setReceived_time(MyDateUtil.toStandardString(new Date()));
        zsReqBean.setF_cnt_up("1");

        zsReqBean.setEtemperature(Etemperature);
        zsReqBean.setEhumidity(Ehumidity);
        zsReqBean.setAirPressure(AirPressure);
        zsReqBean.setNoisepollution(Noisepollution);
        zsReqBean.setWindSpeed(WindSpeed);
        zsReqBean.setWindDirection(WindDirection);

        NonLoraDataReqBean nonLoraDataReqBean = new NonLoraDataReqBean();
        nonLoraDataReqBean.setDataCode(syncReqBean.getDataCode());
        nonLoraDataReqBean.setData(zsReqBean);
        nonLoraDataReqBean.setDeviceId(syncReqBean.getSensorId());
        nonLoraDataReqBean.setPostTime(MyDateUtil.toStandardString(new Date()));

        System.out.println("--------------------" + syncReqBean.getSensorId());
        HttpUtil.onlineAndReportProperty(syncReqBeanList, nonLoraDataReqBean);
    }

    // 上线+上报属性-绿地设备
    private static void onlineAndReportProperty_ld(String originalId, String SoilTemperature, String SoilHumidity, String WindSpeed,
                                                   String WindDirection, String illuminance, String RainAdd, String pH){
        List<SyncReqBean> syncReqBeanList = new ArrayList<>();
        SyncReqBean syncReqBean = getBean(originalId, "LD");
        syncReqBeanList.add(syncReqBean);

        LdReqBean ldReqBean = new LdReqBean();
        ldReqBean.setDevice_id(syncReqBean.getSensorId());
        ldReqBean.setReceived_time(MyDateUtil.toStandardString(new Date()));
        ldReqBean.setF_cnt_up("1");

        ldReqBean.setWindSpeed(WindSpeed);
        ldReqBean.setWindDirection(WindDirection);
        ldReqBean.setIlluminance(illuminance);
        ldReqBean.setRainfall(RainAdd);
        ldReqBean.setSoilTemperature(SoilTemperature);
        ldReqBean.setSoilHumidity(SoilHumidity);
        ldReqBean.setSoilPH(pH);

        NonLoraDataReqBean nonLoraDataReqBean = new NonLoraDataReqBean();
        nonLoraDataReqBean.setDataCode(syncReqBean.getDataCode());
        nonLoraDataReqBean.setData(ldReqBean);
        nonLoraDataReqBean.setDeviceId(syncReqBean.getSensorId());
        nonLoraDataReqBean.setPostTime(MyDateUtil.toStandardString(new Date()));

        HttpUtil.onlineAndReportProperty(syncReqBeanList, nonLoraDataReqBean);
    }

    private static SyncReqBean getBean(String originalId, String devKey) {
        SyncReqBean syncReqBean = new SyncReqBean();

        DevInfoBean devInfoBean = DeviceCons.getDevInfoBean(originalId);
        syncReqBean.setSensorName(devInfoBean.getSensorName());
        syncReqBean.setWsLng(devInfoBean.getWsLng());
        syncReqBean.setWsLat(devInfoBean.getWsLat());
        syncReqBean.setCityId(devInfoBean.getCityId());
        syncReqBean.setAreaId(devInfoBean.getAreaId());
        syncReqBean.setRegionId(devInfoBean.getRegionId());
        syncReqBean.setAddress(devInfoBean.getAddress());

        syncReqBean.setSensorType(DeviceCons.sensorType_MAP.get(devKey));

        syncReqBean.setApplyId("YY000120");

        syncReqBean.setDataCode(DeviceCons.dataCode_MAP.get(devKey));

        String deviceId = originalId;
        if (deviceId.equals("864865060474468")){
            deviceId = "861551051612711";
        }
        syncReqBean.setSensorId(deviceId);
        syncReqBean.setProjectCode(ApplicationCons.ProjectCode);
        return syncReqBean;
    }

}
