package com.eactive.eai.transformer.dao.jdbc;

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

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

import com.eactive.eai.data.entity.onl.transformer.layout.LayoutTypeEntity;
import com.eactive.eai.transformer.dao.LayoutTypeDao;
import com.eactive.eai.transformer.layout.LayoutType;
import com.eactive.eai.transformer.layout.loader.LayoutTypeLoader;
import com.eactive.eai.transformer.layout.mapper.LayoutTypeMapper;

@Service
@Transactional
public class LayoutTypeJdbcDao extends TransDao implements LayoutTypeDao {

	@Autowired
	private LayoutTypeLoader layoutTypeLoader;

	@Autowired
	private LayoutTypeMapper layoutTypeMapper;

	public Map<String, LayoutType> load() {
		if (logger.isInfoEnabled()) {
			logger.info("LayoutTypeJdbcDao | loading... ");
		}

		Map<String, LayoutType> map = new TreeMap<>();
		List<LayoutTypeEntity> layoutTypeEntities = layoutTypeLoader.findAll();

		for (LayoutTypeEntity layoutTypeEntity : layoutTypeEntities) {
			LayoutType layoutType = layoutTypeMapper.toVo(layoutTypeEntity);
			map.put(layoutType.getName(), layoutType);
		}

		if (logger.isDebugEnabled()) {
			logger.debug("LayoutTypeJdbcDao | LayouType 정보를 Load 했습니다. - " + map.size());
		}

		return map;
	}

	public void clearAllLayoutTypes() {
		layoutTypeLoader.deleteAll(layoutTypeLoader.findAll());
	}

	public LayoutType findLayoutTypeByName(String loutPtrnName) {
		return layoutTypeLoader.findById(loutPtrnName).map(layoutTypeMapper::toVo).orElse(null);
	}

}
