package com.eactive.eai.rms.onl.manage.serviceinfo.itsm;

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

import com.eactive.eai.rms.common.base.BaseService;
import com.eactive.eai.rms.common.datasource.DataSourceContextHolder;
import com.eactive.eai.rms.common.datasource.DataSourceType;
import com.eactive.eai.rms.common.datasource.DataSourceTypeManager;
import com.eactive.eai.rms.onl.dashboard.service.Dashboard2RepositoryService;
import com.eactive.eai.rms.onl.manage.serviceinfo.itsm.vo.ItsmRow;
import com.eactive.eai.rms.onl.manage.serviceinfo.itsm.vo.ItsmRows;
import com.eactive.eai.rms.onl.tracking.vo.MemStaticsVo;

@Service
public class ItsmOnlineService extends BaseService {
	
	@Autowired
	private Dashboard2RepositoryService dashboardRepoService;
	
	protected ItsmRows getStatus() {
		
		ItsmRows rows = new ItsmRows();
		
		for(DataSourceType datasource : DataSourceTypeManager.getOnlineDataSourceTypes()) {
			
			ItsmRow row = newRow(datasource);
			rows.add(row);
			logger.debug("Online Data : " + row.toString());
			
		}
		
		return rows;
	}

	private ItsmRow newRow(DataSourceType datasource) {
		
		try {
			DataSourceContextHolder.setDataSourceType(datasource);
			
			String datasourceName = datasource.getName();
			
			MemStaticsVo memTradeVoByService = dashboardRepoService.getMemTradeVoByService(datasourceName);
			
			return ItsmRow.newOnlineInstance(datasourceName, memTradeVoByService);
			
		} catch (Exception e) {
			
			ItsmRow errorRow = new ItsmRow(e);
			logger.debug("ITSM ONLINE ERROR : " + e.getMessage());
			
			return errorRow;
		}
		
	}

}
