package com.ycwl.basic.image.enhancer.adapter;

import com.baidu.aip.imageprocess.AipImageProcess;
import com.ycwl.basic.image.enhancer.entity.BceEnhancerConfig;
import com.ycwl.basic.image.util.ImageUtil;
import com.ycwl.basic.utils.ImageUtils;
import org.json.JSONObject;
import org.springframework.web.multipart.MultipartFile;

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

public class BceImageEnhancer implements IEnhancer {
    private BceEnhancerConfig config;

    public boolean setConfig(BceEnhancerConfig config) {
        this.config = config;
        return true;
    }

    @Override
    public boolean loadConfig(Map<String, String> _config) {
        BceEnhancerConfig config = new BceEnhancerConfig();
        config.setAppId(_config.get("appId"));
        config.setApiKey(_config.get("apiKey"));
        config.setSecretKey(_config.get("secretKey"));
        config.setQps(Float.parseFloat(_config.get("qps")));
        this.config = config;
        return true;
    }

    @Override
    public MultipartFile enhance(String url) {
        AipImageProcess client = getClient();
        HashMap<String, String> options = new HashMap<>();
        JSONObject jsonObject = client.imageDefinitionEnhance(url, options);
        return ImageUtils.base64ToMultipartFile(jsonObject.getString("image"));
    }

    public AipImageProcess getClient() {
        AipImageProcess client = new AipImageProcess(config.getAppId(), config.getApiKey(), config.getSecretKey());
        return client;
    }
}
