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

import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

import com.eactive.eai.rms.common.base.BaseService;
import com.eactive.eai.rms.common.property.UI.MonitoringPropertyGroupUI;
import com.eactive.eai.rms.common.property.mapper.MonitoringPropertyGroupMapper;
import com.eactive.eai.rms.data.entity.man.monitoringProperty.MonitoringProperty;
import com.eactive.eai.rms.data.entity.man.monitoringProperty.MonitoringPropertyGroup;
import com.eactive.eai.rms.data.entity.man.monitoringProperty.service.MonitoringPropertyGroupService;

@Service
@Transactional(transactionManager = "transactionManagerForEMS")
public class MonitoringPropertyManService extends BaseService {

	@Autowired
	private MonitoringPropertyGroupService propertyGroupService;

	@Autowired
	private MonitoringPropertyGroupMapper propertyGroupMapper;

	public Page<MonitoringPropertyGroupUI> selectList(Pageable pageable, MonitoringPropertyGroupUI propertyGroupUI) {
		Page<MonitoringPropertyGroup> page = propertyGroupService.findAll(pageable, propertyGroupUI);
		return page.map(propertyGroupMapper::toVo);
	}

	public MonitoringPropertyGroupUI selectDetail(String prptyGroupName) {
		MonitoringPropertyGroup propertyGroup = propertyGroupService.getById(prptyGroupName);
		propertyGroup.getMonitoringProperties().sort(Comparator.comparing(p -> p.getId().getPrptyName()));
		return propertyGroupMapper.toVo(propertyGroup);
	}

	public void insert(MonitoringPropertyGroupUI propertyGroupUI) {
		MonitoringPropertyGroup propertyGroup = propertyGroupMapper.toEntity(propertyGroupUI);

		if (!propertyGroup.getMonitoringProperties().isEmpty()) {
			for (MonitoringProperty property : propertyGroup.getMonitoringProperties()) {
				property.setMonitoringPropertyGroup(propertyGroup);
			}
		}
		propertyGroupService.save(propertyGroup);
	}

	public void update(MonitoringPropertyGroupUI propertyGroupUI) {
		MonitoringPropertyGroup propertyGroup = propertyGroupService.getById(propertyGroupUI.getPrptyGroupName());

		if (!propertyGroup.getMonitoringProperties().isEmpty()) {
			for (MonitoringProperty property : propertyGroup.getMonitoringProperties()) {
				property.setMonitoringPropertyGroup(propertyGroup);
			}
		}
		propertyGroupMapper.updateToEntity(propertyGroupUI, propertyGroup);
		propertyGroupService.save(propertyGroup);
	}

	public void delete(String prptyGroupName) {
		propertyGroupService.deleteById(prptyGroupName);
	}

	public Map<String, Object>[] selectListToExcel(String prptyGroupName) {
		List<MonitoringProperty> properties = propertyGroupService.getById(prptyGroupName).getMonitoringProperties();
		properties.sort(Comparator.comparing(p -> p.getId().getPrptyName()));

		@SuppressWarnings("unchecked")
		Map<String, Object>[] mapArray = properties.stream().map(e -> {
			Map<String, Object> map = new HashMap<>();
			map.put("PRPTYNAME", e.getId().getPrptyName());
			map.put("PRPTY2VAL", e.getPrpty2Val());
			return map;
		}).toArray(HashMap[]::new);

		return mapArray;
	}

}
