package itn.com.cmm.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Date;

/**
 * 
 * @author 		: 이호영
 * @fileName 	: DateUtil.java 
 * @date 		: 2023.04.06
 * @description : Date 다루는 Util
 * =========================================================== 
 * DATE          AUTHOR   NOTE 
 * ----------------------------------------------------------- *
 * 2023.04.06    이호영          최초 생성
 * 
 * 
 * 
 */
public final class DateUtil {

    // 날짜를 포맷하는 포맷터 정의
    private static final DateTimeFormatter SLUSH_FORMATTER = DateTimeFormatter.ofPattern("yyyy/MM/dd");

    /** 
     * @methodName	: getTodayYearAndMonthAndFirstDay 
     * @author		: 이호영
     * @date		: 2023.04.06 
     * @description	: 해당 년월일 구하기 : 일은 1일
     * @return ex) 2023/04/01
     */
    public static String getTodayYearAndMonthAndFirstDay() {
    	LocalDate currentDate = LocalDate.now();
    	return LocalDate.of(currentDate.getYear(), currentDate.getMonthValue(), 1)
    			.format( DateTimeFormatter.ofPattern("yyyy/MM/dd"));
    }
    
    /** 
     * @methodName	: getTodayYearAndMonthAndLastDay 
     * @author		: 이호영
     * @date		: 2023.04.06 
     * @description	: 해당 년월일 구하기 : 일은 마지막일
     * @return ex) 2023/04/30
     */
    public static String getTodayYearAndMonthAndLastDay() {
    	LocalDate currentDate = LocalDate.now();
    	return LocalDate.of(currentDate.getYear(), currentDate.getMonthValue(), currentDate.lengthOfMonth())
    			.format( DateTimeFormatter.ofPattern("yyyy/MM/dd"));
    }
    
    
    /** 
     * @methodName	: getNowYearToString 
     * @author		: 이호영
     * @date		: 2023.04.07 
     * @description	: 현재 연도 
     * @return : String
     */
    public static String getNowYearToString() {
    	return Integer.toString(LocalDate.now().getYear());
    }
    
    /** 
     * @methodName	: getNowMonthToString 
     * @author		: 이호영
     * @date		: 2023.04.07 
     * @description	: 현재 월
     * @return : String
     */
    public static String getNowMonthToString() {
    	LocalDate currentDate = LocalDate.now();
    	return Integer.toString(currentDate.getMonthValue());
    }
    
    /** 
     * @methodName	: getNowDayToString 
     * @author		: 이호영
     * @date		: 2023.04.07 
     * @description	: 현재 일
     * @return : String
     */
    public static String getNowDayToString() {
    	LocalDate currentDate = LocalDate.now();
    	return Integer.toString(currentDate.getDayOfMonth());
    }
    
    
    
    /** 
     * @methodName	: getNowDayToString 
     * @author		: 이호영
     * @date		: 2023.08.08 
     * @description	: yyyy-MM-dd HH:mm:ss.S 형식을 yyyy-MM-dd HH:mm 로 변환
     * @return 
     * @throws ParseException 
     */
    public static String getChangFormatS(String str) throws ParseException {
	    SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S"); // 원본 형식
	    Date date = inputFormat.parse(str); // 문자열을 날짜 객체로 변환
	
	    SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm"); // 원하는 출력 형식
	    String formattedDate = outputFormat.format(date); // 날짜 객체를 원하는 형식의 문자열로 변환
	    
	    return formattedDate; // 출력: 2023-08-07 09:36
    }

    
    // 현재 날짜를 기본 포맷으로 반환
    public static String getCurrentDate() {
        return getCurrentDate(SLUSH_FORMATTER);
    }

    // 현재 날짜를 지정된 포맷으로 반환
    public static String getCurrentDate(DateTimeFormatter formatter) {
        LocalDate today = LocalDate.now();
        return today.format(formatter);
    }

    // 현재 날짜에서 특정 일수 전 날짜를 기본 포맷으로 반환
    public static String getDateDaysAgo(int days) {
        return getDateDaysAgo(days, SLUSH_FORMATTER);
    }

    // 현재 날짜에서 특정 일수 전 날짜를 지정된 포맷으로 반환
    public static String getDateDaysAgo(int days, DateTimeFormatter formatter) {
        LocalDate today = LocalDate.now();
        LocalDate daysAgo = today.minusDays(days);
        return daysAgo.format(formatter);
    }

	
	public static boolean dateChk365AndValueChk(String searchStartDate, String searchEndDate) {
		

		boolean isValid = true;

		// 날짜 형식 지정
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");

		//날짜 검증
		LocalDate startDate = null;
		LocalDate endDate = null;

		// 검색 시작일자와 종료일자가 있는지 체크
		if (searchStartDate == null || searchStartDate.isEmpty() || searchEndDate == null || searchEndDate.isEmpty()) {
			isValid = false;
		}

		// 날짜 형식으로 변환
		if (isValid) {
			try {
				startDate = LocalDate.parse(searchStartDate, formatter);
				endDate = LocalDate.parse(searchEndDate, formatter);
			} catch (Exception e) {
				isValid = false;
			}
		}

		// 시작일자가 종료일자보다 이후인지 확인
		if (isValid && startDate.isAfter(endDate)) {
			isValid = false;
		}

		// 총 기간이 365일을 넘는지 확인
		if (isValid) {
			long daysBetween = ChronoUnit.DAYS.between(startDate, endDate);
			if (daysBetween > 365) {
				isValid = false;
			}
		}
		
		return isValid;
	}
}
