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

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 com.eactive.eai.rms.data.entity.onl.apim.portaluserterms.PortalUserTermsSearch;
import com.eactive.eai.rms.onl.common.exception.BizException;
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.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 java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Controller
public class PortalUserTermsManController extends BaseAnnotationController {
	private final PortalUserTermsManService portalUserTermsManService;

	@Autowired
	public PortalUserTermsManController(PortalUserTermsManService portalUserTermsManService){
		this.portalUserTermsManService = portalUserTermsManService;
	}

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


	@PostMapping(value = "/onl/apim/portaluserterms/portalUserTermsMan.json", params = "cmd=LIST")
	public ResponseEntity<GridResponse<PortalUserTermsUI>> selectList(@SortDefault(sort = "agreementDate", direction = Sort.Direction.DESC) Pageable pageable,
																	  PortalUserTermsSearch portalUserTermsSearch) {
		Page<PortalUserTermsUI> page = portalUserTermsManService.selectList(pageable, portalUserTermsSearch);
		return ResponseEntity.ok(new GridResponse<>(page));
	}

	@PostMapping(value = "/onl/apim/portaluserterms/portalUserTermsMan.json", params = "cmd=UNMASK")
	public ResponseEntity<GridResponse<PortalUserTermsUI>> selectListUnmask(@SortDefault(sort = "agreementDate", direction = Sort.Direction.DESC) Pageable pageable,
																			PortalUserTermsSearch portalUserTermsSearch,
																			String reason) {
		if (StringUtils.isBlank(reason)) {
			throw new BizException("마스킹 해제 사유를 입력해 주세요.");
		}
		Page<PortalUserTermsUI> page = portalUserTermsManService.selectListUnmask(pageable, portalUserTermsSearch);
		return ResponseEntity.ok(new GridResponse<>(page));
	}

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

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

}
