/** 
 * AdminServiceImpl.java
 * @author 임새미 
 * @since 2016. 10. 28.
 *
 *  	 수정일                     수정자                                       수정내용
 *  -------------	--------	---------------------------
 *  2016. 10. 28. 	    임새미				       최초생성
 * 
 */
package kcg.imis.moniter.service;

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

import javax.annotation.Resource;

import kcg.imis.moniter.mapper.MoniterMapper;
import kcg.imis.moniter.vo.MoniterSearchVO;
import kcg.imis.moniter.vo.MoniterVO;

import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service("moniterService")
public class MoniterServiceImpl implements MoniterService {

	@Resource(name = "moniterMapper")
	private MoniterMapper moniterMapper;

	@Override
	public List<MoniterVO> getMoniterList(String idx) throws Exception {

		List<MoniterVO> list = moniterMapper.getMoniterList(idx);

		return list;
	}
	
	@Override
	public List<Map<String, Object>> getMoniterTreeList() throws Exception {

		 List<Map<String, Object>> list1 = createMoniterTreeList(1);
		
		for (Map<String, Object> depth1 : list1) {

			List<Map<String, Object>> list2 = createMoniterTreeList(depth1);
			
			for (Map<String, Object> depth2 : list2) {

				List<Map<String, Object>> list3 = createMoniterTreeList(depth2);
				
				for (Map<String, Object> depth3 : list3) {

					List<Map<String, Object>> list4 = createMoniterTreeList(depth3);
					
					for (Map<String, Object> depth4 : list4) {

						List<Map<String, Object>> list5 = createMoniterTreeList(depth4);
						
						for (Map<String, Object> depth5 : list5) {

							List<Map<String, Object>> list6 = createMoniterTreeList(depth5);
							
							for (Map<String, Object> depth6 : list6) {
								depth6.put("state", "open");
							}
							
							if(list6.size() == 0){
								depth5.put("state", "open");
							}
							depth5.put("children", list6);

						}
						if(list5.size() == 0){
							depth4.put("state", "open");
						}
						depth4.put("children", list5);

					}
					if(list4.size() == 0){
						depth3.put("state", "open");
					}
					depth3.put("children", list4);

				}
				if(list3.size() == 0){
					depth2.put("state", "open");
				}
				depth2.put("children", list3);

			}
			if(list2.size() == 0){
				depth1.put("state", "open");
			}
			depth1.put("children", list2);

		}
		
		return list1;

	}
	
	private List<Map<String, Object>> createMoniterTreeList(int depth ) throws Exception {
		
		MoniterSearchVO vo = new MoniterSearchVO();
		vo.setDepth(depth);

		List<Map<String, Object>> list1 = moniterMapper.getMoniterList(vo);
		return list1;
	}
	
	private List<Map<String, Object>> createMoniterTreeList(Map<String, Object> moniter) throws Exception {
		
		MoniterSearchVO vo = new MoniterSearchVO();
		
		int dep = Integer.parseInt(String.valueOf(moniter.get("depth"))) + 1;
		String idx = String.valueOf(moniter.get("id"));
		
		vo.setDepth(dep);
		vo.setIdx(idx);

		return moniterMapper.getMoniterList(vo);
	}

	@Override
	public MoniterVO saveMoniter(MoniterVO vo) throws Exception {
		
		String id = vo.getJmIdx();
		
		int result = 0;
		
		if(!StringUtils.isEmpty(id)){
			result =  moniterMapper.updateMoniterInfo(vo);
		}else{
			//신규
			MoniterVO insertVo = moniterMapper.getMoniterInfo(vo.getParentId());
			int idx = moniterMapper.getMaxIdx();
			int depth = Integer.parseInt(insertVo.getJmDepth()) + 1;
			if(depth == 2){
				insertVo.setJmDepth2Idx(String.valueOf(idx));
			} else if(depth == 3){
				insertVo.setJmDepth3Idx(String.valueOf(idx));
			} else if(depth == 4){
				insertVo.setJmDepth4Idx(String.valueOf(idx));
			} else if(depth == 5){
				insertVo.setJmDepth5Idx(String.valueOf(idx));
			} else if(depth == 6){
				insertVo.setJmDepth6Idx(String.valueOf(idx));
			}
			
			insertVo.setJmIdx(String.valueOf(idx));
			insertVo.setJmDepth(String.valueOf(depth));
			insertVo.setJmOrder(vo.getJmOrder());
			insertVo.setJmName(vo.getJmName());
			insertVo.setJmHref(vo.getJmHref());
			
			result = moniterMapper.insertMoniterInfo(insertVo);
			
			id = String.valueOf(idx);
		}
		
		if(result == 1){
			return moniterMapper.getMoniterInfo(id);
		}
		else{
			return new MoniterVO();
		}
	}

	@Override
	public int deleteMoniterTree(MoniterSearchVO vo) throws Exception {
		return moniterMapper.deleteMoniterTree(vo);
	}
}
