package com.eactive.eai.rms.onl.manage.comm.property;

import java.util.HashMap;
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.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.eactive.eai.data.entity.onl.property.Prop;
import com.eactive.eai.data.entity.onl.property.PropGroup;
import com.eactive.eai.rms.common.base.BaseService;
import com.eactive.eai.rms.common.spring.LocaleMessage;
import com.eactive.eai.rms.data.entity.onl.property.PropertyGroupService;
import com.eactive.eai.rms.onl.common.exception.BizException;

@Service
@Transactional
public class PropertyManService extends BaseService {

	private PropertyGroupService propertyGroupService;

	private PropertyGroupMapper propertyGroupMapper;
	
	private LocaleMessage localeMessage;

	@Autowired
	public PropertyManService( PropertyGroupService propertyGroupService, 
										  PropertyGroupMapper propertyGroupMapper, 
										  LocaleMessage localeMessage) {
		this.propertyGroupService = propertyGroupService;
		this.propertyGroupMapper = propertyGroupMapper;
		this.localeMessage = localeMessage;
	}
	
	public Page<PropertyGroupUI> selectList(Pageable pageable, PropertyGroupUI propertyGroupUI) {
		Page<PropGroup> page = propertyGroupService.findAll(pageable, propertyGroupUI);
		return page.map(propertyGroupMapper::toVo);
	}

	public PropertyGroupUI selectDetail(String prptyGroupName) {
		PropGroup propGroup = propertyGroupService.getByIdWithSortedProps(prptyGroupName);
		return propGroup == null ? null : propertyGroupMapper.toVo(propGroup);
	}

	public void insert(PropertyGroupUI propertyGroupUI) {
		if (propertyGroupService.existsById(propertyGroupUI.getPrptyGroupName())) {
			throw new BizException(localeMessage.getString("propertyDetail.propertyGroupExists"));
		}
		
		PropGroup propGroup = propertyGroupMapper.toEntity(propertyGroupUI);
		if (propGroup.getProps() != null) {
			for (Prop prop : propGroup.getProps()) {
				prop.setPropGroup(propGroup);
			}
		}
		propertyGroupService.save(propGroup);
	}

	public void update(PropertyGroupUI propertyGroupUI) {
		PropGroup propGroup = propertyGroupService.getById(propertyGroupUI.getPrptyGroupName());
		if (propGroup.getProps() != null) {
			for (Prop prop : propGroup.getProps()) {
				prop.setPropGroup(propGroup);
			}
		}
		propertyGroupMapper.updateToEntity(propertyGroupUI, propGroup);
		propertyGroupService.save(propGroup);
	}

	public void delete(String prptyGroupName) {
		PropGroup propGroup = propertyGroupService.getById(prptyGroupName);
		if (propGroup != null) {
			propertyGroupService.deleteById(prptyGroupName);
		}
	}

	public Map<String, Object>[] selectListToExcel() {
		@SuppressWarnings("unchecked")
		Map<String, Object>[] mapArray = propertyGroupService.findAll(Sort.by("prptygroupname")).stream().map(e -> {
			Map<String, Object> map = new HashMap<>();
			map.put("PRPTYGROUPNAME", e.getPrptygroupname());
			map.put("PRPTYGROUPDESC", e.getPrptygroupdesc());
			return map;
		}).toArray(HashMap[]::new);
		return mapArray;
	}

}
