package com.dbs.mplus.service;

import java.lang.reflect.Field;

import javax.annotation.PostConstruct;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.dbs.mplus.framework.FrameworkConstants;
import com.dbs.mplus.framework.service.ConfigureService;

@Component  
public class MPlusConfigureService extends ConfigureService {
	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	/* ========================================================================
		동의서 이미지 경로
	======================================================================== */ 		
	
	// 동의서 이미지 경로
	@Value("#{mplusConfig['consent.image.path'] ?: 'http://consent.fatima.or.kr/DataTempImage/'}")
	private String consentImagePath; 
	
	
	public String getConsentImagePath() {
		return consentImagePath;
	}

	public void setConsentImagePath(String consentImagePath) {
		this.consentImagePath = consentImagePath;
	}

	@PostConstruct 
	public void afterPropertiesSet() { 
		if(logger.isInfoEnabled()) { 
			StringBuilder builder = new StringBuilder(FrameworkConstants.NEW_LINE); 
			
			builder.append("병원전용환경설정 --------------------------------------------------------").append(FrameworkConstants.NEW_LINE); 
			builder.append(this.toString()).append(FrameworkConstants.NEW_LINE); 
			builder.append("--------------------------------------------------------------------").append(FrameworkConstants.NEW_LINE); 
		
		
			logger.info(builder.toString());
		} 
	}
	
	@Override 
	public String toString() { 
		StringBuilder builder = new StringBuilder(); 
		
		Class<MPlusConfigureService> clazz = MPlusConfigureService.class; 
		// 모든 필드를 구하고 
		Field[] fields = clazz.getDeclaredFields(); 
		// 필드 기본값 
		final String defaultValue = "UNKNOWN"; 
		
		// 돌아가면서 값 확인 
		for(Field field : fields) { 
			// annotation 확인 
			Value value = field.getAnnotation(Value.class); 
			// 값 확인 
			Object fieldValue = defaultValue; 
			
			try { 
				// 값 확인 
				fieldValue = field.get(this); 
			}
			catch(Exception ex) { 
				// 의도적인 예외 무시 
				if(logger.isDebugEnabled()) {
					logger.debug("무시 : " + field.getName());
				}
			}
			
			// super class에 들어있는 값이 기본값으로 찍히는 것을 막기위함. 
			if(value == null || defaultValue.equals(fieldValue)) {
				continue; 
			}
			
			builder.append(String.format("%65s = %-60s", value.value(), fieldValue)).append(FrameworkConstants.NEW_LINE); 
		}
		
		return builder.toString();
	}	
}
