package com.sonic.frog.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.frog.lib.output.AIUserAlbumApiOutput;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

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

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

    private static final String GET_RANDOM_LOCK_IMAGE = "/api/ai-user-album/random-lock-image";
    private static final String USE_CREATE_COUNT = "/api/ai-user-album/use-create-count";

    private RpcClient rpcClient;
    private String host;

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


    /**
     * 获取一张随机未解锁的图片
     *
     * @param userId
     * @param aiId
     * @return
     */
    public AIUserAlbumApiOutput getRandomLockImage(Long userId, Long aiId) {
        Map<String, Object> input = new HashMap<>();
        input.put("userId", userId);
        input.put("aiId", aiId);
        return rpcClient.post(host + GET_RANDOM_LOCK_IMAGE, input, new TypeReference<Result<AIUserAlbumApiOutput>>() {
        });
    }


    /**
     * 编辑形象或创建相册图片时，使用创作次数
     *
     * @param userId
     * @return
     */
    public Void useCreateCount(Long userId) {
        Map<String, Object> input = new HashMap<>();
        input.put("userId", userId);
        return rpcClient.post(host + USE_CREATE_COUNT, input, new TypeReference<Result<Void>>() {
        });
    }

}
