package com.eactive.eai.rms.onl.manage.authserver.scope;

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.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.eactive.eai.agent.authserver.ReloadApiScopeCommand;
import com.eactive.eai.agent.command.CommonCommand;
import com.eactive.eai.rms.common.base.OnlBaseService;
import com.eactive.eai.rms.common.spring.LocaleMessage;
import com.eactive.eai.rms.data.entity.onl.authserver.ScopeEntityService;
import com.eactive.eai.rms.data.entity.onl.authserver.ScopeInfoService;
import com.eactive.eai.rms.data.entity.onl.eaimsg.ApiScopeDto;
import com.eactive.eai.rms.data.entity.onl.eaimsg.ApiScopeSearch;
import com.eactive.eai.rms.data.entity.onl.eaimsg.EAIMessageService;
import com.eactive.eai.rms.data.entity.onl.stdmessage.StandardMessageInfoService;
import com.eactive.eai.rms.onl.common.exception.BizException;

@Service
@Transactional
public class ApiScopeManService extends OnlBaseService {

	private ScopeInfoService scopeInfoService;

	private ScopeEntityService scopeEntityService;

	private EAIMessageService eaiMessageService;

	private StandardMessageInfoService standardMessageInfoService;

	private ApiScopeUIMapper apiScopeUIMapper;
	
	private LocaleMessage localeMessage;

	@Autowired
	public ApiScopeManService(ScopeInfoService scopeInfoService, ScopeEntityService scopeEntityService, EAIMessageService eaiMessageService, 
			StandardMessageInfoService standardMessageInfoService, ApiScopeUIMapper apiScopeUIMapper, LocaleMessage localeMessage) {

		this.scopeInfoService = scopeInfoService;
		this.scopeEntityService = scopeEntityService;
		this.eaiMessageService = eaiMessageService;
		this.standardMessageInfoService = standardMessageInfoService;
		this.apiScopeUIMapper = apiScopeUIMapper;
		this.localeMessage = localeMessage;
	}

	public Page<ApiScopeUI> selectApiScopeRelationsList(Pageable pageable, ApiScopeSearch apiScopeSearch) {
		Page<ApiScopeDto> page = eaiMessageService.findAll(pageable, apiScopeSearch);
		return page.map(this::convertToApiScopeUI);
	}
	
	private ApiScopeUI convertToApiScopeUI(ApiScopeDto dto) {
		ApiScopeUI apiScopeUI = new ApiScopeUI();
		apiScopeUI.setApiDesc(dto.getEaiSvcDesc());
		apiScopeUI.setScopeId(dto.getScopeId());
		apiScopeUI.setBzwkSvcKeyName(dto.getBzwkSvcKeyName());
		apiScopeUI.setScopeName(dto.getScopeName());
		apiScopeUI.setApiFullPath(dto.getApiFullPath());
		apiScopeUI.setApiId(dto.getEaiSvcName());
		return apiScopeUI;
	}
	
	public void insertApiScopeRelation(ApiScopeUI apiScopeUI) {
		// 중복체크
		if(scopeEntityService.existsById(apiScopeUIMapper.toId(apiScopeUI))) {
			throw new BizException(localeMessage.getString("apiScpMan.apiScpIdExists"));
		}
		
		scopeEntityService.save(apiScopeUIMapper.toEntity(apiScopeUI));
	}

	public void insertApiScopeRelationList(List<ApiScopeUI> apiScopeList, String scopeId) {
		// ScopeId 기준 전체 삭제 후 LIST 기준으로 저장.
		if(scopeId == null || scopeId.isEmpty()) {
			throw new BizException("ScopeId 가 존재하지 않습니다.");
		}
		
		scopeEntityService.deleteByIdScopeId(scopeId);
		
		if(apiScopeList == null || apiScopeList.isEmpty()) {
			return;
		}
		
		for (ApiScopeUI ui : apiScopeList) {
			ui.setScopeId(scopeId);
			
			scopeEntityService.save(apiScopeUIMapper.toEntity(ui));
			
			CommonCommand.builder()
			.name(CommonCommand.RELOAD_API_SCOPE_COMMAND)
			.args(new String[] {ReloadApiScopeCommand.COMMAND_TYPE_API_SCOPE, 
					ui.getBzwkSvcKeyName(), ui.getScopeId()})
			.build()
			.broadcast(agentUtilService);
		}
	}

	public void deleteApiScopeRelation(ApiScopeUI apiScopeUI) {
		scopeEntityService.deleteById(apiScopeUIMapper.toId(apiScopeUI));
	}

	public List<Map<String, Object>> selectApiListCombo() {
		return standardMessageInfoService.selectApiListCombo();
	}
	
	public List<Map<String, Object>> selectScopeListCombo() {
		
		return scopeInfoService.findAll().stream().map(e -> {
			Map<String, Object> comboMap = new HashMap<>();
			comboMap.put("SCOPEID", e.getScopeId());
			comboMap.put("SCOPENAME", e.getScopeName());
			return comboMap;
		}).collect(Collectors.toList());
	}
	
}
