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.BlurryRecordInput;
import com.sonic.shark.lib.input.CopyAndBlurryImgInput;
import com.sonic.shark.lib.output.BlurryRecordOutput;
import com.sonic.shark.lib.output.CopyAndBlurryImgOutput;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

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

    private static final String COPY_AND_BLURRY_IMG_URL = "/api/img/copy-and-blurry";

    private static final String BLURRY_RECORD_LIST_URL = "/api/img/blurry-record-list";

    private RpcClient rpcClient;
    private String host;

    public S3BlurryImgClient(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 input
     * @return 返回原图地址
     */
    public String copyAndBlurryImg(CopyAndBlurryImgInput input) {
        List<CopyAndBlurryImgInput> inputList = Lists.newArrayList();
        inputList.add(input);
        List<CopyAndBlurryImgOutput> list = rpcClient.postBodySign(host + COPY_AND_BLURRY_IMG_URL, input, new TypeReference<Result<List<CopyAndBlurryImgOutput>>>(){});
        if(CollectionUtils.isEmpty(list)) {
            return null;
        }
        return list.get(0).getImgUrl();
    }

    /**
     * 批量拷贝相册图片并生成模糊图片访问地址   key是业务类型ID
     * @param inputList
     * @return
     */
    public Map<Long, String> copyAndBlurryAlbumImg(List<CopyAndBlurryImgInput> inputList) {
        List<CopyAndBlurryImgOutput> list = rpcClient.postBodySign(host + COPY_AND_BLURRY_IMG_URL, inputList, new TypeReference<Result<List<CopyAndBlurryImgOutput>>>(){});
        Map<Long, String> resultMap = list.stream().collect(Collectors.toMap(e -> e.getBizId(), e -> e.getImgUrl()));
        return resultMap;
    }

    /**
     * 批量拷贝相册图片并生成模糊图片访问地址   key是图片相对路径ID
     * @param inputList
     * @return
     */
    public Map<String, String> copyAndBlurryTopicImg(List<CopyAndBlurryImgInput> inputList) {
        List<CopyAndBlurryImgOutput> list = rpcClient.postBodySign(host + COPY_AND_BLURRY_IMG_URL, inputList, new TypeReference<Result<List<CopyAndBlurryImgOutput>>>(){});
        Map<String, String> resultMap = list.stream().collect(Collectors.toMap(e -> e.getSourceImgPath(), e -> e.getImgUrl()));
        return resultMap;
    }

    /**
     * 批量拷贝相册图片并生成模糊图片访问地址
     * @param input
     * @return
     */
    public CopyAndBlurryImgOutput copyAndBlurryTopicImg(CopyAndBlurryImgInput input) {
        List<CopyAndBlurryImgInput> inputList = Lists.newArrayList(input);
        List<CopyAndBlurryImgOutput> list = rpcClient.postBodySign(host + COPY_AND_BLURRY_IMG_URL, inputList, new TypeReference<Result<List<CopyAndBlurryImgOutput>>>(){});
        return CollectionUtils.isEmpty(list) ? null : list.get(0);
    }

    /**
     * 批量查询模糊图片数据 key是相对路径
     * @param bizType
     * @param bizIdList
     * @return
     */
    public Map<String, BlurryRecordOutput> blurryRecordImgPathMap(String bizType, List<Long> bizIdList) {
        BlurryRecordInput input = new BlurryRecordInput();
        input.setBizType(bizType);
        input.setBizIdList(bizIdList);
        List<BlurryRecordOutput> list = rpcClient.postBodySign(host + BLURRY_RECORD_LIST_URL, input, new TypeReference<Result<List<BlurryRecordOutput>>>(){});
        return list.stream().collect(Collectors.toMap(e -> e.getImgPath(), Function.identity()));
    }

    /**
     * 批量查询模糊图片数据 key是全路径
     * @param bizType
     * @param bizIdList
     * @return
     */
    public Map<String, BlurryRecordOutput> blurryRecordImgUrlMap(String bizType, List<Long> bizIdList) {
        BlurryRecordInput input = new BlurryRecordInput();
        input.setBizType(bizType);
        input.setBizIdList(bizIdList);
        input.setImgUrlBl(true);
        List<BlurryRecordOutput> list = rpcClient.postBodySign(host + BLURRY_RECORD_LIST_URL, input, new TypeReference<Result<List<BlurryRecordOutput>>>(){});
        return list.stream().collect(Collectors.toMap(e -> e.getImgUrl(), Function.identity()));
    }

    /**
     * 批量查询模糊图片数据   key是业务类型ID
     * @param bizType
     * @param bizIdList
     * @return
     */
    public Map<Long, BlurryRecordOutput> blurryRecordBizIdMap(String bizType, List<Long> bizIdList) {
        BlurryRecordInput input = new BlurryRecordInput();
        input.setBizType(bizType);
        input.setBizIdList(bizIdList);
        input.setImgUrlBl(false);
        List<BlurryRecordOutput> list = rpcClient.postBodySign(host + BLURRY_RECORD_LIST_URL, input, new TypeReference<Result<List<BlurryRecordOutput>>>(){});
        return list.stream().collect(Collectors.toMap(e -> e.getBizId(), Function.identity()));
    }


}
