package com.eactive.eai.batch.eventscheduler.util;

import com.ibm.icu.util.Calendar ;
import com.ibm.icu.util.ChineseCalendar ;

public final class LunarCalendar {
	private static final int CHINESE_DIFF = 2637;
	
	public LunarCalendar() {
		//
	}
	
	
	/**
	 * 양력을 음력으로 변환
	 *
	 */
	public static  String toLunar( String lunarDateStr ) {
		if(  lunarDateStr == null || lunarDateStr.length()  !=8 ) {
			return "" ;
		}
		
		Calendar cal = Calendar.getInstance() ;
		ChineseCalendar cc = new ChineseCalendar();
		
		cal.set( Calendar.YEAR, Integer.parseInt(lunarDateStr.substring(0,4)) ) ;
		cal.set( Calendar.MONTH, Integer.parseInt(lunarDateStr.substring(4,6))-1 ) ;
		cal.set( Calendar.DAY_OF_MONTH, Integer.parseInt(lunarDateStr.substring(6)) ) ;
		
		cc.setTimeInMillis( cal.getTimeInMillis() ) ;
		
		
		int y = cc.get(ChineseCalendar.EXTENDED_YEAR)- CHINESE_DIFF ;
		int m = cc.get(ChineseCalendar.MONTH)+1 ;
		int d = cc.get(ChineseCalendar.DAY_OF_MONTH) ;
		
		StringBuffer sb = new StringBuffer() ;		
		
		sb.append( y ) ;		
		
		if( m < 10 ) sb.append( "0" ) ;
		sb.append( m ) ;
		
		if( d < 10 ) sb.append( "0" ) ;
		sb.append( d ) ;
		
		return sb.toString() ;
	}
	
	
	/**
	 * 음력을 양력으로 변환
	 *
	 */
	public static String toSolar( String solarDateStr ) {
		if(  solarDateStr == null || solarDateStr.length()  !=8 ) {
			return "" ;
		}
		
		Calendar cal = Calendar.getInstance() ;
		ChineseCalendar cc = new ChineseCalendar();
		
		cc.set( ChineseCalendar.EXTENDED_YEAR, Integer.parseInt(solarDateStr.substring(0,4)) + CHINESE_DIFF ) ;
		cc.set( ChineseCalendar.MONTH, Integer.parseInt(solarDateStr.substring(4,6))-1 ) ;
		cc.set( ChineseCalendar.DAY_OF_MONTH, Integer.parseInt(solarDateStr.substring(6)) ) ;
		
		cal.setTimeInMillis( cc.getTimeInMillis() ) ;
		
		int y = cal.get(Calendar.YEAR) ;
		int m = cal.get(Calendar.MONTH) + 1 ;
		int d = cal.get(Calendar.DAY_OF_MONTH) ;
		
		StringBuffer sb = new StringBuffer() ;		
		
		sb.append( y ) ;		
		
		if( m < 10 ) sb.append( "0" ) ;
		sb.append( m ) ;
		
		if( d < 10 ) sb.append( "0" ) ;
		sb.append( d ) ;
		
		return sb.toString() ;
		
	}
	
	/**
	public static String[] toSolarOfNationalHoliday (String date){
		
		String[] nholiday = new String[3];
		
		int dateInt = (Integer.parseInt(date))-1;	//음력날짜 -1
		try {
			
		}catch (Exception e){
			e.printStackTrace();
		}
		//지정된 음력날짜 앞뒤까지 휴일로 설정하기 위함.
		for (int i=0; i<nholiday.length; i++){
			nholiday[i]=toSolar(""+dateInt);	
			dateInt++;
		}
		return nholiday;

	}*/
	
	public static String toSolarOfNationalHoliday (String date){
		String nholiday = new String();
		try {
			nholiday=toSolar(date);	
		}catch (Exception e){
			e.printStackTrace();
		}
		
		return nholiday;

	}
	
//	public static void main(String args[]) throws Exception {
////		LunarCalendar lc = new LunarCalendar() ;
////		String lunarDate = lc.toLunar("20080627") ;
////		String date = lc.toSolar("20080524") ;		
////		System.out.println("음력으로 20080627 ->"+lunarDate);
////		System.out.println("양력으로 20080524 ->"+date);
//		
////		 MultiThread Test
//    	new Thread("T1") { 
//            public void run() { 
//            	for(int i=0; i<100; i++) {
//					LunarCalendar lc = new LunarCalendar() ;
//					String lunarDate = lc.toLunar("20080627") ;
//					String date = lc.toSolar("20080524") ;		
//					System.out.println("T1 음력으로 20080627 ->"+lunarDate);
//					System.out.println("T1 양력으로 20080524 ->"+date);
//            	}
//            } 
//         }.start(); // T1 쓰레드 시작 
//
//         new Thread("T2") { 
//            public void run() { 
//            	for(int i=0; i<100; i++) {
//					LunarCalendar lc = new LunarCalendar() ;
//					String lunarDate = lc.toLunar("20080626") ;
//					String date = lc.toSolar("20080523") ;		
//					System.out.println("T2 음력으로 20080626 ->"+lunarDate);
//					System.out.println("T2 양력으로 20080523 ->"+date);
//            	}
//            } 
//         }.start(); // T2 쓰레드 시작
//	}

}
