package thyyxxk.webserver.http.websocket;

import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import thyyxxk.webserver.config.envionment.ApiUrl;
import thyyxxk.webserver.constants.Message;
import thyyxxk.webserver.entity.outpatient.triage.MessageForPush;
import thyyxxk.webserver.http.websocket.dto.*;
import thyyxxk.webserver.service.externalhttp.ThirdpartApiService;
import thyyxxk.webserver.utils.SocketMsg;
import thyyxxk.webserver.utils.StringUtil;
import thyyxxk.webserver.utils.ValidatorUtils;

import java.util.Objects;

@Slf4j
@Component
@RequiredArgsConstructor
public class SocketV2 {
    private final ApiUrl apiUrl;
    private final ThirdpartApiService thirdpartApi;

    public HttpRequest getPostApi(String business) {
        return HttpRequest.post(apiUrl.getSocketApi() + "/send" + business)
                .header(Header.CONTENT_TYPE, "application/json;charset=utf-8")
                .header(Header.ACCEPT, "application/json");
    }

    public void sendMessageByUserCode(WebSocketByUserCode json) {
        json.setMode("SINGLE");
        getPostApi("/intergrationPlatform")
                .body(JSONUtil.toJsonStr(json))
                .executeAsync().close();
    }

    public void sendUserCodeListMessage(WebSocketByListUserCode json) {
        json.setMode("MULTI");
        getPostApi("/intergrationPlatform")
                .body(JSONUtil.toJsonStr(json))
                .executeAsync().close();
    }

    public void sendMessageToAll(WebSocketToAll json) {
        json.setMode("ALL");
        getPostApi("/intergrationPlatform")
                .body(JSONUtil.toJsonStr(json))
                .executeAsync().close();
    }

    public void sendArchiveMessage(ArchiveSocketParam json) {
        getPostApi("/archive")
                .body(JSONUtil.toJsonStr(json))
                .executeAsync().close();
    }

    public void sendTask(SocketTask task) {
        ValidatorUtils.validate(task);

        JSONObject js = new JSONObject();
        js.set("mode", "SINGLE");
        js.set("sid", task.getUserCode());
        js.set("msg", SocketMsg.socketVo(Message.backgroundTask, JSONUtil.toJsonStr(task)));

        getPostApi("/intergrationPlatform")
                .body(js.toJSONString(0))
                .executeAsync().close();
    }

    /**
     * 向导诊台候诊大屏发送消息
     */
    public void sendMsgToHelpDesk(MessageForPush body) {
        if (Objects.equals(body.getAction(), 1)) {
            setSpeechUrl(body);
        }
        JSONObject json = new JSONObject();
        json.set("sid", body.getSid());
        JSONObject msg = JSONUtil.parseObj(body);
        json.set("msg", msg);
        getPostApi("/helpDeskScreen")
                .body(json.toJSONString(0))
                .execute().close();
        log.info("向导诊台候诊大屏发送消息: {}", json);
    }

    /**
     * 向诊室候诊小屏发送消息
     */
    public void sendMsgToRoomScreen(MessageForPush body) {
        if (Objects.equals(body.getAction(), 1) &&
            StringUtil.isBlank(body.getSpeechUrl())) {
            setSpeechUrl(body);
        }
        JSONObject json = new JSONObject();
        json.set("sid", body.getSid());
        JSONObject msg = JSONUtil.parseObj(body);
        json.set("msg", msg);
        getPostApi("/roomScreen")
                .body(json.toJSONString(0))
                .execute().close();
        log.info("向诊室候诊小屏发送消息: {}", json);
    }

    private void setSpeechUrl(MessageForPush body) {
        String text = String.format("请%s号%s，到%s%s号诊室就诊。",
                body.getFzNo(), body.getName(),
                body.getDeptName(), body.getRoomNo());
        String id = String.valueOf(body.getSerialNo());
        body.setSpeechUrl(thirdpartApi.getSpeechUrl(text, id));
    }

}
