package com.eactive.eai.message.layout;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.util.ArrayList;

import com.eactive.eai.common.util.Logger;
import com.eactive.eai.message.StandardItem;
import com.eactive.eai.message.StandardMessage;
import com.eactive.eai.message.StandardMessageUtil;

public class CsvFileReader implements LayoutReader {
	static Logger logger = Logger.getLogger(Logger.LOGGER_DEFAULT);
	String COMMA_DELIMITER = ",";
	String COMMENT_HEADER = "#";
	
	public CsvFileReader() {
	}
	
	private BufferedReader getReader(String filePath) throws Exception {
		BufferedReader reader = null;
		if(logger.isInfoEnabled()) logger.info("getReader from filepath. read file = " + filePath);
		try {
			reader = new BufferedReader(new FileReader(filePath));
			return reader;
		}
		catch(Exception ex) {
			if(logger.isWarnEnabled()) {
//				logger.warn("getReader failed. read file =  " +filePath, ex);
				logger.warn("getReader failed. read file =  " +filePath +", " +  ex.getMessage());
			}
		}
		try {
			if(logger.isInfoEnabled()) logger.info("getReader from classpath. read file = " + filePath);
			ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
			reader = new BufferedReader(new InputStreamReader(classLoader.getResourceAsStream(filePath)) );			
			return reader;
		}
		catch(Exception ex) {
			if(logger.isWarnEnabled()) {
//				logger.warn("getReader failed. read classpath file = " + filePath, ex);
				logger.warn("getReader failed. read classpath file = " + filePath + ", " + ex.getMessage());
			}
		}
		throw new Exception("file not found : " + filePath);
	}
	
	@Override
	public ArrayList<StandardItem> parse(String filePath) throws Exception {
		if(logger.isInfoEnabled()) logger.info("CsvFileReader read file = " + filePath);
		
		ArrayList<StandardItem>  list = new ArrayList<StandardItem>();

		int lineNo = 0;
		try (BufferedReader br = getReader(filePath)) {
		    String line;
		    while ((line = br.readLine()) != null) {
		    	lineNo++;
		    	if(line.startsWith(COMMENT_HEADER)) {
		    		if(logger.isDebugEnabled()) logger.debug(String.format("SKIP comment %s : %s", lineNo, line) );
		    		continue;
		    	}
		    	if(logger.isDebugEnabled()) logger.debug(String.format("Parsing line %s : %s", lineNo, line) );
		        String[] values = line.split(COMMA_DELIMITER, StandardItem.ITEM_COUNT);
		        StandardItem item = new StandardItem(values);		        
	            list.add(item);
		    }
		}
		catch(Exception ex) {
        	throw new Exception(String.format("csv file %s parsing error lineNo=%d", filePath, lineNo), ex);
        }
		return list;
	}
	
	public static void main(String[] args) {
		ArrayList<StandardItem> list = null;
		String filePath = "./resources/standard-layout.csv";
		try {
			LayoutReader reader = new CsvFileReader();
			list = (ArrayList<StandardItem>) reader.parse(filePath);
		}
		catch(Exception ex) {
			ex.printStackTrace();
			return;
		}
		StandardMessage message = null;;
		try {
			message = StandardMessageUtil.generate(list);
			logger.debug("@toCSVString\n" + message.toCSVString());
			logger.debug("@toString\n" + message.toString());
			logger.debug("@toJson\n" + message.toJson());
		} catch (Exception e) {
			e.printStackTrace();			
		}
	}
}
