package com.sonic.bear.lib.client;

import com.alibaba.fastjson.TypeReference;
import com.google.common.collect.ImmutableMap;
import com.sonic.bear.lib.enums.OptTypeEnum;
import com.sonic.bear.lib.enums.UserTypeEnum;
import com.sonic.bear.lib.input.UserNicknameSyncPoolInput;
import com.sonic.bear.lib.output.NicknameOutput;
import com.sonic.common.AppRuntime;
import com.sonic.common.rpc.Result;
import com.sonic.common.rpc.RpcClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

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

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

    private static final String URI_USER_NICKNAME_EXIST_CHECK = "/api/user-nickname/exist-check";

    private static final String URI_USER_NICKNAME_SYNC_POOL = "/api/user-nickname/sync-pool";

    private static final String URI_USER_NICKNAME_BATCH_GET = "/api/user-nickname/batch-get";



    private RpcClient rpcClient;
    private String host;

    public UserNicknamePoolClient(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-bear-svc:8080";
                break;
            case product:
            default:
                this.host = "http://prod-bear-svc:8080";
        }
    }


    /**
     * 昵称重复性校验
     * @param nickname
     * @return
     */
    public boolean userNicknameExistCheck(String nickname) {
        return rpcClient.post(host + URI_USER_NICKNAME_EXIST_CHECK, ImmutableMap.of("nickname", nickname), new TypeReference<Result<Boolean>>(){});
    }

    /**
     * 昵称重复性校验
     * @param exUserId
     * @param nickname
     * @return
     */
    public boolean userNicknameExistCheck(Long exUserId, String nickname) {
        return rpcClient.post(host + URI_USER_NICKNAME_EXIST_CHECK, ImmutableMap.of("exUserId", exUserId, "nickname", nickname), new TypeReference<Result<Boolean>>(){});
    }

    /**
     * 同步AI昵称到数据池
     * @param nickname
     * @return
     */
    public void syncAiNickname(Long aiUserId, String nickname, OptTypeEnum optType) {
        UserNicknameSyncPoolInput input = UserNicknameSyncPoolInput.builder()
                .userId(aiUserId)
                .nickname(nickname)
                .userType(UserTypeEnum.AI.getCode())
                .optType(optType)
                .build();
        rpcClient.post(host + URI_USER_NICKNAME_SYNC_POOL, input, new TypeReference<Result<Void>>(){});
    }

    /**
     * 批量获取用户昵称
     * @param userIdList
     * @return
     */
    public Map<Long, String> batchGetNickname(List<Long> userIdList) {
        Map<String, Object> param = new HashMap<>(1);
        param.put("userIdList", userIdList);
        List<NicknameOutput> outputList = rpcClient.post(host + URI_USER_NICKNAME_BATCH_GET, param, new TypeReference<Result<List<NicknameOutput>>>(){});
        return outputList.stream().collect(HashMap::new, (m, v) -> m.put(v.getUserId(), v.getNickname()), HashMap::putAll);
    }


}
