package com.sonic.shark.lib.client;

import com.alibaba.fastjson.TypeReference;
import com.google.common.collect.Lists;
import com.sonic.common.AppRuntime;
import com.sonic.common.auth.domains.Session;
import com.sonic.common.rpc.Result;
import com.sonic.common.rpc.RpcClient;
import com.sonic.shark.lib.input.ImageCheckInput;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

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

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

    private static final String UPLOAD_IMG_TO_AWS_URL = "/api/oss/upload/aws/s3";
    private static final String NSFW_RECOGNITION_URL = "/api/nsfw/recognition";

    private RpcClient rpcClient;
    private String host;

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

    public String uploadAwsS3(byte[] bytes, String bizTypeEnum, String suffix) {
        Map<String, Object> param = new HashMap<>(3);
        param.put("bytes", bytes);
        param.put("bizTypeEnum", bizTypeEnum);
        param.put("suffix", suffix);
        String resultUrl = rpcClient.postBodySign(host + UPLOAD_IMG_TO_AWS_URL, param, new TypeReference<Result<String>>() {
        });
        return resultUrl;
    }

    public Boolean nsfwRecognition(String fileFullPath, String bizTypeEnum) {
        Map<String, Object> param = new HashMap<>(3);
        param.put("fileFullPath", fileFullPath);
        param.put("bizTypeEnum", bizTypeEnum);
        return rpcClient.postBodySign(host + NSFW_RECOGNITION_URL, param, new TypeReference<Result<Boolean>>() {
        });
    }

}
