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

import com.eactive.apim.portal.agreements.entity.AgreementType;
import com.eactive.eai.rms.common.base.BaseAnnotationController;
import com.eactive.eai.rms.common.combo.ComboVo;
import com.eactive.eai.rms.common.vo.GridResponse;
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.data.web.SortDefault;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Controller
public class PortalTermsManController extends BaseAnnotationController {
	private final PortalTermsManService portalTermsManService;

	@Autowired
	public PortalTermsManController(PortalTermsManService portalTermsManService){
		this.portalTermsManService = portalTermsManService;
	}

	@GetMapping(value = "/onl/apim/portalterms/portalTermsMan.view")
	public void view() {
		// view
	}

	@GetMapping(value = "/onl/apim/portalterms/portalTermsMan.view", params = "cmd=DETAIL")
	public String detailView() {
		return "/onl/apim/portalterms/portalTermsManDetail";
	}


	@PostMapping(value = "/onl/apim/portalterms/portalTermsMan.json", params = "cmd=LIST")
	public ResponseEntity<GridResponse<PortalTermsUI>> selectList(@SortDefault.SortDefaults({
																  @SortDefault(sort = "revision", direction = Sort.Direction.DESC),
			  													  @SortDefault(sort = "agreementsType", direction = Sort.Direction.ASC)}) Pageable pageable,
																  String searchAgreementsType) {
		Page<PortalTermsUI> page = portalTermsManService.selectList(pageable, searchAgreementsType);
		return ResponseEntity.ok(new GridResponse<>(page));
	}

	@PostMapping(value = "/onl/apim/portalterms/portalTermsMan.json", params = "cmd=LIST_INIT_COMBO")
	public ResponseEntity<Map<String, Object>> initCombo() {
		List<ComboVo> termsTypeList = Arrays.stream(AgreementType.values())
				.map(type -> {
					ComboVo vo = new ComboVo();
					vo.setCode(type.name());
					vo.setCodename(PortalTermsManService.displayLabel(type));
					return vo;
				}).collect(Collectors.toList());

		Map<String, Object> resultMap = new HashMap<>();
		resultMap.put("termsTypeRows", termsTypeList);
		return ResponseEntity.ok(resultMap);
	}

	@PostMapping(value = "/onl/apim/portalterms/portalTermsMan.json", params = "cmd=DETAIL")
	public ResponseEntity<PortalTermsUI> selectDetail(String agreementsType, int revision) {
		PortalTermsUI portalTermsUI = portalTermsManService.selectDetail(agreementsType, revision);
		return ResponseEntity.ok(portalTermsUI);
	}

	@PostMapping(value = "/onl/apim/portalterms/portalTermsMan.json", params = "cmd=INSERT")
	public ResponseEntity<String> insert(PortalTermsUI portalTermsUI,
										 @RequestParam(value = "attachedFile", required = false) MultipartFile attachedFile) throws IOException {
		portalTermsManService.insert(portalTermsUI, attachedFile);
		return ResponseEntity.ok().build();
	}

	@PostMapping(value = "/onl/apim/portalterms/portalTermsMan.json", params = "cmd=UPDATE")
	public ResponseEntity<String> update(PortalTermsUI portalTermsUI, String usedFileIds,
										 @RequestParam(value = "attachedFile", required = false) MultipartFile attachedFile,
										 @RequestParam(value = "deleteAttachment", defaultValue = "false") boolean deleteAttachment) throws IOException {
		// 사용 중인 파일 ID 목록 파싱
		Set<String> usedFileIdSet = new HashSet<>();
		if (usedFileIds != null && !usedFileIds.isEmpty()) {
			usedFileIdSet.addAll(Arrays.asList(usedFileIds.split(",")));
		}
		portalTermsManService.update(portalTermsUI, usedFileIdSet, attachedFile, deleteAttachment);
		return ResponseEntity.ok().build();
	}

	@PostMapping(value = "/onl/apim/portalterms/portalTermsMan.json", params = "cmd=DELETE")
	public ResponseEntity<Void> delete(String agreementsType, Integer revision) {
		portalTermsManService.delete(agreementsType, revision);
		return ResponseEntity.ok().build();
	}

}
