package com.eactive.eai.adapter.socket2.common;

import com.eactive.eai.common.util.DatetimeUtil;
import org.apache.commons.lang3.StringUtils;

import java.text.MessageFormat;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class TimeUtil {
    private static final MessageFormat ELAPSED_TIME_FORMAT = new MessageFormat("{0}.{1}.{2}");
    public static String elapsedTime(long time) {
    	long elapsed = System.currentTimeMillis() - time;
        long seconds = elapsed / 1000;
        long minutes = seconds / 60;
        long hours = minutes / 60;
        Object[] args = {
    		StringUtils.leftPad(Long.toString(hours), 2, '0'), 
    		StringUtils.leftPad(Long.toString(minutes%60), 2, '0'), 
    		StringUtils.leftPad(Long.toString(seconds%60), 2, '0')
        };
        return ELAPSED_TIME_FORMAT.format(args);
    }    
    
    public static String formatDate(long time) {
    	return DatetimeUtil.getFormattedDate("MM-dd_HH:mm:ss", new Date(time));
    }
    
    /**
     * 
     * @param prop [BUSINESS_DAY|ALL_WEEK],%d{HHmmss},%d{HHmmss} - BusinessDay,StartTime,EndTime
     * @return whether SMS sending
     */
    public static boolean isSMSRunningTime(String prop) {
    	String props[] = prop.split(",");
    	if (props.length < 3) return true;
    	
    	int dayOfWeek = DatetimeUtil.getDayOfWeekNum();
    	if (props[0].equals("BUSINESS_DAY") && dayOfWeek == 1 || dayOfWeek == 7) return false;
    	
    	String currentTime = DateTimeFormatter.ofPattern("HHmmss").format(ZonedDateTime.now());
    	if (Integer.parseInt(currentTime) < Integer.parseInt(props[1]) ||
    			Integer.parseInt(currentTime) > Integer.parseInt(props[2])) return false;

    	return true;
    }
}
