package com.jiayue.ipfcst.client.protocol2file;

import com.jiayue.ipfcst.client.domain.entity.BaseStatusData;
import com.jiayue.ipfcst.client.utils.Constant;
import com.syjy.container.ProtocolDataContainer;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 包风塔或气象站数据
 *
 * @author xiuwei
 * @date 2022/04/22
 */
@Slf4j
public class PackageWindTowerOrWeatherStationData<T extends BaseStatusData> {

    private  final  T t ;

    public PackageWindTowerOrWeatherStationData(T t){
        this.t=t;
        this.equipmentType= this.t.getEquipmentType();
    }

    /**
     * 环境监测仪attrs
     */
    private static final Map<String,String> WEATHER_STATION_ATTRS =new HashMap<>();

   static {
       WEATHER_STATION_ATTRS.put("总辐射","globalR");
       WEATHER_STATION_ATTRS.put("直辐射","directR");
       WEATHER_STATION_ATTRS.put("散辐射","diffuseR");
       WEATHER_STATION_ATTRS.put("斜辐射","obliqueR");
       WEATHER_STATION_ATTRS.put("总辐射累积","globalRDA");
       WEATHER_STATION_ATTRS.put("直辐射累积","directRDA");
       WEATHER_STATION_ATTRS.put("散辐射累积","diffuseRDA");
       WEATHER_STATION_ATTRS.put("斜辐射累积","obliqueRDA");
       WEATHER_STATION_ATTRS.put("日照小时数","hourDA");
       WEATHER_STATION_ATTRS.put("环境温度","airT");
       WEATHER_STATION_ATTRS.put("背板温度","cellT");
       WEATHER_STATION_ATTRS.put("湿度","rh");
       WEATHER_STATION_ATTRS.put("气压","p");
   }


    /**
     * 测风塔attrs
     */
    private static final Map<String,String> WIND_TOWER_ATTRS =new HashMap<>();

    static {
        WIND_TOWER_ATTRS.put("10M风速","wsInst10");
        WIND_TOWER_ATTRS.put("10M风向","wdInst10");
        WIND_TOWER_ATTRS.put("30M风速","wsInst30");
        WIND_TOWER_ATTRS.put("30M风向","wdInst30");
        WIND_TOWER_ATTRS.put("50M风速","wsInst50");
        WIND_TOWER_ATTRS.put("50M风向","wdInst50");
        WIND_TOWER_ATTRS.put("70M风速","wsInst70");
        WIND_TOWER_ATTRS.put("70M风向","wdInst70");
        WIND_TOWER_ATTRS.put("90M风速","wsInst90");
        WIND_TOWER_ATTRS.put("90M风向","wdInst90");
        WIND_TOWER_ATTRS.put("100M风速","wsInst100");
        WIND_TOWER_ATTRS.put("100M风向","wdInst100");
        WIND_TOWER_ATTRS.put("110M风速","wsInst110");
        WIND_TOWER_ATTRS.put("110M风向","wdInst110");
        WIND_TOWER_ATTRS.put("120M风速","wsInst120");
        WIND_TOWER_ATTRS.put("120M风向","wdInst120");
        WIND_TOWER_ATTRS.put("温度","tInst");
        WIND_TOWER_ATTRS.put("湿度","rhInst");
        WIND_TOWER_ATTRS.put("气压","paInst");
    }



    Map<String,MyDataPoint> fileNameAndPoint= new HashMap<>();


    /**
     * 设备类型
     */
    private EquipmentType equipmentType;


    /**
     * 数据点位
     */
    public List<MyDataPoint> dataPointList =new ArrayList<>();



    public void addDataPoint(MyDataPoint dataPoint){
        if(dataPoint.getEquipmentType()!=null && this.equipmentType==dataPoint.getEquipmentType()){
            this.dataPointList.add(dataPoint);
        }
    }

    public void  init(){
        if(dataPointList!=null && dataPointList.size()!=0 && fileNameAndPoint.size()==0){
            Map<String,String> map=equipmentType==EquipmentType.WIND_TOWER?WIND_TOWER_ATTRS:WEATHER_STATION_ATTRS;
            for(MyDataPoint p: this.dataPointList){
                if(map.containsKey(p.getEquipmentAttr())){
                    this.fileNameAndPoint.put(map.get(p.getEquipmentAttr()),p);
                }
            }
        }
    }


    public T packageData(){
        T t1 = null;
        try {
            t1= (T) t.getClass().newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        for (Map.Entry<String,MyDataPoint> e :fileNameAndPoint.entrySet()){
            try {
                t1.putNumberValueByFiled(e.getKey(), ProtocolDataContainer.getInstance().getNumber(e.getValue().getId()));
            }catch (Exception exception){
                log.error("组装设备数据过程出错",e);
            }
        }
        t1.setStationCode(Constant.cacheClientConfig.getStationCode());
        t1.setTime(DateTime.now().withSecondOfMinute(0).withMillisOfSecond(0).toDate());
        return t1;
    }







}
