package com.eactive.eai.rms.onl.apim.apigroup;

import com.eactive.eai.data.entity.onl.apim.apigroup.ApiGroup;
import com.eactive.eai.data.entity.onl.apim.apigroup.ApiGroupApi;
import com.eactive.eai.data.entity.onl.message.EAIMessageEntity;
import com.eactive.eai.rms.common.base.OnlBaseService;
import com.eactive.eai.rms.data.entity.onl.apim.apigroup.ApiGroupService;
import com.eactive.eai.rms.data.entity.onl.eaimsg.EAIMessageService;
import com.eactive.eai.rms.onl.apim.apigroup.mapping.ApiGroupUIMapper;
import com.eactive.eai.rms.onl.apim.apigroup.ui.ApiGroupUI;
import com.eactive.eai.rms.onl.apim.apigroup.ui.ApiGroupUISearch;
import com.eactive.eai.transformer.util.Logger;
import org.apache.commons.lang3.StringUtils;
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 java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Transactional
public class ApiGroupManService extends OnlBaseService {

	@Autowired
	private ApiGroupService apiGroupService;
	@Autowired
	private EAIMessageService eaiMessageService;
	@Autowired
	private ApiGroupUIMapper apiGroupUIMapper;

	private static final Logger logger = Logger.getLogger(ApiGroupManService.class);

	public Page<ApiGroupUI> selectList(ApiGroupUISearch apiGroupUISearch, Pageable pageable) {
		Page<ApiGroup> tuples = apiGroupService.selectList(apiGroupUISearch, pageable);

		return tuples.map(apiGroup -> {
			ApiGroupUI apiGroupUI = apiGroupUIMapper.toVo(apiGroup);
			apiGroupUI.setMainIcon(null);
			apiGroupUI.setContentIcon(null);
			return apiGroupUI;
		});
	}

	public ApiGroupUI selectDetail(String apiGroupId) throws Exception {
		ApiGroup apiGroup = apiGroupService.getById(apiGroupId);
		ApiGroupUI apiGroupUI;

		if(apiGroup.getApiGroupApiList() != null && apiGroup.getApiGroupApiList().size() > 0) {
			List<String> apiIds = apiGroup.getApiGroupApiList().stream()
					.map(api -> api.getId().getApiId())
					.collect(Collectors.toList());

			List<EAIMessageEntity> eaiMessageEntities = eaiMessageService.findByEaisvcnameList(apiIds);
			apiGroupUI = apiGroupUIMapper.toVo(apiGroup, eaiMessageEntities);

			// displayOrder 정보를 포함하는 ApiGroupApiList 설정
			Map<String, Integer> displayOrders = apiGroup.getApiGroupApiList().stream()
					.collect(Collectors.toMap(
							api -> api.getId().getApiId(),
							ApiGroupApi::getDisplayOrder
					));

			// ApiGroupApiUI 리스트에 displayOrder 설정
			apiGroupUI.getApiGroupApiList().forEach(apiGroupApiUI -> {
				apiGroupApiUI.setDisplayOrder(displayOrders.get(apiGroupApiUI.getApiId()));
			});
		} else {
			apiGroupUI = apiGroupUIMapper.toVo(apiGroup);
		}

		return apiGroupUI;
	}

	public void insert(ApiGroupUI apiGroupUI) {
		applyContentSubjectDefault(apiGroupUI);
		ApiGroup apiGroup = apiGroupUIMapper.toEntity(apiGroupUI);
		for (ApiGroupApi apiGroupApi : apiGroup.getApiGroupApiList()){
			apiGroupApi.setApiGroup(apiGroup);
		}
		apiGroupService.save(apiGroup);
		for (ApiGroupApi apiGroupApi : apiGroup.getApiGroupApiList()){
			apiGroupApi.getId().setApiGroupId(apiGroup.getId());
		}
	}

	public void update(ApiGroupUI apiGroupUI) {
		applyContentSubjectDefault(apiGroupUI);
		ApiGroup apiGroup = apiGroupService.getById(apiGroupUI.getId());
		apiGroupUIMapper.updateToEntity(apiGroupUI, apiGroup);
		apiGroupService.save(apiGroup);
	}

	// 메인 그룹명(contentSubject) 미입력 시 API 그룹 명(groupName)으로 대체
	private void applyContentSubjectDefault(ApiGroupUI apiGroupUI) {
		if (StringUtils.isBlank(apiGroupUI.getContentSubject())) {
			apiGroupUI.setContentSubject(apiGroupUI.getGroupName());
		}
	}

	public void delete(ApiGroupUI apiGroupUI) {
		ApiGroup apiGroup = apiGroupService.getById(apiGroupUI.getId());
		apiGroupService.delete(apiGroup);
	}
}
