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.List;

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

    private static final String URI_S3_CHECK_CHECK_IMAGE = "/api/s3-check/check-image";

    private RpcClient rpcClient;
    private String host;

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

    /**
     * 图片校验
     * @param url
     */
    public void checkImage(String url) {
        checkImage(null, url);
    }

    /**
     * 图片校验
     * @param urlList
     * @return
     */
    public void checkImage(List<String> urlList) {
        checkImage(null, urlList);
    }

    /**
     * 图片校验
     * @param userId
     * @param url
     */
    public void checkImage(Long userId, String url) {
        if(StringUtils.isEmpty(url)) {
            return;
        }
        ImageCheckInput input = new ImageCheckInput();
        input.setUrls(Lists.newArrayList(url));
        input.setUserId(userId);
        rpcClient.post(host + URI_S3_CHECK_CHECK_IMAGE, input, new TypeReference<Result<Session>>(){});
    }

    /**
     * 图片校验
     * @param userId
     * @param urlList
     * @return
     */
    public void checkImage(Long userId, List<String> urlList) {
        if(CollectionUtils.isEmpty(urlList)) {
            return;
        }
        ImageCheckInput input = new ImageCheckInput();
        input.setUserId(userId);
        input.setUrls(urlList);
        rpcClient.post(host + URI_S3_CHECK_CHECK_IMAGE, input, new TypeReference<Result<Session>>(){});
    }

}
