package com.heyu.api.baidu.handle.doc;

import com.heyu.api.baidu.BaiduBaseHandle;
import com.heyu.api.baidu.request.doc.BForgeryDetectionRequest;
import com.heyu.api.baidu.response.doc.BForgeryDetectionResp;
import com.heyu.api.data.utils.StringUtils;
import org.springframework.stereotype.Component;

/**
 * 图片篡改检测 - Handle
 *
 * 百度文档：https://cloud.baidu.com/doc/OCR/s/kmfdnccok
 * URI: /rest/2.0/ocr/v1/forgery_detection
 */
@Component
public class BForgeryDetectionHandle extends BaiduBaseHandle<BForgeryDetectionRequest, BForgeryDetectionResp> {

    private static final double THRESHOLD_MIN = 0.0001;
    private static final double THRESHOLD_MAX = 1.0;
    private static final double PROBABILITY_MIN = 0.1;
    private static final double PROBABILITY_MAX = 1.0;

    @Override
    public String getUri() {
        return "/rest/2.0/ocr/v1/forgery_detection";
    }

    @Override
    public String check(BForgeryDetectionRequest request) {
        String imageError = checkImageUri(request);
        if (imageError != null) {
            return imageError;
        }

        if (StringUtils.isNotBlank(request.getDetectProportion())) {
            if (checkNotTrueFalse(request.getDetectProportion())) {
                return "detectProportion 必须传 true 或 false";
            }
        }

        if (StringUtils.isNotBlank(request.getDetectThreshold())) {
            try {
                double threshold = Double.parseDouble(request.getDetectThreshold());
                if (threshold < THRESHOLD_MIN || threshold > THRESHOLD_MAX) {
                    return "detectThreshold 范围必须在 " + THRESHOLD_MIN + "～" + THRESHOLD_MAX + " 之间";
                }
            } catch (NumberFormatException e) {
                return "detectThreshold 必须为数字";
            }
        }

        if (StringUtils.isNotBlank(request.getReturnHeatmap())) {
            if (checkNotTrueFalse(request.getReturnHeatmap())) {
                return "returnHeatmap 必须传 true 或 false";
            }
        }

        if (StringUtils.isNotBlank(request.getRestrictProbability())) {
            try {
                double probability = Double.parseDouble(request.getRestrictProbability());
                if (probability < PROBABILITY_MIN || probability > PROBABILITY_MAX) {
                    return "restrictProbability 范围必须在 " + PROBABILITY_MIN + "～" + PROBABILITY_MAX + " 之间";
                }
            } catch (NumberFormatException e) {
                return "restrictProbability 必须为数字";
            }
        }

        return null;
    }

    @Override
    public String getContent(BForgeryDetectionRequest request) {
        StringBuffer sb = getImageContent(request);

        if (StringUtils.isNotBlank(request.getDetectProportion())) {
            sb.append("&detect_proportion=").append(request.getDetectProportion());
        }
        if (StringUtils.isNotBlank(request.getDetectThreshold())) {
            sb.append("&detect_threshold=").append(request.getDetectThreshold());
        }
        if (StringUtils.isNotBlank(request.getReturnHeatmap())) {
            sb.append("&return_heatmap=").append(request.getReturnHeatmap());
        }
        if (StringUtils.isNotBlank(request.getRestrictProbability())) {
            sb.append("&restrict_probability=").append(request.getRestrictProbability());
        }

        return sb.toString();
    }
}
