package com.sonic.pigeon.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.netease.nim.server.sdk.core.Result;
import com.netease.nim.server.sdk.core.exception.YunxinSdkException;
import com.netease.nim.server.sdk.im.v2.YunxinV2ApiServices;
import com.netease.nim.server.sdk.im.v2.conversation.request.CreateConversationRequestV2;
import com.netease.nim.server.sdk.im.v2.conversation.request.UpdateConversationRequestV2;
import com.netease.nim.server.sdk.im.v2.conversation.response.CreateConversationResponseV2;
import com.netease.nim.server.sdk.im.v2.conversation.response.UpdateConversationResponseV2;
import com.sonic.pigeon.lib.enums.ImUserTypeEnum;
import com.sonic.pigeon.lib.input.CreateConversationInput;
import com.sonic.pigeon.lib.input.UpdateConversationInput;
import com.sonic.pigeon.service.ImConversationService;
import com.sonic.pigeon.utils.ImGenerator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class ImConversationServiceImpl implements ImConversationService {

    @Autowired
    private YunxinV2ApiServices yunxinV2ApiServices;
    @Autowired
    private ImGenerator imGenerator;

    @Override
    public void createConversation(CreateConversationInput input) {
        CreateConversationRequestV2 request = new CreateConversationRequestV2();
        String fromAccountId = imGenerator.genImAccountId(input.getFromUserId(), ImUserTypeEnum.u);
        String toAccountId = imGenerator.genImAccountId(input.getToUserId(), ImUserTypeEnum.r);
        request.setConversationId(fromAccountId + "|1|" + toAccountId);
        try {
            Result<CreateConversationResponseV2> result = yunxinV2ApiServices.getConversationService().createConversation(request);
            if (result.isSuccess()) {
                CreateConversationResponseV2 response = result.getResponse();
                // 更新成功
                log.info("===> createConversation success, response : {}, traceId : {}", JSONObject.toJSONString(response), result.getTraceId());
            } else {
                // 更新失败，如参数错误等
                log.info("===> createConversation fail, code : {}, msg : {}, traceId : {}", result.getCode(), result.getMsg(), result.getTraceId());
            }
        } catch (YunxinSdkException e) {
            // 超时等异常
            log.error("===> createConversation error, endpoint : {}, traceId : {}", e.getContext().getEndpoint(), e.getTraceId());
        }
    }

    @Override
    public void updateConversationExtension(UpdateConversationInput input) {
        UpdateConversationRequestV2 request = new UpdateConversationRequestV2();
        String fromAccountId = imGenerator.genImAccountId(input.getFromUserId(), ImUserTypeEnum.u);
        String toAccountId = imGenerator.genImAccountId(input.getToUserId(), ImUserTypeEnum.r);
        request.setConversationId(fromAccountId + "|1|" + toAccountId);
        request.setServerExtension(input.getExtension());
        try {
            Result<UpdateConversationResponseV2> result = yunxinV2ApiServices.getConversationService().updateConversation(request);
            if (result.isSuccess()) {
                UpdateConversationResponseV2 response = result.getResponse();
                // 更新成功
                log.info("===> updateConversationExtension success, response : {}, traceId : {}", JSONObject.toJSONString(response), result.getTraceId());
            } else {
                // 更新失败，如参数错误等
                log.info("===> updateConversationExtension fail, code : {}, msg : {}, traceId : {}", result.getCode(), result.getMsg(), result.getTraceId());
            }
        } catch (YunxinSdkException e) {
            // 超时等异常
            log.error("===> updateConversationExtension error, endpoint : {}, traceId : {}", e.getContext().getEndpoint(), e.getTraceId());
        }
    }

}
