package com.eactive.eai.rms.onl.loader.service;

import java.util.ArrayList;
import java.util.HashMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.eactive.eai.common.util.NullControl;
import com.eactive.eai.rms.common.base.BaseService;
import com.eactive.eai.rms.common.datasource.DataSourceType;
import com.eactive.eai.rms.onl.common.util.FileWriteUtil;
import com.eactive.eai.rms.onl.loader.dao.HistoryDao;

@Service("historyService")
public class HistoryServiceImpl extends BaseService implements HistoryService {

    @Autowired
    @Qualifier("historyDao")
    private HistoryDao historyDao;
   
    
    public HistoryServiceImpl() { 
        super();
    }

    /**
     * layout loading
     */
    public HashMap<String, ArrayList<HashMap<String, String>>> userHistoryList(DataSourceType dt, String userId)
    throws Exception {
    	HashMap<String, ArrayList<HashMap<String, String>>> list = new HashMap<String, ArrayList<HashMap<String, String>>>();
    	ArrayList<HashMap<String, String>> iList = historyDao.getEaiMessageList(dt, userId);
    	if( !iList.isEmpty() ) list.put(FileWriteUtil.INTERFACE, iList);
    	ArrayList<HashMap<String, String>> lList = historyDao.getLayoutList(dt, userId);
    	if( !lList.isEmpty() ) list.put(FileWriteUtil.LAYOUT, lList);
    	ArrayList<HashMap<String, String>> tList = historyDao.getTransformList(dt, userId);
    	if( !tList.isEmpty() ) list.put(FileWriteUtil.TRANSFORM, tList);
    	
    	return list;
    }
    
    /**
	 * layout loading
	 */
	public HashMap<String, ArrayList<HashMap<String, String>>> userHistoryListByInterface(DataSourceType dt, String interfaceId)
			throws Exception {
		HashMap<String, ArrayList<HashMap<String, String>>> list = new HashMap<String, ArrayList<HashMap<String, String>>>();
		ArrayList<HashMap<String, String>> iList = historyDao.getMessageListByInterface(dt, interfaceId);
		if( !iList.isEmpty() ) list.put(FileWriteUtil.INTERFACE, iList);
		ArrayList<HashMap<String, String>> lList = historyDao.getLayoutListByInterface(dt, interfaceId);
		if( !lList.isEmpty() ) list.put(FileWriteUtil.LAYOUT, lList);
		ArrayList<HashMap<String, String>> tList = historyDao.getTransformListByInterface(dt, interfaceId);
		if( !tList.isEmpty() ) list.put(FileWriteUtil.TRANSFORM, tList);

		return list;
	}
  
    /**
업무코드         10
작성자사번      10
프로그램명     200
프로그램설명  1000 
디렉토리        500
프로그램종류  100
언어             100
관리자서번      10
*/
	public String parse(String serviceType, String iomapPath, String userId, HashMap<String, ArrayList<HashMap<String, String>>> list) throws Exception {
		String delimeter = "\t";

		StringBuffer buffer = new StringBuffer();
				
		if( list.get(FileWriteUtil.INTERFACE) != null && list.get(FileWriteUtil.INTERFACE).size() > 0 ) {
			for(HashMap<String, String> data : list.get(FileWriteUtil.INTERFACE)) {
				buffer.append(data.get("bizCode").trim()).append(delimeter);
				buffer.append(data.get("UpdateBy")).append(delimeter);
				buffer.append(data.get("programNm")).append(delimeter);
				buffer.append(NullControl.trimSpace(data.get("programDesc"))).append(delimeter);
				buffer.append(iomapPath).append(serviceType).append("/").append(data.get("bizCode").trim()).append("/").append(FileWriteUtil.INTERFACE).append(delimeter);
				buffer.append(serviceType).append("-인터페이스").append(delimeter);
				buffer.append("전문").append(delimeter);
				buffer.append("").append(delimeter);
				buffer.append("\n");
			}
		}

		if( list.get(FileWriteUtil.LAYOUT) != null && list.get(FileWriteUtil.LAYOUT).size() > 0 ) {
			for(HashMap<String, String> data : list.get(FileWriteUtil.LAYOUT)) {
				buffer.append(data.get("bizCode")).append(delimeter);
				buffer.append(data.get("UpdateBy")).append(delimeter);
				buffer.append(data.get("programNm")).append(delimeter);
				buffer.append(NullControl.trimSpace(data.get("programDesc"))).append(delimeter);
				buffer.append(iomapPath).append(serviceType).append("/").append(data.get("bizCode")).append("/").append(FileWriteUtil.LAYOUT).append(delimeter);
				buffer.append(serviceType).append("-레이아웃로드").append(delimeter);
				buffer.append("전문").append(delimeter);
				buffer.append("").append(delimeter);
				buffer.append("\n");
			}
		}

		if( list.get(FileWriteUtil.TRANSFORM) != null && list.get(FileWriteUtil.TRANSFORM).size() > 0 ) {
			for(HashMap<String, String> data : list.get(FileWriteUtil.TRANSFORM)) {
				buffer.append(data.get("bizCode")).append(delimeter);
				buffer.append(data.get("UpdateBy")).append(delimeter);
				buffer.append(data.get("programNm")).append(delimeter);
				buffer.append(NullControl.trimSpace(data.get("programDesc"))).append(delimeter);
				buffer.append(iomapPath).append(serviceType).append("/").append(data.get("bizCode")).append("/").append(FileWriteUtil.TRANSFORM).append(delimeter);
				buffer.append(serviceType).append("-변환로드").append(delimeter);
				buffer.append("전문").append(delimeter);
				buffer.append("").append(delimeter);
				buffer.append("\n");
			}
		}
		return buffer.toString();
	}
	
	public String parseSimple(String serviceType, String iomapPath, HashMap<String, ArrayList<HashMap<String, String>>> list) throws Exception {
		String delimeter = "|";

		StringBuffer buffer = new StringBuffer();

		if( list.get(FileWriteUtil.INTERFACE) != null && list.get(FileWriteUtil.INTERFACE).size() > 0 ) {
			for(HashMap<String, String> data : list.get(FileWriteUtil.INTERFACE)) {
				buffer.append(data.get("bizCode").trim()).append(delimeter);
				buffer.append(data.get("UpdateBy")).append(delimeter);
				buffer.append(data.get("programNm")).append(delimeter);
				buffer.append(iomapPath).append(serviceType).append("/").append(data.get("bizCode").trim()).append("/").append(FileWriteUtil.INTERFACE).append(delimeter);
				buffer.append(FileWriteUtil.INTERFACE).append(delimeter);
				buffer.append("\n");
			}
		}

		if( list.get(FileWriteUtil.LAYOUT) != null && list.get(FileWriteUtil.LAYOUT).size() > 0 ) {
			for(HashMap<String, String> data : list.get(FileWriteUtil.LAYOUT)) {
				buffer.append(data.get("bizCode")).append(delimeter);
				buffer.append(data.get("UpdateBy")).append(delimeter);
				buffer.append(data.get("programNm")).append(delimeter);
				buffer.append(iomapPath).append(serviceType).append("/").append(data.get("bizCode")).append("/").append(FileWriteUtil.LAYOUT).append(delimeter);
				buffer.append(FileWriteUtil.LAYOUT).append(delimeter);
				buffer.append("\n");
			}
		}

		if( list.get(FileWriteUtil.TRANSFORM) != null && list.get(FileWriteUtil.TRANSFORM).size() > 0 ) {
			for(HashMap<String, String> data : list.get(FileWriteUtil.TRANSFORM)) {
				buffer.append(data.get("bizCode")).append(delimeter);
				buffer.append(data.get("UpdateBy")).append(delimeter);
				buffer.append(data.get("programNm")).append(delimeter);
				buffer.append(iomapPath).append(serviceType).append("/").append(data.get("bizCode")).append("/").append(FileWriteUtil.TRANSFORM).append(delimeter);
				buffer.append(FileWriteUtil.TRANSFORM).append(delimeter);
				buffer.append("\n");
			}
		}
		
		if( list.get(FileWriteUtil.ADAPTER) != null && list.get(FileWriteUtil.ADAPTER).size() > 0 ) {
			for(HashMap<String, String> data : list.get(FileWriteUtil.ADAPTER)) {
				buffer.append(data.get("bizCode")).append(delimeter);
				buffer.append(data.get("UpdateBy")).append(delimeter);
				buffer.append(data.get("programNm")).append(delimeter);
				buffer.append(iomapPath).append(serviceType).append("/").append(data.get("bizCode")).append("/").append(FileWriteUtil.TRANSFORM).append(delimeter);
				buffer.append(FileWriteUtil.ADAPTER).append(delimeter);
				buffer.append("\n");
			}
		}
		
		return buffer.toString();
	}

	@Override
	public HashMap<String, ArrayList<HashMap<String, String>>> userHistoryListByLayout(DataSourceType dt,
			String[] eaiLoutNames) throws Exception {
		HashMap<String, ArrayList<HashMap<String, String>>> list = new HashMap<String, ArrayList<HashMap<String, String>>>();
		ArrayList<HashMap<String, String>> lList = historyDao.getLayoutListByLyoutNames(dt, eaiLoutNames);
		if (!lList.isEmpty()) {
			list.put(FileWriteUtil.LAYOUT, lList);
		}
		return list;
	}

	@Override
	public HashMap<String, ArrayList<HashMap<String, String>>> userHistoryListByTransform(DataSourceType dt,
			String[] cnvsnNames) throws Exception {
		HashMap<String, ArrayList<HashMap<String, String>>> list = new HashMap<String, ArrayList<HashMap<String, String>>>();
		ArrayList<HashMap<String, String>> lList = historyDao.getTranstormListByTransformNames(dt, cnvsnNames);
		if (!lList.isEmpty()) {
			list.put(FileWriteUtil.TRANSFORM, lList);
		}
		return list;
	}
	
	@Override
	public HashMap<String, ArrayList<HashMap<String, String>>> userHistoryListByAdapter(DataSourceType dt,
			String[] adapterNames) throws Exception {
		HashMap<String, ArrayList<HashMap<String, String>>> list = new HashMap<String, ArrayList<HashMap<String, String>>>();
		ArrayList<HashMap<String, String>> lList = historyDao.getAdapterListByAdapterNames(dt, adapterNames);
		if (!lList.isEmpty()) {
			list.put(FileWriteUtil.ADAPTER, lList);
		}
		return list;
	}
}