package com.eactive.eai.adapter.socket2.protocol.inisafe2020;

import com.eactive.eai.adapter.socket2.protocol.ISocketAdapter;
import org.apache.mina.common.IoSession;

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class InisafeClientHandler extends InisafeHandler {
	public InisafeClientHandler(ISocketAdapter adapter) {
		super(adapter);
	}
	
	public void sessionOpened(IoSession session) throws Exception {
		super.sessionOpened(session);
	}

	public void sessionClosed(IoSession session) throws Exception {
		super.sessionClosed(session);
		/**
		 * client-server pair 로 작업한다.
		 */
		doSessionClosed(session);
	}
	
	protected String messageReplace (String str){
		Matcher matcher = Pattern.compile("%d\\{([a-zA-Z \\-]+)\\}").matcher(str);
		
		while (matcher.find()) { 
		    String datePattern = matcher.group(1);
		    DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern(datePattern);
		    str = str.replaceFirst("%d\\{([a-zA-Z \\-]+)\\}", dateFormat.format(ZonedDateTime.now()));
		}		
		return str;		
	}	
}
