package com.cloudcross.ssp.common.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;

import com.cloudcross.ssp.model.AdGroupSchedule;



/**
 * 排期定向工具类
 * 
 * @author tonglili
 * 
 */
public class OperatorScheduleAclsUtis {
  private static final String NODEHOUR = "-1";

  /**
   * 获得排期信息列表
   * 
   * @author tonglili
   * @param adGroupId
   * @param scheduleStr
   * @return 排期信息列表
   */
  public static List<AdGroupSchedule> getScheduleList(long adGroupId,
      String scheduleStr) {
    Date now = new Date();
    SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
    List<AdGroupSchedule> scheduleList = new ArrayList<AdGroupSchedule>();
    JSONArray scheduleArray = JSONArray.fromObject(scheduleStr);
    for (Object object : scheduleArray) {
    	AdGroupSchedule schedule = (AdGroupSchedule) JSONObject.toBean((JSONObject) object,
    			AdGroupSchedule.class);
      schedule.setAdGroupId(adGroupId);
      schedule.setUpdated(now);
      try {
    	  if(schedule.getDehours() == null || "".equals(schedule.getDehours().trim())  || schedule.getDehours().equals(NODEHOUR)){
    		  schedule.setDehours("");
    	  }
        schedule.setBeginDate(sf.parse(schedule.getStartDate()));
        schedule.setsEndDate(sf.parse(schedule.getEndDate()));
      } catch (ParseException e) {
        e.printStackTrace();
      }
      scheduleList.add(schedule);
      /*
      if (!schedule.getDehours().equals(NODEHOUR)) {
        scheduleList.add(schedule);
      }*/
    }
    return scheduleList;
  }

  public static List<Date> findDates(Date dBegin, Date dEnd) {
    SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
    try {
      dBegin = sf.parse(sf.format(dBegin));
      dEnd = sf.parse(sf.format(dEnd));
    } catch (ParseException e) {
      System.out.println(e.getLocalizedMessage());
      return null;
    }
    List<Date> lDate = new ArrayList<Date>();
    lDate.add(dBegin);
    Calendar calBegin = Calendar.getInstance();
    // 使用给定的 Date 设置此 Calendar 的时间
    calBegin.setTime(dBegin);
    Calendar calEnd = Calendar.getInstance();
    // 使用给定的 Date 设置此 Calendar 的时间
    calEnd.setTime(dEnd);
    // 测试此日期是否在指定日期之后
    while (dEnd.after(calBegin.getTime())) {
      // 根据日历的规则，为给定的日历字段添加或减去指定的时间量
      calBegin.add(Calendar.DAY_OF_MONTH, 1);
      lDate.add(calBegin.getTime());
    }
    return lDate;
  }

  /**
   * 获得排期定向时间值
   * 
   * @author tonglili
   * @param scheduleList
   *          排期对象列表
   * @return 排期定向时间值
   */
  public static String scheduleRoleStr(List<AdGroupSchedule> scheduleList) {

    if (null == scheduleList || scheduleList.size() == 0) {
      return "";
    }
    List<String> returnList = new ArrayList<String>();
    for (AdGroupSchedule schedule : scheduleList) {
    	if(schedule.getDehours() == null || "".equalsIgnoreCase(schedule.getDehours().trim()) ||  schedule.getDehours().equals(NODEHOUR)){
    		continue;
    	}
    	//默认排期，不加定向限制
    	if(scheduleList.size() == 1 && "0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23".equalsIgnoreCase(schedule.getDehours())){
    		return "";
    	}
      String scheduleStr = scheduleRoleStr(schedule.getBeginDate(),
          schedule.getsEndDate(), schedule.getDehours());

      if (null != scheduleStr) {
        returnList.add(scheduleStr);
      }
    }
    System.out.println("StringUtils.join(returnList.toArray())"
        + StringUtils.join(returnList.toArray(), ","));
    return StringUtils.join(returnList.toArray(), ",");
  }

  /**
   * 获得排期时间定向值
   * 
   * @author tonglili
   * @param startDate
   *          开始时间(yyyy-MM-dd)
   * @param endDate
   *          结束时间(yyyy-MM-dd)
   * @param dehours
   *          时间段
   * @return 排期定向值
   */
  public static String scheduleRoleStr(Date startDate, Date endDate,
      String dehours) {
    List<String> scheduleList = new ArrayList<String>();
    List<Date> dateList = findDates(startDate, endDate);
    SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
    String[] hours = dehours.split(",");
    //for (int i = 0; i < hours.length; i++) {
    //  String hour = hours[i];
     // if (!hour.equals(NODEHOUR))
        for (Date date : dateList) {
        	String s = sf.format(date);
        	for (int i = 0; i < hours.length; i++) {
        		String hour = hours[i];
        		if (!hour.equals(NODEHOUR)){
        			s = s.concat("_").concat(hour);
        		}
        	}
          //String s = sf.format(date).concat("_").concat(hour);
          scheduleList.add(s);
        }
    String returnStr = StringUtils.join(scheduleList.toArray(), ",");
    System.out.println(returnStr);
    return returnStr;
  }
  
  /*
  public static String scheduleRoleStr(Date startDate, Date endDate,
	      String dehours) {
	    List<String> scheduleList = new ArrayList<String>();
	    List<Date> dateList = findDates(startDate, endDate);
	    SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
	    String[] hours = dehours.split(",");
	    for (int i = 0; i < hours.length; i++) {
	      String hour = hours[i];
	      if (!hour.equals(NODEHOUR))
	        for (Date date : dateList) {

	          String s = sf.format(date).concat("_").concat(hour);
	          scheduleList.add(s);
	        }
	    }
	    String returnStr = StringUtils.join(scheduleList.toArray(), ",");
	    System.out.println(returnStr);
	    return returnStr;
	  }*/

  /**
   * 例子
   * 
   * @param args
   * @throws ParseException
   */
  public static void main(String[] args) throws ParseException {
    SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
    Date s = sf.parse("2015-01-26");
    Date e = sf.parse("2015-02-29");
    scheduleRoleStr(s, e, "1,2,3");
  }
}
