package com.cim.idm.wmsextend.generic.util;

import com.cim.idm.framework.util.time.TimeStampUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Random;


public class TimeStampUtils extends TimeStampUtil {
	private static Log log = LogFactory.getLog(TimeStampUtils.class);
	private ApplicationContext applicationContext;

	public void setApplicationContext(ApplicationContext arg0)
			throws BeansException {
		// TODO Auto-generated method stub
		applicationContext = arg0;
	}
	
	// 2019.03.07 - New Local Timekey
	//Do not use TimeStampUtil.getCurrentEventTimeKey()
	public static String getLocalCurrentEventTimeKey() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		
		Random rand = new Random();
		int iRand = rand.nextInt(999);
        return sdf.format(Long.valueOf(System.currentTimeMillis())) + String.format("%03d", iRand);
	}
	
	//20190804 Time Mismatch - Using getTimeStampCheckTimekey
	//Do not direct use TimeStampUtil.getTimestamp
	public static Timestamp getTimeStampCheckTimekey(String time) {
		
		if(time.length() == 20){
			String checkDBTimeValue = StringUtils.substring(time, 14, 17);
			if(StringUtils.equals(checkDBTimeValue, "000")){
				//TimeStamp(000 + ms)
			}else{
				//DB Timekey
				time = StringUtils.substring(time, 0, 17);
			}
		}
		Timestamp timestamp = TimeStampUtil.getTimestamp(time);
		return timestamp;
	}
}