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

import com.eactive.eai.rms.common.base.BaseAnnotationController;
import com.eactive.eai.rms.common.login.SessionManager;
import com.eactive.eai.rms.common.vo.GridResponse;
import com.eactive.eai.rms.data.entity.onl.apim.approval.PortalApprovalUISearch;
import com.eactive.eai.rms.onl.common.exception.BizException;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
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;

@Controller
@RequiredArgsConstructor
public class PortalApprovalManController extends BaseAnnotationController {

    private final PortalApprovalManService portalApprovalManService;

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

    @GetMapping(value = "/onl/apim/approval/portalApprovalMan.view", params = "cmd=DETAIL")
    public String detailView(@RequestParam(name = "type", defaultValue = "app") String type) {
        if (type.equalsIgnoreCase("app")) {
            return "/onl/apim/approval/portalAppApprovalManPopup";
        } else {
            return "/onl/apim/approval/portalUserApprovalManPopup";
        }
    }

    @PostMapping(value = "/onl/apim/approval/portalApprovalMan.json", params = "cmd=LIST")
    public ResponseEntity<GridResponse<PortalApprovalUI>> selectList(@SortDefault(value = "createdDate", direction = Sort.Direction.DESC) Pageable pageable,
        PortalApprovalUISearch portalApprovalUISearch) {
        Page<PortalApprovalUI> page = portalApprovalManService.selectList(pageable, portalApprovalUISearch);
        return ResponseEntity.ok(new GridResponse<>(page));
    }

    @PostMapping(value = "/onl/apim/approval/portalApprovalMan.json", params = "cmd=DETAIL")
    public ResponseEntity<PortalApprovalUI> selectDetail(String id) {
        PortalApprovalUI portalApprovalUI = portalApprovalManService.selectDetail(id);
        return ResponseEntity.ok(portalApprovalUI);
    }

    @PostMapping(value = "/onl/apim/approval/portalApprovalMan.json", params = "cmd=UNMASK")
    public ResponseEntity<PortalApprovalUI> selectDetail(String id, String reason) {
        if (StringUtils.isBlank(reason)) {
            throw new BizException("마스킹 해제 사유를 입력해 주세요.");
        }
        PortalApprovalUI portalApprovalUI = portalApprovalManService.selectDetailUnmask(id);
        return ResponseEntity.ok(portalApprovalUI);
    }

    @PostMapping(value = "/onl/apim/approval/portalApprovalMan.json", params = "cmd=APPROVE")
    public ResponseEntity<Void> approve(String id) {
        portalApprovalManService.approve(id, SessionManager.getLoginVo().getUserId());
        return ResponseEntity.ok().build();
    }

    @PostMapping(value = "/onl/apim/approval/portalApprovalMan.json", params = "cmd=REJECT")
    public ResponseEntity<Void> reject(String id, String rejectReason) {
        portalApprovalManService.reject(id, SessionManager.getLoginVo().getUserId(), rejectReason);
        return ResponseEntity.ok().build();
    }

    @PostMapping(value = "/onl/apim/approval/portalApprovalMan.json", params = "cmd=REDEPLOY")
    public ResponseEntity<Void> redeploy(String id) {
        portalApprovalManService.redeploy(id);
        return ResponseEntity.ok().build();
    }
}
