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

import com.eactive.apim.portal.portalorg.entity.PortalOrg;
import com.eactive.eai.rms.data.entity.onl.apim.portalorg.PortalOrgService;
import com.eactive.eai.rms.onl.apim.portalorg.PortalOrgUI;
import com.eactive.eai.rms.onl.apim.portalorg.PortalOrgUIMapper;
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.data.entity.onl.authserver.ClientEntity;
import com.eactive.eai.rms.common.base.OnlBaseService;
import com.eactive.eai.rms.data.entity.onl.authserver.ClientEntityService;
import com.eactive.eai.rms.data.entity.onl.authserver.ClientSearch;

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

@Service
@Transactional
public class ClientManService extends OnlBaseService {

	private ClientEntityService clientEntityService;
	private ClientUIMapper clientUIMapper;
	private PortalOrgService portalOrgService;
	private PortalOrgUIMapper portalOrgUIMapper;

	@Autowired
	public ClientManService(ClientEntityService clientEntityService,
							ClientUIMapper clientUIMapper,
							PortalOrgService portalOrgService,
							PortalOrgUIMapper portalOrgUIMapper) {
		this.clientEntityService = clientEntityService;
		this.clientUIMapper = clientUIMapper;
		this.portalOrgService = portalOrgService;
		this.portalOrgUIMapper = portalOrgUIMapper;
	}


	public Page<ClientUI> selectList(Pageable pageable, ClientSearch clientSearch) {
		Page<ClientEntity> page = clientEntityService.findAll(pageable, clientSearch);
		return page.map(clientUIMapper::toVo);
	}

	public HashMap selectDetailCombo() throws Exception {
		List<PortalOrg> portalOrgList = portalOrgService.findByOrgStatusAndApprovalStatus("ACTIVE", "COMPLETED");
		List<PortalOrgUI> portalOrgUIList = portalOrgList.stream()
				.map(portalOrgUIMapper::toVo)
				.collect(Collectors.toList());
		HashMap map = new HashMap();
		map.put("portalOrgList", portalOrgUIList);
		return map;
	}

	public ClientUI selectDetail(String clientId) {
		ClientEntity clientEntity = clientEntityService.getById(clientId);
		return clientUIMapper.toVo(clientEntity);
	}

	public void insert(ClientUI clientUI) {
		ClientEntity clientEntity = clientUIMapper.toEntity(clientUI);
		clientEntity.setAppstatus("1"); // Set default status to normal
		clientEntityService.save(clientEntity);
	}

	public void update(ClientUI clientUI) {
		ClientEntity clientEntity = clientEntityService.getById(clientUI.getClientId());
		clientUIMapper.updateToEntity(clientUI, clientEntity);
		clientEntityService.save(clientEntity);
	}

	public void delete(String clientId){
		clientEntityService.deleteById(clientId);
	}


	public List<String> findAll() {
		List<ClientEntity> clientList = clientEntityService.findAll();
		return clientList.stream()
				.map(ClientEntity::getClientid)
				.collect(Collectors.toList());
	}
}
