package com.eactive.eai.rms.common.comDomain;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.eactive.eai.rms.common.base.OnlBaseService;
import com.eactive.eai.rms.common.combo.ComboService;
import com.eactive.eai.rms.common.datasource.DataSourceContextHolder;
import com.eactive.eai.rms.common.spring.LocaleMessage;
import com.eactive.eai.rms.data.entity.onl.adapter.CommonDomain;
import com.eactive.eai.rms.data.entity.onl.adapter.CommonDomainService;
import com.eactive.eai.rms.onl.common.exception.BizException;

@Service
@Transactional
public class CommonDomainManService extends OnlBaseService {

	@Autowired
	private CommonDomainDAO commonDomainDAO;

	@Autowired
	private ComboService comboService;

	@Autowired
	private CommonDomainService commonDomainService;

	@Autowired
	private CommonDomainUIMapper commonDomainUIMapper;

	@Autowired
	private LocaleMessage localeMessage;

	public Page<CommonDomainUI> selectList(Pageable pageable, CommonDomainUI commonDomainUI) {
		Page<CommonDomain> page = commonDomainService.findAll(pageable, commonDomainUI);
		return page.map(commonDomainUIMapper::toVo);
	}

	public Map<String, Object>[] selectListToExcel() {
		@SuppressWarnings("unchecked")
		Map<String, Object>[] mapArray = commonDomainService.findAll(Sort.by("domainNm")).stream().map(e -> {
			Map<String, Object> map = new HashMap<>();
			map.put("DOMAINNM", e.getDomainNm());
			map.put("DOMAINTYPE", e.getDomainType());
			map.put("DOMAINOPTION", e.getDomainOption());
			return map;
		}).toArray(HashMap[]::new);
		return mapArray;
	}

	public CommonDomainUI selectDetail(String domainNM) {
		CommonDomain commonDomain = commonDomainService.getById(domainNM);
		return commonDomainUIMapper.toVo(commonDomain);
	}

	public void update(CommonDomainUI commonDomainUI) {
		CommonDomain commonDomain = commonDomainService.getById(commonDomainUI.getDomainNM());
		commonDomainUIMapper.updateToEntity(commonDomainUI, commonDomain);
		commonDomainService.save(commonDomain);
	}

	public void insert(CommonDomainUI commonDomainUI) {
		if (commonDomainService.existsById(commonDomainUI.getDomainNM())) {
			throw new BizException(localeMessage.getString("domain.domainNMExists"));
		}
		CommonDomain commonDomain = commonDomainUIMapper.toEntity(commonDomainUI);
		commonDomainService.save(commonDomain);
	}

	public void delete(String domainNM) {
		commonDomainService.deleteById(domainNM);
	}

	public Map<String, Object> selectDomainInfo(String domainName) {
		HashMap<String, Object> ret = new HashMap<>();
		Optional<CommonDomain> domainOptional = commonDomainService.findById(domainName);

		if (!domainOptional.isPresent()) {
			return ret;
		}

		CommonDomain commonDomain = domainOptional.get();
		String value = commonDomain.getDomainVal();
		List<?> data = new ArrayList<>();

		if ("CODE".equals(commonDomain.getDomainOption())) {
			// code 에서 조회
			data = comboService.getFromCode(value);
		} else if ("SQL".equals(commonDomain.getDomainOption())) {
			// sql 을 직접 실행
			String schemaId = DataSourceContextHolder.getDataSourceType().getSchema();
			HashMap<String, String> param = new HashMap<>();
			value = value.replace("$schemaId$", schemaId);
			param.put("sql", value);
			data = commonDomainDAO.selectQuery(param);
		} else if ("MASK".equals(commonDomain.getDomainOption())) {
			// value 값을 사용
			List<String> maskData = new ArrayList<>();
			maskData.add(value);
			data = maskData;
		}

		ret.put("info", commonDomain);
		ret.put("data", data);

		return ret;
	}

}