package shop.alien.store.controller;

import com.alibaba.fastjson2.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSupport;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import shop.alien.entity.result.R;
import shop.alien.store.config.WebSocketProcess;
import shop.alien.util.common.JwtUtil;

import java.util.Objects;

/**
 * websocket消息推送
 *
 * @author ssk
 * @version 1.0
 * @date 2024/2/29 14:45
 */
@Api(tags = {"WebSocket消息推送"})
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/websocket")
@RequiredArgsConstructor
public class WebSocketController {

    private final WebSocketProcess webSocketProcess;

    @ApiOperationSupport(order = 1)
    @ApiOperation("向指定客户端发消息")
    @GetMapping(value = "/sendMsgToClientByPhoneId")
    public R<Boolean> sendMsgToClientByPhoneId(String messageReceiverId, String webSocketVoStr) {
        try {
            log.info("WebSocketController.sendMsgToClientByPhoneId?messageReceiverId={}, webSocketVoStr={}", messageReceiverId, webSocketVoStr);
            webSocketProcess.sendMessage(messageReceiverId, webSocketVoStr);
            return R.success("发送成功");
        } catch (Exception e) {
            log.error("WebSocketController_sendMsgToClientById Error Mgs={}", e.getMessage());
        }
        return R.fail("发送失败");
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation("socket轮询获取token状态")
    @GetMapping(value = "/getTokenStatus")
    public R<Boolean> getTokenStatus() {
        String phoneId = Objects.requireNonNull(JwtUtil.getCurrentUserInfo()).getString("userType") + "_" + JwtUtil.getCurrentUserInfo().getString("phone");
//        log.info("WebSocketController.getTokenStatus()----------phoneId={}", phoneId);
        return R.data(true);
    }

//    @ApiOperationSupport(order = 1)
//    @ApiOperation("向指定客户端发消息")
//    @GetMapping(value = "/sendMsgToClientByPhoneId")
//    public R<Boolean> sendMsgToClientByPhoneId(String messageReceiverId,
//                                               String senderId,
//                                               String receiverId,
//                                               String category,
//                                               String type,
//                                               String text,
//                                               Integer messageId) {
//        log.info("WebSocketController.sendMsgToClientByPhoneId?messageReceiverId={}, senderId={}, receiverId={}, category={}, type={}, text={}, messageId={}",
//                messageReceiverId, senderId, receiverId, category, type, text, messageId);
//        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("senderId", senderId);
//        jsonObject.put("receiverId", receiverId);
//        jsonObject.put("category", category);
//        jsonObject.put("type", type);
//        jsonObject.put("text", text);
//        jsonObject.put("messageId", messageId);
//        jsonObject.put("isRead", 0);
//        try {
//            webSocketProcess.sendMessage(messageReceiverId, jsonObject.toJSONString());
//            return R.success("发送成功");
//        } catch (Exception e) {
//            log.error("WebSocketController_sendMsgToClientById Error Mgs={}", e.getMessage());
//        }
//        return R.fail("发送失败");
//    }


//    /**
//     * 向指定客户端发消息
//     */
//    @ApiOperationSupport(order = 1)
//    @ApiOperation("向指定客户端发消息")
//    @PostMapping(value = "sendMsgToClientById")
//    public R<Boolean> sendMsgToClientById(@RequestBody WebsocketVo websocketVo) {
//        log.info("WebSocketController.sendMsgToClientById?websocketVo={}", websocketVo.toString());
//        try {
//            JSONObject jsonObject = new JSONObject();
//            jsonObject.put("type", websocketVo.getType());
//            jsonObject.put("text", websocketVo.getText());
//            webSocketProcess.sendMessage(websocketVo.getReceiverId(), jsonObject.toJSONString());
//            LifeMessage lifeMessage = new LifeMessage();
//            lifeMessage.setSenderId(websocketVo.getSenderId());
//            lifeMessage.setReceiverId(websocketVo.getReceiverId());
//            lifeMessage.setContent(websocketVo.getText());
//            lifeMessage.setType(websocketVo.getType());
//            lifeMessageMapper.insert(lifeMessage);
//            return R.success("发送成功");
//        } catch (Exception e) {
//            log.error("WebSocketController_sendMsgToClientById Error Mgs={}", e.getMessage());
//        }
//        return R.fail("发送失败");
//    }
//
//    /**
//     * 发消息到所有客户端
//     *
//     * @param text 消息内容
//     */
//    @ApiOperationSupport(order = 2)
//    @ApiOperation("发消息到所有客户端")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "text", value = "消息内容", dataType = "String", paramType = "query")
//    })
//    @GetMapping(value = "sendMsgToAllClient")
//    public void sendMsgToAllClient(@RequestParam String text) {
//        try {
//            webSocketProcess.sendAllMessage(text);
//        } catch (Exception e) {
//            log.error("WebSocketController_sendMsgToAllClient Error Mgs={}", e.getMessage());
//        }
//    }

}
