package com.gyee.frame.service.warn;

import com.gyee.frame.common.conf.TemperatureConfig;
import com.gyee.frame.common.spring.InitialRunner;
import com.gyee.frame.model.auto.WindTurbineTestingPointaiyj;
import com.gyee.frame.model.auto.Windturbine;
import com.gyee.frame.model.custom.PointData;
import com.gyee.frame.model.custom.TemperatureVo;
import com.gyee.frame.util.IRealTimeDataBaseUtil;
import com.gyee.frame.util.MathUtil;
import com.gyee.frame.util.RealTimeDataBaseFactory;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;


@Service
public class TemperatureService {

	IRealTimeDataBaseUtil realApiUtil = RealTimeDataBaseFactory.createRealTimeDataBase();

    public List<List<TemperatureVo>> temperatureQuery(String wpId,String type) throws Exception {

		List<List<TemperatureVo>> vos = new ArrayList<List<TemperatureVo>>();

		List<String> codels=new ArrayList<String>();
		switch(type) {
			case "bj" :
				codels= TemperatureConfig.getBj();
				break;
			case "yyz" :
				codels= TemperatureConfig.getYyz();
				break;
			case "clx" :
				codels= TemperatureConfig.getClx();
				break;
			case "fdj" :
				codels= TemperatureConfig.getFdj();
				break;
			case "qt" :
				codels= TemperatureConfig.getQt();
				break;
			case "zz" :
				codels= TemperatureConfig.getZz();
				break;
			case "zd" :
				codels= TemperatureConfig.getZd();
				break;

			default:
				break;
		}
			if (InitialRunner.wp_wtmap.containsKey(wpId)) {
				List<Windturbine> ws = InitialRunner.wp_wtmap.get(wpId);
				for (int i = 0; i < ws.size(); i++) {
					Windturbine wb = ws.get(i);
					List<TemperatureVo> retValue = new ArrayList<TemperatureVo>();
					for (int j = 0; j < codels.size(); j++) {
						WindTurbineTestingPointaiyj ai = InitialRunner.wtpAiyjmap.get(wb.getId()).get(codels.get(j));
						if (ai == null) {
							continue;
						}
						String id = ai.getId();

						PointData fs = realApiUtil.getRealData(id);
						TemperatureVo vo = new TemperatureVo();
						vo.setId(wb.getId() + codels.get(j));
						if (wb.getProjectid().equals("XS01_GC") && codels.get(j).equals("AI018")) {
							vo.setValue(MathUtil.decimal((Double.parseDouble(new DecimalFormat("#.000").format(fs.getPointValueInDouble())) / 100000), 2));
						} else {
							vo.setValue(Double.parseDouble(new DecimalFormat("#.000").format(fs.getPointValueInDouble())));
						}

						vo.setWtId(wb.getId());
						vo.setName(ai.getName());
						vo.setMaxValue(ai.getMaxval());
						vo.setMiValue(ai.getMinval());
						vo.setReasonableMaxVal(ai.getReasonablemaxval());
						vo.setReasonableMinVal(ai.getReasonableminval());
						vo.setColor("white");
						changeColor(vo, codels.get(j));
						retValue.add(vo);
					}
					vos.add(retValue);
				}
			}



		return vos;
    }

	private void setVoField(TemperatureVo vo, String point) {
		int ai=Integer.parseInt(point.substring(2));
		switch(ai){
			case 45:
			case 46:
			case 47:
			case 48:
			case 49:
			case 50:
				vo.setReasonableMaxVal(165d);
				vo.setReasonableMinVal(120d);
				vo.setMaxValue(165d);
				vo.setMiValue(0d);
				break;
			case 52:
			case 53:
				vo.setReasonableMaxVal(95d);
				vo.setReasonableMinVal(80d);
				vo.setMaxValue(95d);
				vo.setMiValue(0d);
				break;
			case 69:
			case 70:
				vo.setReasonableMaxVal(55d);
				vo.setReasonableMinVal(50d);
				vo.setMaxValue(55d);
				vo.setMiValue(0d);
				break;
			case 39:
				vo.setReasonableMaxVal(170d);
				vo.setReasonableMinVal(129d);
				vo.setMaxValue(170d);
				vo.setMiValue(125d);
				break;
			case 41:
				vo.setReasonableMaxVal(80d);
				vo.setReasonableMinVal(70d);
				vo.setMaxValue(80d);
				vo.setMiValue(10d);
				break;
			case 242:
			case 243:
			case 244:
				vo.setReasonableMaxVal(65d);
				vo.setReasonableMinVal(55d);
				vo.setMaxValue(65d);
				vo.setMiValue(-10d);
				break;
		}

	}


	private void changeColor(TemperatureVo vo,String point) {
		if(vo.getMaxValue()==null || vo.getMaxValue()==0d){
			setVoField(vo,point);

		} else {
			if (vo.getMaxValue() != null) {
				if (vo.getValue() > vo.getReasonableMaxVal()) {
					vo.setColor("yellow");
				}
				if (vo.getValue() >= vo.getMaxValue()) {
					vo.setColor("red");
				}
			}
		}
	}
}
