package com.eactive.eai.rms.onl.manage.adapter.adaptertype;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

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.data.entity.onl.adapter.AdapterType;
import com.eactive.eai.rms.common.base.BaseService;
import com.eactive.eai.rms.data.entity.onl.adapter.AdapterTypeProperty;
import com.eactive.eai.rms.data.entity.onl.adapter.AdapterTypePropertyService;
import com.eactive.eai.rms.data.entity.onl.adaptertype.AdapterTypeService;

@Service
@Transactional
public class AdapterTypeManService extends BaseService {

	private AdapterTypeService adapterTypeService;

	private AdapterTypePropertyService adapterTypePropertyService;

	private AdapterTypeUIMapper adapterTypeUIMapper;

	private AdapterTypePropertyUIMapper adapterTypePropertyUIMapper;
	
	@Autowired
	public AdapterTypeManService(AdapterTypeService adapterTypeService, AdapterTypePropertyService adapterTypePropertyService, 
											   AdapterTypeUIMapper adapterTypeUIMapper, AdapterTypePropertyUIMapper adapterTypePropertyUIMapper) {
		this.adapterTypeService = adapterTypeService;
		this.adapterTypePropertyService = adapterTypePropertyService;
		this.adapterTypeUIMapper = adapterTypeUIMapper;
		this.adapterTypePropertyUIMapper = adapterTypePropertyUIMapper;
	}

	
	public Page<AdapterTypeUI> selectList(Pageable pageable, AdapterTypeUI adapterTypeUI) {
		return adapterTypeService.findAll(pageable, adapterTypeUI.getSearchAdptrCd())
											 .map(adapterTypeUIMapper::toVo);
	}

	public AdapterTypeUI selectDetail(String adptrCd) {
		AdapterTypeUI adapterTypeUI = adapterTypeUIMapper.toVo(adapterTypeService.getById(adptrCd));
		Iterable<AdapterTypeProperty> adapterTypeProperty = adapterTypePropertyService.findByAdptrCd(adptrCd);
		adapterTypeUI.setGridData(adapterTypePropertyUIMapper.toVoList(adapterTypeProperty));
		return adapterTypeUI;
	}

	public void update(AdapterTypeUI adapterTypeUI) {
		AdapterType adapterType = adapterTypeService.getById(adapterTypeUI.getAdptrCd());
		adapterTypeUIMapper.updateToEntity(adapterTypeUI, adapterType);
		adapterTypeService.save(adapterType);

		List<AdapterTypeProperty> adapterTypeProperties =  getAdapterTypeProperties(adapterTypeUI, adapterType);
		adapterTypePropertyService.deleteOrSave(adapterType.getAdptrcd(), adapterTypeProperties);
	}

	private List<AdapterTypeProperty> getAdapterTypeProperties(AdapterTypeUI adapterTypeUI, AdapterType adapterType) {
		return adapterTypeUI.getGridData().stream()
				.map(propertyUI -> {
						AdapterTypeProperty property = adapterTypePropertyUIMapper.toEntity(propertyUI);
						property.getId().setAdptrCd(adapterType.getAdptrcd());
						return property;
			}).collect(Collectors.toList());
	}

	public void insert(AdapterTypeUI adapterTypeUI) {
		AdapterType adapterType = adapterTypeUIMapper.toEntity(adapterTypeUI);
		adapterTypeService.save(adapterType);
		
		List<AdapterTypeProperty> adapterTypeProperties =  getAdapterTypeProperties(adapterTypeUI, adapterType);
		adapterTypePropertyService.saveAll(adapterTypeProperties);
	}

	public void delete(String adptrCd) {
		adapterTypeService.deleteById(adptrCd);
		adapterTypePropertyService.deleteByIdAdptrCd(adptrCd);
	}

	@SuppressWarnings("unchecked")
	public Map<String, Object>[] selectListToExcel() {

		return adapterTypeService.findAll(Sort.by("adptrcd")).stream().map(e -> {
			Map<String, Object> map = new HashMap<>();
			map.put("ADPTRCD", e.getAdptrcd());
			map.put("ADPTRNAME", e.getAdptrname());
			return map;
		}).toArray(HashMap[]::new);
	}

}
