package com.cim.idm.mwmsextend.transportjob.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.cim.idm.framework.util.time.TimeStampUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.cim.idm.mwmsextend.transport.TransportServiceProxy;
import com.cim.idm.mwmsextend.transport.management.data.TransportCommandDetail;
import com.cim.idm.mwmsextend.transport.management.data.TransportCommandDetailKey;
import com.cim.idm.mwmsextend.transport.management.data.TransportCommandKey;
import com.cim.idm.mwmsextend.transport.management.info.CreateInfo;
import com.cim.idm.mwmsextend.transport.management.info.SetDetailEventInfo;
import com.cim.idm.mwmsextend.transport.management.info.SetEventInfo;
import com.cim.idm.framework.data.EventInfo;
import com.cim.idm.framework.util.time.TimeUtils;


public class TransportJobServiceImpl {
	private static Log log = LogFactory.getLog(TransportJobServiceImpl.class);
			
	public void create(EventInfo eventInfo, CreateInfo createInfo, Map<String, Object> userColumns){
		if(StringUtils.isEmpty(createInfo.getTransportCommandID())){
			createInfo.setTransportCommandID("TRANSCMD-"+ TimeStampUtil.getCurrentEventTimeKey());
		}
		createInfo.setUserColumns(userColumns);
		TransportServiceProxy.getTransportCommandService().create(eventInfo, createInfo);
	}
	
	public void create(EventInfo eventInfo, List<CreateInfo> createInfoList){
		TransportServiceProxy.getTransportCommandService().create(eventInfo,createInfoList);
	}
	
	public void createDetail(List<TransportCommandDetail> detailList){
		TransportServiceProxy.getTransportCommandDetailService().insert(detailList);
	}
	
	public void setEvent(String transportCommandID , EventInfo eventInfo, Map<String , Object> userColumns){
		SetEventInfo setEventInfo = new SetEventInfo();
		setEventInfo.setUserColumns(userColumns);
		
		TransportCommandKey key = new TransportCommandKey();
		key.setTransportCommandID(transportCommandID);
		
		TransportServiceProxy.getTransportCommandService().setEvent(key, eventInfo, setEventInfo);
		
	}
	
	public void setDetailEvent(String transportCommandID ,String durableName, EventInfo eventInfo, Map<String , Object> userColumns){
		SetDetailEventInfo setEventInfo = new SetDetailEventInfo();
		setEventInfo.setUserColumns(userColumns);
		
		TransportCommandDetailKey key = new TransportCommandDetailKey();
		key.setTransportCommandID(transportCommandID);
		key.setDurableName(durableName);
		
		TransportServiceProxy.getTransportCommandDetailService().setEvent(key, eventInfo, setEventInfo);
		
	}
	
	public void setDetailEventByKey(List<TransportCommandDetailKey> keyList, EventInfo eventInfo, Map<String , Object> userColumns){
		SetDetailEventInfo setEventInfo = new SetDetailEventInfo();
		setEventInfo.setUserColumns(userColumns);
		
		TransportServiceProxy.getTransportCommandDetailService().setEvent(keyList, eventInfo, setEventInfo);
	}
	
	public void setDetailEventByData(List<TransportCommandDetail> transportCommandDetailList, EventInfo eventInfo, Map<String , Object> userColumns){
		List<TransportCommandDetailKey> keyList = new ArrayList<TransportCommandDetailKey>();
		for(TransportCommandDetail transportCommandDetail:transportCommandDetailList){
			keyList.add(transportCommandDetail.getKey());
		}
		
		setDetailEventByKey(keyList, eventInfo, userColumns);
	}
	
	public void update(List<TransportCommandDetail> detailList){
		TransportServiceProxy.getTransportCommandDetailService().update(detailList);
	}
	
	public void update(TransportCommandDetail detail){
		TransportServiceProxy.getTransportCommandDetailService().update(detail);
	}
}