package com.sonic.pigeon.lib.client;

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.HistoryMessageInput;
import com.sonic.pigeon.lib.input.SendAiCustomerMessageInput;
import com.sonic.pigeon.lib.input.SendAiTextMessageInput;
import com.sonic.pigeon.lib.input.UpdateAiSendCustomImageMessageInput;
import com.sonic.pigeon.lib.output.HistoryMessageOutput;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author code
 */
@Slf4j
@Service
public class ImMessageClient {

    private static final String SEND_AI_TO_USER_TEXT_MESSAGE = "/api/im-message/send-ai-to-user-text-message";

    private static final String SEND_USER_TO_AI_TEXT_MESSAGE = "/api/im-message/send-user-to-ai-text-message";

    private static final String SEND_AI_TO_USER_CUSTOMER_MESSAGE = "/api/im-message/send-ai-to-user-customer-message";

    private static final String SEND_USER_TO_AI_CUSTOMER_MESSAGE = "/api/im-message/send-user-to-ai-customer-message";

    private static final String GET_HISTORY_MESSAGE = "/api/im-message/get-history-message";

    private static final String GET_AI_TO_USER_HISTORY_MESSAGE = "/api/im-message/get-ai-to-user-history-message";

    private static final String UPDATE_AI_CUSTOM_IMAGE_MESSAGE = "/api/im-message/update-ai-custom-image-message";

    private RpcClient rpcClient;
    private String host;

    public ImMessageClient(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";
        }
    }


    /**
     * 发送AI消息
     * @param input
     * @return
     */
    public void sendAiToUserTextMessage(SendAiTextMessageInput input) {
        rpcClient.post(host + SEND_AI_TO_USER_TEXT_MESSAGE, input, new TypeReference<Result<Void>>(){});
    }

    /**
     * 发送AI消息
     * @param input
     * @return
     */
    public void sendUserToAiTextMessage(SendAiTextMessageInput input) {
        rpcClient.post(host + SEND_USER_TO_AI_TEXT_MESSAGE, input, new TypeReference<Result<Void>>(){});
    }

    /**
     * 发送AI消息
     * @param input
     * @return
     */
    public void sendAiToUserCustomerMessage(SendAiCustomerMessageInput input) {
        rpcClient.post(host + SEND_AI_TO_USER_CUSTOMER_MESSAGE, input, new TypeReference<Result<Void>>(){});
    }

    /**
     * 发送AI消息
     * @param input
     * @return
     */
    public void sendUserToAiCustomerMessage(SendAiCustomerMessageInput input) {
        rpcClient.post(host + SEND_USER_TO_AI_CUSTOMER_MESSAGE, input, new TypeReference<Result<Void>>(){});
    }

    /**
     * 获取历史消息
     * @param input
     * @return
     */
    public List<HistoryMessageOutput> getHistoryMessage(HistoryMessageInput input) {
        return rpcClient.post(host + GET_HISTORY_MESSAGE, input, new TypeReference<Result<List<HistoryMessageOutput>>>(){});
    }

    /**
     * 获取历史消息
     * @param input
     * @return
     */
    public List<HistoryMessageOutput> getUserToAiHistoryMessage(HistoryMessageInput input) {
        return rpcClient.post(host + GET_HISTORY_MESSAGE, input, new TypeReference<Result<List<HistoryMessageOutput>>>(){});
    }

    /**
     * 获取历史消息
     * @param input
     * @return
     */
    public List<HistoryMessageOutput> getAiToUserHistoryMessage(HistoryMessageInput input) {
        return rpcClient.post(host + GET_AI_TO_USER_HISTORY_MESSAGE, input, new TypeReference<Result<List<HistoryMessageOutput>>>(){});
    }

    /**
     * 修改AI发送的自定义图片消息
     * @param input
     */
    public void updateAiSendCustomImageMessage(UpdateAiSendCustomImageMessageInput input) {
        rpcClient.post(host + UPDATE_AI_CUSTOM_IMAGE_MESSAGE, input, new TypeReference<Result<Void>>(){});
    }

}
