package com.ylcm.sys.common;

import java.time.LocalDate;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.List;

import com.ylcm.sys.enums.AdTheatreEffectEnum;
import com.ylcm.sys.enums.AdTheatreTimeEnum;

public class ProjectUtil {
	
	private static LocalTime DAY = LocalTime.of(7, 30);
	private static LocalTime GOLD = LocalTime.of(18, 0);
	private static LocalTime NIGHT = LocalTime.of(21, 30);
	private static LocalTime MORNING = LocalTime.of(23, 30);

	public static String MORNING_STR = "23:30:00";


	/**
	 *  左含右不含
	 * 	// 0日间 1黄金 2午夜 3凌晨
		// 7:30-18:00    日间剧场
		// 18:00-21:30   黄金剧场
		// 21:30-23:30  午夜剧场
		// 23:30-7:30    凌晨剧场
	 * @param theatreTimeType
	 * @return
	 */
	public static List<LocalTime> getLocalTimeByType(Integer theatreTimeType) {
		if (theatreTimeType == 0) {
			return Arrays.asList(DAY, GOLD);
		}
		if (theatreTimeType == 1) {
			return Arrays.asList(GOLD, NIGHT);
		}
		if (theatreTimeType == 2) {
			return Arrays.asList(NIGHT, MORNING);
		} 
		return Arrays.asList(MORNING, DAY);
	}
	
	public static int getTypeByLocalTime(LocalTime localTime) {
		if (localTime.compareTo(DAY) >= 0 && localTime.compareTo(GOLD) < 0 ) {
			return AdTheatreTimeEnum.DAY.getCode();
		}
		if (localTime.compareTo(GOLD) >= 0 && localTime.compareTo(NIGHT) < 0 ) {
			return AdTheatreTimeEnum.GOLD.getCode();
		}
		if (localTime.compareTo(NIGHT) >= 0 && localTime.compareTo(MORNING) < 0 ) {
			return AdTheatreTimeEnum.MID_NIGHT.getCode();
		}
		return AdTheatreTimeEnum.EARLY_MORNING.getCode();
	}
	
	public static List<LocalDate> getLocalDateByString(String season) {
		String year = season.substring(0, 4);
		Integer yearInt = Integer.parseInt(year);
		String type = season.substring(5, 6);
		if (type.equals("1")) {
			return Arrays.asList(LocalDate.of(yearInt, 1, 1), LocalDate.of(yearInt, 3, 31));
		}
		if (type.equals("2")) {
			return Arrays.asList(LocalDate.of(yearInt, 4, 1), LocalDate.of(yearInt, 6, 30));
		}
		if (type.equals("3")) {
			return Arrays.asList(LocalDate.of(yearInt, 7, 1), LocalDate.of(yearInt, 9, 30));
		}
		return Arrays.asList(LocalDate.of(yearInt, 10, 1), LocalDate.of(yearInt, 12, 31));
	}
	
	public static String getMediaEffect(float a, float b) {
		if (a == 0) {
			return AdTheatreEffectEnum.COMMON.getName();
		}
		if (a >= b*1.1) {
			return AdTheatreEffectEnum.GOOD.getName();
		}
		if (a >= b) {
			return AdTheatreEffectEnum.COMMON.getName();
		}
		if (a >= b*0.9) {
			return AdTheatreEffectEnum.NOT_BAD.getName();
		}
		return AdTheatreEffectEnum.BAD.getName();
	}

	public static String getSeasonByLocalDate(LocalDate localDate) {
		String localDateString = localDate.format(DateTimeFormatter.ISO_DATE);
		String season = localDateString.substring(0, 4) + "-";
		int month = localDate.getMonthValue();
		if (month <= 3) {
			return season + "1Q";
		}
		if (month <= 6) {
			return season + "2Q";
		}
		if (month <= 9) {
			return season + "3Q";
		}
		return season + "4Q";
	}
	
	public static void main(String[] args) {
		System.out.println(getLocalDateByString("2020-1Q"));
	}
}
