package com.sonic.pigeon.lib.client;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.sonic.common.AppRuntime;
import com.sonic.common.rpc.Result;
import com.sonic.common.rpc.RpcClient;
import com.sonic.pigeon.lib.input.SendMessageInput;
import org.springframework.stereotype.Service;

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

/**
 * @Author zzhan
 * @Date 2020/10/12 16:24
 * @Version 1.0
 */
@Service
public class MessageClient {

    private static final String SEND_MESSAGE = "/api/message/send-message";
    private static final String BATCH_SEND_MESSAGE = "/api/message/batch-send-message";
    private static final String SEND_CHATROOM_INVITE = "/api/message/send-chatroom-invite";

    private RpcClient rpcClient;
    private String host;

    public MessageClient(RpcClient rpcClient, AppRuntime appRuntime) {
        this.rpcClient = rpcClient;
        switch (appRuntime.getEnv()) {
            case dev:
                this.host = "http://localhost:8080";
                break;
            case local:
                this.host = "http://localhost:8080";
                break;
            case test:
                this.host = "http://test-pigeon-svc:8080";
                break;
            case product:
            default:
                this.host = "http://prod-pigeon-svc:8080";
        }
    }

    /**
     * 发送系统内的通知消息
     * @param req
     */
    public void sendMessage(SendMessageInput req) {
        try {
            rpcClient.postBodySign(host + SEND_MESSAGE, req, new TypeReference<Result<Void>>(){});
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 批量发送系统内的通知消息
     */
    public void batchSendMessage(List<SendMessageInput> req) {
        try {
            rpcClient.postBodySign(host + BATCH_SEND_MESSAGE, req, new TypeReference<Result<Void>>() {
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
