package cn.com.countrygarden.bi.main.controller.marketing.backend;

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

import javax.persistence.Column;

import cn.com.countrygarden.bi.main.model.common.Comm;
import cn.com.countrygarden.bi.main.model.project.CommInfoVO;
import cn.com.countrygarden.bi.main.model.project.CommRecord;
import cn.com.countrygarden.bi.main.model.project.ProjectClass;
import cn.com.countrygarden.bi.main.service.project.impl.vo.IndicatorLight;
import cn.com.countrygarden.bi.main.util.DateUtil;

public class CommUtil {
	public final static String YELLOW = "yellow";//黄
	public final static  String RED="red";//红
	public final static  String BLACK="black";//黑
	public final static  String GREEN = "green";//绿
	/**
	 * 
	 * @param beginDateStr 开始时间
	 * @param endDateStr 结束时间
	 * @return 时间相减天数
	 */
    public static long getDaySub(Date beginDateStr,Date endDateStr)
    {
    	long day =0;
    	if(endDateStr!=null) {
    		day = (beginDateStr.getTime()-endDateStr.getTime())/(24*60*60*1000);
    	}
      return day;
    }
    
    
    public static CommRecord assembleCommRecord(String classType,Comm c,Map<String, CommInfoVO> map,long day,Date date ) {
    	CommRecord cr = new CommRecord();
    	if(date == null) {
    		date = new Date();
    	}
    	//如果是o类
    	if(c.getProjectClassCode().equals(ProjectClass.CODE_CLASSBYO)) {
    		//如果跳A 那么 D,C,A直接通过
    		if(classType.equals(ProjectClass.CODE_CLASSBYA)) {
    			cr.setClassD(GREEN);
    			cr.setdDate(date);
    			cr.setClassC(GREEN);
    			cr.setcDate(date);
    			cr.setClassB(colour(map,day,classType));
    			cr.setbDate(date);
    			//跳入当前节点不存储该节点的灯
    			//cr.setClassA();
    		}else if(classType.equals(ProjectClass.CODE_CLASSBYB)) {
    			cr.setClassD(GREEN);
    			cr.setdDate(date);
    			cr.setClassC(colour(map,day,classType));
    			cr.setcDate(date);
    			//cr.setClassB();
    		}else if(classType.equals(ProjectClass.CODE_CLASSBYC)) {
    			cr.setdDate(date);
    			cr.setClassD(colour(map,day,classType));
    			//cr.setClassC();
    			
    		}else if(classType.equals(ProjectClass.CODE_CLASSBYD)) {
    			//O类跳D类 不用存储 因为D类是开始节点
    			//cr.setClassD(colour(map,day));
    			
    		}else if(classType.equals(ProjectClass.CODE_CLASSBYY)) {
    			//如果是重O类跳Y类 设置全部通过绿灯
    			cr.setClassD(GREEN);
    			cr.setdDate(date);
    			cr.setClassC(GREEN);
    			cr.setcDate(date);
    			cr.setClassB(GREEN);
    			cr.setbDate(date);
    			cr.setClassA(GREEN);
    			cr.setaDate(date);
    			cr.setClassY(DateUtil.simpleFormat(new Date()));
    		}
    	}else {
    		
    		//如果跳A 那么 D,C,A直接通过
    		if(classType.equals(ProjectClass.CODE_CLASSBYA)) {
    			cr.setClassB(colour(map,day,ProjectClass.CODE_CLASSBYB));
    			cr.setbDate(date);
    			//跳入当前节点不存储该节点的灯
    			//cr.setClassA();
    		}else if(classType.equals(ProjectClass.CODE_CLASSBYB)) {
    			cr.setClassC(colour(map,day,ProjectClass.CODE_CLASSBYC));
    			cr.setcDate(date);
    			//cr.setClassB();
    		}else if(classType.equals(ProjectClass.CODE_CLASSBYC)) {
    			cr.setClassD(colour(map,day,ProjectClass.CODE_CLASSBYD));
    			cr.setdDate(date);
    			//cr.setClassC();
    			
    		}else if(classType.equals(ProjectClass.CODE_CLASSBYD)) {
    			//O类跳D类 不用存储 因为D类是开始节点
    			//cr.setClassD(colour(map,day));
    			
    		}else if(classType.equals(ProjectClass.CODE_CLASSBYY)) {
    			cr.setClassA(colour(map,day,ProjectClass.CODE_CLASSBYA));
    			cr.setaDate(date);
    		}
    	}
    	return cr;
    }
    
	public static String colour(Map<String, CommInfoVO> map,long day,String classType) {
		CommInfoVO integer = map.get(classType);
		if(integer.getYellow()==0) {
			return GREEN;
		}else {
		if(day>=integer.getBlack()) {
			return BLACK;
		}else if(day>=integer.getRed()) {
			return RED;
		}else if(day>=integer.getYellow()) {
			return YELLOW;
		}else {
			return GREEN;
		}
		}
			
	}
	
	public static Map<String, CommInfoVO>  colourDay(List<CommInfoVO> ci,IndicatorLight findLight){
		 Map<String, CommInfoVO> map = new HashMap<>();
	       for (CommInfoVO commInfoVO : ci) {
	    	   map.put(commInfoVO.getType(), commInfoVO);
	       }
		   if(!map.containsKey(ProjectClass.CODE_CLASSBYA)) {
			   CommInfoVO commInfoVO = new CommInfoVO();
			   commInfoVO.setYellow(3);
			   commInfoVO.setRed(6);
			   commInfoVO.setBlack(10);
			   if(findLight.getaYellow()!=null) {
			   commInfoVO.setYellow(findLight.getaYellow());
			   }
			   if(findLight.getaRed()!=null) {
			   commInfoVO.setRed(findLight.getaRed());
			   }
			   
			   map.put(ProjectClass.CODE_CLASSBYA, commInfoVO);
		   }
		   
		   if(!map.containsKey(ProjectClass.CODE_CLASSBYB)) {
			   CommInfoVO commInfoVO = new CommInfoVO();
			   commInfoVO.setYellow(3);
			   commInfoVO.setRed(6);
			   commInfoVO.setBlack(10);
			   if(findLight.getbYellow()!=null) {
				   commInfoVO.setYellow(findLight.getbYellow());
				   }
				   if(findLight.getbRed()!=null) {
				   commInfoVO.setRed(findLight.getbRed());
				   }
				   map.put(ProjectClass.CODE_CLASSBYB, commInfoVO);
		   }
		   if(!map.containsKey(ProjectClass.CODE_CLASSBYC)) {
			   CommInfoVO commInfoVO = new CommInfoVO();
			   commInfoVO.setYellow(3);
			   commInfoVO.setRed(6);
			   commInfoVO.setBlack(10);
			   if(findLight.getcYellow()!=null) {
				   commInfoVO.setYellow(findLight.getcYellow());
				   }
				   if(findLight.getcRed()!=null) {
				   commInfoVO.setRed(findLight.getcRed());
				   }
				   map.put(ProjectClass.CODE_CLASSBYC, commInfoVO);
		   }
		   if(!map.containsKey(ProjectClass.CODE_CLASSBYD)) {
			   CommInfoVO commInfoVO = new CommInfoVO();
			   commInfoVO.setYellow(3);
			   commInfoVO.setRed(6);
			   commInfoVO.setBlack(10);
			   if(findLight.getdYellow()!=null) {
				   commInfoVO.setYellow(findLight.getdYellow());
				   }
				   if(findLight.getdRed()!=null) {
				   commInfoVO.setRed(findLight.getdRed());
				   }
				   map.put(ProjectClass.CODE_CLASSBYD, commInfoVO);
		   }
		   return map;
	}
}
