package com.eactive.eai.rms.onl.manage.authoutbound;

import com.eactive.eai.agent.command.CommonCommand;
import com.eactive.eai.rms.common.base.OnlBaseAnnotationController;
import com.eactive.eai.rms.common.login.SessionManager;
import com.eactive.eai.rms.common.vo.GridResponse;
import com.eactive.eai.rms.onl.manage.comm.property.PropertyGroupUI;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
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 javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Controller
public class OutboundOAuthCredentialController extends OnlBaseAnnotationController {
    enum CommandType { RELOAD, DELETE };

    public static final Log logger = LogFactory.getLog(OutboundOAuthCredentialController.class);

    @Autowired
    OutboundOAuthCredentialManService service ;

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

    @GetMapping(value = "/onl/admin/authoutbound/outboundOauthCredentialMan.view", params = "cmd=DETAIL")
    public String viewDetail() {
        return "/onl/admin/authoutbound/outboundOauthCredentialManDetail";
    }

    @PostMapping(value= "/onl/admin/authoutbound/outboundOauthCredentialMan.json", params = "cmd=LIST")
    public ResponseEntity<GridResponse<OutboundOAuthCredentialUI>>selectList(String searchAdapterGroupName, Pageable pageable) {
        Page<OutboundOAuthCredentialUI> page = service.selectList(pageable, searchAdapterGroupName);
        return ResponseEntity.ok(new GridResponse<>(page));
    }

    @PostMapping(value = "/onl/admin/authoutbound/outboundOauthCredentialMan.json", params = "cmd=DETAIL")
    public ResponseEntity<OutboundOAuthCredentialUI> selectDetail(String adapterGroupName) throws Exception {
        OutboundOAuthCredentialUI outboundOAuthCredentialUI = service.selectDetail(adapterGroupName);
        return ResponseEntity.ok(outboundOAuthCredentialUI);
    }

    @PostMapping(value= "/onl/admin/authoutbound/outboundOauthCredentialMan.json",params = "cmd=LIST_DETAIL_COMBO")
    public ResponseEntity<Map<String, Object>> selectDetailCombo() throws Exception {
        Map<String, Object> map = service.selectDetailCombo();
        return ResponseEntity.ok(map);
    }

    @PostMapping(value = "/onl/admin/authoutbound/outboundOauthCredentialMan.json", params = "cmd=INSERT")
    public ResponseEntity<Void> insert(OutboundOAuthCredentialUI outboundOAuthCredentialUI) throws Exception {
        service.insert(outboundOAuthCredentialUI);
        commandOAuthInfo(CommandType.RELOAD, outboundOAuthCredentialUI.getAdapterGroupName());
        return ResponseEntity.ok().build();
    }

    @PostMapping(value = "/onl/admin/authoutbound/outboundOauthCredentialMan.json", params = "cmd=UPDATE")
    public ResponseEntity<Void> update(OutboundOAuthCredentialUI outboundOAuthCredentialUI) throws Exception {
        service.update(outboundOAuthCredentialUI);
        commandOAuthInfo(CommandType.RELOAD, outboundOAuthCredentialUI.getAdapterGroupName());
        return ResponseEntity.ok().build();
    }

    @PostMapping(value = "/onl/admin/authoutbound/outboundOauthCredentialMan.json", params = "cmd=DELETE")
    public ResponseEntity<Void> delete(String adapterGroupName) throws Exception {
        service.delete(adapterGroupName);
        commandOAuthInfo(CommandType.DELETE, adapterGroupName);
        return ResponseEntity.ok().build();
    }

    private boolean commandOAuthInfo(CommandType commandType, String adapterGroupName) {
        String commandClass ;

        if(CommandType.DELETE == commandType){
            commandClass = "com.eactive.eai.agent.authoutbound.RemoveOutboundOAuthCredentialCommand";
        }else{
            commandClass = "com.eactive.eai.agent.authoutbound.ReloadOutboundOAuthCredentialCommand";
        }
        logger.warn("Command:" + commandType + ", Target: " + adapterGroupName);
        CommonCommand command = new CommonCommand(commandClass, adapterGroupName);
        try {
            agentUtilService.broadcast(command);
        } catch (Exception e) {
            logger.warn("Command error", e);
            return false;
        }
        return true;
    }
}
