package com.ssj.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ssj.framework.core.util.PropertiesUtil;
import com.ssj.framework.core.util.StringUtil;
import com.ssj.service.kmt.dto.exercise.ExerciseQuestionAnswerStyle;
import com.ssj.service.kmt.dto.exercise.ExerciseQuestionAnswerValue;
import com.ssj.weixin.utils.HttpUtils;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;

/**
 * @author yrf
 * @date 2020/6/30 20:18
 */
public class ExerciseUtil {

    private static final Logger logger = LoggerFactory.getLogger(ExerciseUtil.class);

    private ExerciseUtil() {
    }

    public static ExerciseQuestionAnswerValue getExerciseQuestionAnswerValue(String imageUrl) {
        try {
            HttpResponse response = HttpUtils.doGet(PropertiesUtil.getValue("OSS_INTERNAL_PATH"), imageUrl ,
                    "Get",
                    Collections.emptyMap(),
                    Collections.singletonMap("x-oss-process", "image/info")
            );
            if(response.getStatusLine().getStatusCode() != 200){
                return null;
            }
            String imageInfo = EntityUtils.toString(response.getEntity());
            if("Error".equals(imageInfo)){
                return null;
            }
            if (StringUtil.isNotBlank(imageInfo)){
                JSONObject jsonObject = JSON.parseObject(imageInfo);
                ExerciseQuestionAnswerValue questionJson = new ExerciseQuestionAnswerValue();
                questionJson.setType(2);
                questionJson.setValue(PropertiesUtil.getValue("STATIC_FILE_URL") + imageUrl);
                ExerciseQuestionAnswerStyle questionStyle  = new ExerciseQuestionAnswerStyle();
                questionStyle.setHeight(jsonObject.getJSONObject("ImageHeight").getInteger("value"));
                questionStyle.setWidth(jsonObject.getJSONObject("ImageWidth").getInteger("value"));
                questionJson.setStyle(questionStyle);

                return questionJson;
            }
            return null;
        } catch (Exception e) {
            logger.error("异常", e);
        }

        return null;
    }

}
