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

import com.heyu.api.baidu.request.doc.BForgeryDetectionRequest;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

/**
 * BForgeryDetectionHandle 单元测试
 * 测试参数校验和内容拼接逻辑
 */
public class BForgeryDetectionHandleTest {

    private BForgeryDetectionHandle handle;

    @Before
    public void setUp() {
        handle = new BForgeryDetectionHandle();
    }

    // ========== URI 测试 ==========

    @Test
    public void getUriTest() {
        assertEquals("/rest/2.0/ocr/v1/forgery_detection", handle.getUri());
    }

    // ========== 图片校验测试 ==========

    @Test
    public void checkImageAndUrlBothEmptyTest() {
        BForgeryDetectionRequest request = new BForgeryDetectionRequest();
        String error = handle.check(request);
        assertNotNull(error);
        assertTrue(error.contains("不能都为空"));
    }

    @Test
    public void checkWithImageBase64Test() {
        BForgeryDetectionRequest request = new BForgeryDetectionRequest();
        request.setImageBase64("base64Data");
        String error = handle.check(request);
        assertNull(error);
    }

    @Test
    public void checkWithImageUrlTest() {
        BForgeryDetectionRequest request = new BForgeryDetectionRequest();
        request.setImageUrl("https://example.com/image.jpg");
        String error = handle.check(request);
        assertNull(error);
    }

    // ========== detectProportion 校验测试 ==========

    @Test
    public void checkDetectProportionValidTrueTest() {
        BForgeryDetectionRequest request = new BForgeryDetectionRequest();
        request.setImageBase64("data");
        request.setDetectProportion("true");
        assertNull(handle.check(request));
    }

    @Test
    public void checkDetectProportionValidFalseTest() {
        BForgeryDetectionRequest request = new BForgeryDetectionRequest();
        request.setImageBase64("data");
        request.setDetectProportion("false");
        assertNull(handle.check(request));
    }

    @Test
    public void checkDetectProportionInvalidTest() {
        BForgeryDetectionRequest request = new BForgeryDetectionRequest();
        request.setImageBase64("data");
        request.setDetectProportion("yes");
        String error = handle.check(request);
        assertNotNull(error);
        assertTrue(error.contains("detectProportion"));
    }

    // ========== detectThreshold 校验测试 ==========

    @Test
    public void checkDetectThresholdMinValidTest() {
        BForgeryDetectionRequest request = new BForgeryDetectionRequest();
        request.setImageBase64("data");
        request.setDetectThreshold("0.0001");
        assertNull(handle.check(request));
    }

    @Test
    public void checkDetectThresholdMaxValidTest() {
        BForgeryDetectionRequest request = new BForgeryDetectionRequest();
        request.setImageBase64("data");
        request.setDetectThreshold("1");
        assertNull(handle.check(request));
    }

    @Test
    public void checkDetectThresholdTooSmallTest() {
        BForgeryDetectionRequest request = new BForgeryDetectionRequest();
        request.setImageBase64("data");
        request.setDetectThreshold("0.00001");
        String error = handle.check(request);
        assertNotNull(error);
        assertTrue(error.contains("detectThreshold"));
    }

    @Test
    public void checkDetectThresholdTooLargeTest() {
        BForgeryDetectionRequest request = new BForgeryDetectionRequest();
        request.setImageBase64("data");
        request.setDetectThreshold("1.1");
        String error = handle.check(request);
        assertNotNull(error);
        assertTrue(error.contains("detectThreshold"));
    }

    @Test
    public void checkDetectThresholdNotNumberTest() {
        BForgeryDetectionRequest request = new BForgeryDetectionRequest();
        request.setImageBase64("data");
        request.setDetectThreshold("abc");
        String error = handle.check(request);
        assertNotNull(error);
        assertTrue(error.contains("detectThreshold"));
    }

    // ========== returnHeatmap 校验测试 ==========

    @Test
    public void checkReturnHeatmapValidTest() {
        BForgeryDetectionRequest request = new BForgeryDetectionRequest();
        request.setImageBase64("data");
        request.setReturnHeatmap("true");
        assertNull(handle.check(request));
    }

    @Test
    public void checkReturnHeatmapInvalidTest() {
        BForgeryDetectionRequest request = new BForgeryDetectionRequest();
        request.setImageBase64("data");
        request.setReturnHeatmap("1");
        String error = handle.check(request);
        assertNotNull(error);
        assertTrue(error.contains("returnHeatmap"));
    }

    // ========== restrictProbability 校验测试 ==========

    @Test
    public void checkRestrictProbabilityMinValidTest() {
        BForgeryDetectionRequest request = new BForgeryDetectionRequest();
        request.setImageBase64("data");
        request.setRestrictProbability("0.1");
        assertNull(handle.check(request));
    }

    @Test
    public void checkRestrictProbabilityMaxValidTest() {
        BForgeryDetectionRequest request = new BForgeryDetectionRequest();
        request.setImageBase64("data");
        request.setRestrictProbability("1");
        assertNull(handle.check(request));
    }

    @Test
    public void checkRestrictProbabilityTooSmallTest() {
        BForgeryDetectionRequest request = new BForgeryDetectionRequest();
        request.setImageBase64("data");
        request.setRestrictProbability("0.05");
        String error = handle.check(request);
        assertNotNull(error);
        assertTrue(error.contains("restrictProbability"));
    }

    @Test
    public void checkRestrictProbabilityTooLargeTest() {
        BForgeryDetectionRequest request = new BForgeryDetectionRequest();
        request.setImageBase64("data");
        request.setRestrictProbability("1.5");
        String error = handle.check(request);
        assertNotNull(error);
        assertTrue(error.contains("restrictProbability"));
    }

    @Test
    public void checkRestrictProbabilityNotNumberTest() {
        BForgeryDetectionRequest request = new BForgeryDetectionRequest();
        request.setImageBase64("data");
        request.setRestrictProbability("xyz");
        String error = handle.check(request);
        assertNotNull(error);
        assertTrue(error.contains("restrictProbability"));
    }

    // ========== 可选参数为空时不校验 ==========

    @Test
    public void checkOptionalParamsNullTest() {
        BForgeryDetectionRequest request = new BForgeryDetectionRequest();
        request.setImageBase64("data");
        // 所有可选参数都不设置
        assertNull(handle.check(request));
    }

    // ========== getContent 内容拼接测试 ==========

    @Test
    public void getContentImageOnlyTest() {
        BForgeryDetectionRequest request = new BForgeryDetectionRequest();
        request.setImageBase64("base64Img");
        String content = handle.getContent(request);
        assertTrue(content.contains("&image=base64Img"));
        assertFalse(content.contains("&url="));
        assertFalse(content.contains("&detect_proportion="));
    }

    @Test
    public void getContentUrlOnlyTest() {
        BForgeryDetectionRequest request = new BForgeryDetectionRequest();
        request.setImageUrl("https://example.com/img.jpg");
        String content = handle.getContent(request);
        assertTrue(content.contains("&url=https://example.com/img.jpg"));
        assertFalse(content.contains("&image="));
    }

    @Test
    public void getContentAllParamsTest() {
        BForgeryDetectionRequest request = new BForgeryDetectionRequest();
        request.setImageBase64("base64Img");
        request.setDetectProportion("true");
        request.setDetectThreshold("0.9");
        request.setReturnHeatmap("true");
        request.setRestrictProbability("0.8");

        String content = handle.getContent(request);

        assertTrue(content.contains("&image=base64Img"));
        assertTrue(content.contains("&detect_proportion=true"));
        assertTrue(content.contains("&detect_threshold=0.9"));
        assertTrue(content.contains("&return_heatmap=true"));
        assertTrue(content.contains("&restrict_probability=0.8"));
    }

    @Test
    public void getContentOptionalParamsNullTest() {
        BForgeryDetectionRequest request = new BForgeryDetectionRequest();
        request.setImageBase64("base64Img");
        // 不设置任何可选参数
        String content = handle.getContent(request);
        assertTrue(content.contains("&image=base64Img"));
        assertFalse(content.contains("&detect_proportion="));
        assertFalse(content.contains("&detect_threshold="));
        assertFalse(content.contains("&return_heatmap="));
        assertFalse(content.contains("&restrict_probability="));
    }
}
