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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
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 com.eactive.eai.rms.common.base.OnlBaseAnnotationController;
import com.eactive.eai.rms.common.vo.GridResponse;
import com.eactive.eai.rms.data.entity.onl.authserver.ClientSearch;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class TokenIssuanceStatusController extends OnlBaseAnnotationController {

    private TokenIssuanceStatusService tokenIssuanceStatusService;

    @Autowired
    public TokenIssuanceStatusController(TokenIssuanceStatusService tokenIssuanceStatusService) {
        this.tokenIssuanceStatusService = tokenIssuanceStatusService;
    }

    @GetMapping(value = "/onl/admin/tokenissuance/tokenIssuanceStatusMan.view")
    public void view() {
        // view
    }

    @GetMapping(value = "/onl/admin/tokenissuance/tokenIssuanceStatusMan.view", params = "cmd=DETAIL")
    public String viewList() {
        return "/onl/admin/tokenissuance/tokenIssuanceStatusManDetail";
    }

    @PostMapping(value = "/onl/admin/tokenissuance/tokenIssuanceStatusMan.json", params = "cmd=LIST")
    public ResponseEntity<GridResponse<TokenIssuanceStatusUI>> selectList(@SortDefault("clientname") Pageable pageable,
                                                                          ClientSearch clientSearch) {
        Page<TokenIssuanceStatusUI> page = tokenIssuanceStatusService.selectList(pageable, clientSearch);
        return ResponseEntity.ok(new GridResponse<>(page));
    }

    @PostMapping(value = "/onl/admin/tokenissuance/tokenIssuanceStatusMan.json", params = "cmd=DETAIL")
    public ResponseEntity<Map<String, Object>> selectDetail(@RequestParam("clientId") String clientId) {
        TokenIssuanceStatusDetailUI detailUI = tokenIssuanceStatusService.selectDetail(clientId);
        Map<String, Object> response = new HashMap<>();
        response.put("detail", detailUI);
        return ResponseEntity.ok(response);
    }

    @PostMapping(value = "/onl/admin/tokenissuance/tokenIssuanceStatusMan.json", params = "cmd=EXEMPT_RESTRICTION")
    public ResponseEntity<Map<String, Object>> exemptRestriction(@RequestParam("clientId") String clientId) {
        tokenIssuanceStatusService.exemptRestrictionForClient(clientId);
        Map<String, Object> response = new HashMap<>();
        response.put("success", true);
        response.put("message", "제한이 해제되었습니다.");
        return ResponseEntity.ok(response);
    }
}