package com.gyee.frame.service;

import com.gyee.frame.common.spring.InitialRunner;
import com.gyee.frame.model.auto.Line;
import com.gyee.frame.model.auto.Project;
import com.gyee.frame.model.auto.Windpowerstation;
import com.gyee.frame.model.auto.Windturbine;
import com.gyee.frame.model.custom.DataVo;
import com.gyee.frame.service.weather.WeatherDay5Service;
import com.gyee.frame.util.DateUtils;
import com.gyee.frame.util.MathUtil;
import com.gyee.frame.util.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;


@Service
public class ForecastwindspeedService  {
	@Resource
	private WeatherDay5Service weatherDay5Service;
	@Resource
	private PowerToPowerService powerToPowerService;
	private final String QS = "0";
	private final String FD = "-1";
	private final String GF = "-2";

	public List<DataVo> getForecastwindspeedInfo(String wpId) {
		List<DataVo> vos=new ArrayList<>();

		if(StringUtils.notEmp(wpId))
		{

			if(wpId.equals(QS) || wpId.equals(FD))
			{
				for(Windpowerstation wp: InitialRunner.wpls)
				{
					DataVo vo=new DataVo();
					vo.setName(wp.getName());
					Map<String,Object> weathermap= weatherDay5Service.getWeatherRealDay5Info(wp.getId());

					if(!weathermap.isEmpty() && weathermap.containsKey("ls"))
					{
						java.util.List<Map<String,Object>> weatherls=(java.util.List<Map<String,Object>>)weathermap.get("ls");

						int num=0;
						for(int j=0;j<5;j++)
						{
							Map<Double, Integer> speedmap = new HashMap<Double, Integer>();
							for (int i=num;i<num+8;i++) {

								Map<String,Object> wh=weatherls.get(i);

								double fs=(double)wh.get("fs");

								if (speedmap.containsKey(fs)) {
									int temp = speedmap.get(fs) + 180;
									speedmap.put(fs, temp);
								} else {
									speedmap.put(fs, 180);
								}
							}
							switch (j)
							{
								case 0:
									double ycfdl = powerToPowerService.getPowerByWp(speedmap, wp.getId(), 1);
									vo.setValue1(MathUtil.twoBit(ycfdl));
									break;
								case 1:
									ycfdl = powerToPowerService.getPowerByWp(speedmap, wp.getId(), 1);
									vo.setValue2(MathUtil.twoBit(ycfdl));
									break;
								case 2:
									ycfdl = powerToPowerService.getPowerByWp(speedmap, wp.getId(), 1);
									vo.setValue3(MathUtil.twoBit(ycfdl));
									break;
								case 3:
									ycfdl = powerToPowerService.getPowerByWp(speedmap, wp.getId(), 1);
									vo.setValue4(MathUtil.twoBit(ycfdl));
									break;
								case 4:
									ycfdl = powerToPowerService.getPowerByWp(speedmap, wp.getId(), 1);
									vo.setValue5(MathUtil.twoBit(ycfdl));
									break;
							}
							num=num+8;

						}

						vos.add(vo);
					}

				}
			}else if(InitialRunner.wpmap.containsKey(wpId))
			{
				List<Project> pjls=InitialRunner.pjsmap.get(wpId);
				Map<String,Object> weathermap= weatherDay5Service.getWeatherRealDay5Info(wpId);
				for(Project pj:pjls)
				{
					DataVo vo=new DataVo();
					vo.setName(pj.getName());
					if(!weathermap.isEmpty() && weathermap.containsKey("ls"))
					{
						java.util.List<Map<String,Object>> weatherls=(java.util.List<Map<String,Object>>)weathermap.get("ls");

						int num=0;
						for(int j=0;j<5;j++)
						{
							Map<Double, Integer> speedmap = new HashMap<Double, Integer>();
							for (int i=num;i<num+8;i++) {

								Map<String,Object> wh=weatherls.get(i);

								double fs=(double)wh.get("fs");

								if (speedmap.containsKey(fs)) {
									int temp = speedmap.get(fs) + 180;
									speedmap.put(fs, temp);
								} else {
									speedmap.put(fs, 180);
								}
							}
							switch (j)
							{
								case 0:
									double ycfdl = powerToPowerService.getPowerByWp(speedmap, pj.getId(), 1);
									vo.setValue1(MathUtil.twoBit(ycfdl));
									break;
								case 1:
									ycfdl = powerToPowerService.getPowerByWp(speedmap, pj.getId(), 1);
									vo.setValue2(MathUtil.twoBit(ycfdl));
									break;
								case 2:
									ycfdl = powerToPowerService.getPowerByWp(speedmap, pj.getId(), 1);
									vo.setValue3(MathUtil.twoBit(ycfdl));
									break;
								case 3:
									ycfdl = powerToPowerService.getPowerByWp(speedmap, pj.getId(), 1);
									vo.setValue4(MathUtil.twoBit(ycfdl));
									break;
								case 4:
									ycfdl = powerToPowerService.getPowerByWp(speedmap, pj.getId(), 1);
									vo.setValue5(MathUtil.twoBit(ycfdl));
									break;
							}
							num=num+8;

						}

						vos.add(vo);
					}
				}
			}else if(InitialRunner.pjmap.containsKey(wpId))
			{
				List<Line> lnls=InitialRunner.lnsmap.get(wpId);
				Map<String,Object> weathermap= weatherDay5Service.getWeatherRealDay5Info(wpId);
				for(Line ln:lnls)
				{
					DataVo vo=new DataVo();
					vo.setName(ln.getName());
					if(!weathermap.isEmpty() && weathermap.containsKey("ls"))
					{
						java.util.List<Map<String,Object>> weatherls=(java.util.List<Map<String,Object>>)weathermap.get("ls");

						int num=0;
						for(int j=0;j<5;j++)
						{
							Map<Double, Integer> speedmap = new HashMap<Double, Integer>();
							for (int i=num;i<num+8;i++) {

								Map<String,Object> wh=weatherls.get(i);

								double fs=(double)wh.get("fs");

								if (speedmap.containsKey(fs)) {
									int temp = speedmap.get(fs) + 180;
									speedmap.put(fs, temp);
								} else {
									speedmap.put(fs, 180);
								}
							}
							switch (j)
							{
								case 0:
									double ycfdl = powerToPowerService.getPowerByWp(speedmap, ln.getId(), 1);
									vo.setValue1(MathUtil.twoBit(ycfdl));
									break;
								case 1:
									ycfdl = powerToPowerService.getPowerByWp(speedmap, ln.getId(), 1);
									vo.setValue2(MathUtil.twoBit(ycfdl));
									break;
								case 2:
									ycfdl = powerToPowerService.getPowerByWp(speedmap, ln.getId(), 1);
									vo.setValue3(MathUtil.twoBit(ycfdl));
									break;
								case 3:
									ycfdl = powerToPowerService.getPowerByWp(speedmap, ln.getId(), 1);
									vo.setValue4(MathUtil.twoBit(ycfdl));
									break;
								case 4:
									ycfdl = powerToPowerService.getPowerByWp(speedmap, ln.getId(), 1);
									vo.setValue5(MathUtil.twoBit(ycfdl));
									break;
							}
							num=num+8;

						}

						vos.add(vo);
					}
				}
			}else if(InitialRunner.lnmap.containsKey(wpId))
			{
				List<Windturbine> wtls=InitialRunner.ln_wtmap.get(wpId);
				Map<String,Object> weathermap= weatherDay5Service.getWeatherRealDay5Info(wpId);
				for(Windturbine wt:wtls)
				{
					DataVo vo=new DataVo();
					vo.setName(wt.getName());
					if(!weathermap.isEmpty() && weathermap.containsKey("ls"))
					{
						java.util.List<Map<String,Object>> weatherls=(java.util.List<Map<String,Object>>)weathermap.get("ls");

						int num=0;
						for(int j=0;j<5;j++)
						{
							Map<Double, Integer> speedmap = new HashMap<Double, Integer>();
							for (int i=num;i<num+8;i++) {

								Map<String,Object> wh=weatherls.get(i);

								double fs=(double)wh.get("fs");

								if (speedmap.containsKey(fs)) {
									int temp = speedmap.get(fs) + 180;
									speedmap.put(fs, temp);
								} else {
									speedmap.put(fs, 180);
								}
							}
							switch (j)
							{
								case 0:
									double ycfdl = powerToPowerService.getPowerByWp(speedmap, wt.getId(), 1);
									vo.setValue1(MathUtil.twoBit(ycfdl));
									break;
								case 1:
									ycfdl = powerToPowerService.getPowerByWp(speedmap, wt.getId(), 1);
									vo.setValue2(MathUtil.twoBit(ycfdl));
									break;
								case 2:
									ycfdl = powerToPowerService.getPowerByWp(speedmap, wt.getId(), 1);
									vo.setValue3(MathUtil.twoBit(ycfdl));
									break;
								case 3:
									ycfdl = powerToPowerService.getPowerByWp(speedmap, wt.getId(), 1);
									vo.setValue4(MathUtil.twoBit(ycfdl));
									break;
								case 4:
									ycfdl = powerToPowerService.getPowerByWp(speedmap, wt.getId(), 1);
									vo.setValue5(MathUtil.twoBit(ycfdl));
									break;
							}
							num=num+8;

						}

						vos.add(vo);
					}
				}
			}

		}


		return vos;
	}

	public List<String> getDayInfo()
	{
		List<String> vos=new ArrayList<>();
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DAY_OF_MONTH,1);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		for(int i=0;i<5;i++)
		{
			vos.add(DateUtils.format(cal.getTime()));
			cal.add(Calendar.DAY_OF_MONTH,1);

		}
		return vos;
	}

}
