package com.nokia.sms.controller;

import com.nokia.common.http.R;
import com.nokia.sms.service.QueryService;
import com.nokia.sms.service.SocketClientService;
import com.nokia.sms.vo.DelBlkResp;
import com.nokia.sms.vo.QueryResp;
import com.nokia.sms.vo.RequestParams;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 黑名单查询及解除接口类
 */
@RestController
@RequestMapping("/sms/blacklist/api/")
public class BlkController {
    private final SocketClientService socketClientService;
    private final QueryService queryService;

    public BlkController(SocketClientService socketClientService, QueryService queryService) {
        this.socketClientService = socketClientService;
        this.queryService = queryService;
    }

    /**
     * 黑名单解除接口
     */
    @PostMapping("remove")
    public R remove(@RequestBody RequestParams params) {
        DelBlkResp delBlkResp = new DelBlkResp();
        // 查询黑名单
        Map<String, Object> current = queryService.getBlackListInfo(params.getPhone());
        // 不可以解黑
        if (!CollectionUtils.isEmpty(current) && !current.get("suggestion").equals(1)) {
            delBlkResp.setDelBlkSuccess(false);
            delBlkResp.setDelBlkMessage("解除黑名单失败");
            return R.ok().data(delBlkResp);
        }
        // 尝试解除黑名单
        // TODO 此处测试环境和正式环境不同，待测试环境下的server部署后，可以修改测试环境的配置文件，即可考虑测试环境和正式环境部署相同的jar包
        if (socketClientService.delBlk(params.getPhone())) {
            // if (params.getPhone().equals("13231899751")) {
            delBlkResp.setDelBlkSuccess(true);
            delBlkResp.setDelBlkMessage("成功解除黑名单");
        } else {
            delBlkResp.setDelBlkSuccess(false);
            delBlkResp.setDelBlkMessage("解除黑名单失败");
        }
        return R.ok().data(delBlkResp);
    }

    /**
     * 黑名单查询接口
     */
    @PostMapping("query")
    public R query(@RequestBody RequestParams params) {
        QueryResp resp = queryService.query(params);
        return R.ok().data(resp);
    }
}
