package com.ruoyi.web.modules.prosperity.support;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.List;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.web.modules.prosperity.domain.BizCommonProsperityProjectOpImage;

/**
 * GTFY-XM-UT-005～012
 */
@DisplayName("CommonProsperityProjectValidation")
class CommonProsperityProjectValidationTest
{

    @Test
    @DisplayName("GTFY-XM-UT-005 项目名称长度")
    void ut005_projectName()
    {
        assertThrows(ServiceException.class, () -> CommonProsperityProjectValidation.validateProjectName(""));
        assertThrows(ServiceException.class, () -> CommonProsperityProjectValidation.validateProjectName(longStr(65)));
        assertDoesNotThrow(() -> CommonProsperityProjectValidation.validateProjectName("项"));
        assertDoesNotThrow(() -> CommonProsperityProjectValidation.validateProjectName(longStr(64)));
    }

    @Test
    @DisplayName("GTFY-XM-UT-006 项目类型")
    void ut006_projectType()
    {
        assertDoesNotThrow(() -> CommonProsperityProjectValidation.validateProjectType(1));
        assertDoesNotThrow(() -> CommonProsperityProjectValidation.validateProjectType(3));
        assertThrows(ServiceException.class, () -> CommonProsperityProjectValidation.validateProjectType(0));
        assertThrows(ServiceException.class, () -> CommonProsperityProjectValidation.validateProjectType(4));
        assertThrows(ServiceException.class, () -> CommonProsperityProjectValidation.validateProjectType(null));
    }

    @Test
    @DisplayName("GTFY-XM-UT-007 简介选填/内容字数")
    void ut007_introContent()
    {
        assertDoesNotThrow(() -> CommonProsperityProjectValidation.validateIntroduction(null));
        assertDoesNotThrow(() -> CommonProsperityProjectValidation.validateIntroduction(""));
        assertThrows(ServiceException.class, () -> CommonProsperityProjectValidation.validateIntroduction(longStr(51)));
        assertDoesNotThrow(() -> CommonProsperityProjectValidation.validateProjectContent(null));
        assertDoesNotThrow(() -> CommonProsperityProjectValidation.validateProjectContent(""));
        assertThrows(ServiceException.class, () -> CommonProsperityProjectValidation.validateProjectContent(longStr(501)));
        assertDoesNotThrow(() -> CommonProsperityProjectValidation.validateIntroduction("简介"));
        assertDoesNotThrow(() -> CommonProsperityProjectValidation.validateProjectContent("内容"));
    }

    @Test
    @DisplayName("GTFY-XM-UT-008 实施年限")
    void ut008_implYear()
    {
        int current = Calendar.getInstance().get(Calendar.YEAR);
        int minYear = current - CommonProsperityProjectRules.IMPL_YEAR_LOOKBACK;
        assertThrows(ServiceException.class, () -> CommonProsperityProjectValidation.validateImplYear(null));
        assertThrows(ServiceException.class, () -> CommonProsperityProjectValidation.validateImplYear(minYear - 1));
        assertThrows(ServiceException.class, () -> CommonProsperityProjectValidation.validateImplYear(current + 1));
        assertDoesNotThrow(() -> CommonProsperityProjectValidation.validateImplYear(current));
        assertDoesNotThrow(() -> CommonProsperityProjectValidation.validateImplYear(minYear));
    }

    @Test
    @DisplayName("GTFY-XM-UT-008b 投资金额")
    void ut008b_investment()
    {
        assertDoesNotThrow(() -> CommonProsperityProjectValidation.validateInvestmentAmount(null));
        assertDoesNotThrow(() -> CommonProsperityProjectValidation.validateInvestmentAmount(BigDecimal.ZERO));
        assertThrows(ServiceException.class,
                () -> CommonProsperityProjectValidation.validateInvestmentAmount(new BigDecimal("-1")));
    }

    @Test
    @DisplayName("GTFY-XM-UT-009 运营图片数量")
    void ut009_operationImageCount()
    {
        assertThrows(ServiceException.class,
                () -> CommonProsperityProjectValidation.validateOperationImages(Collections.emptyList()));
        assertDoesNotThrow(() -> CommonProsperityProjectValidation.validateOperationImages(
                Collections.singletonList(sampleImage(1))));
        assertDoesNotThrow(() -> CommonProsperityProjectValidation.validateOperationImages(
                Arrays.asList(sampleImage(1), sampleImage(2), sampleImage(3))));
        List<BizCommonProsperityProjectOpImage> four = new ArrayList<>();
        four.add(sampleImage(1));
        four.add(sampleImage(2));
        four.add(sampleImage(3));
        four.add(sampleImage(1));
        assertThrows(ServiceException.class, () -> CommonProsperityProjectValidation.validateOperationImages(four));
    }

    @Test
    @DisplayName("GTFY-XM-UT-010 sortOrder 唯一")
    void ut010_sortOrderUnique()
    {
        BizCommonProsperityProjectOpImage a = sampleImage(1);
        BizCommonProsperityProjectOpImage b = sampleImage(1);
        assertThrows(ServiceException.class,
                () -> CommonProsperityProjectValidation.validateOperationImages(Arrays.asList(a, b)));
    }

    @Test
    @DisplayName("GTFY-XM-UT-012 视频/封面成对")
    void ut012_videoCoverPair()
    {
        assertDoesNotThrow(() -> CommonProsperityProjectValidation.validateVideoPair(null, null));
        assertDoesNotThrow(() -> CommonProsperityProjectValidation.validateVideoPair(
                "http://v", Constants.RESOURCE_PREFIX + "/upload/v.mp4"));
        assertThrows(ServiceException.class, () -> CommonProsperityProjectValidation.validateVideoPair(
                "http://v", null));
        assertDoesNotThrow(() -> CommonProsperityProjectValidation.validateCoverPair(
                "http://c", Constants.RESOURCE_PREFIX + "/upload/c.jpg"));
        assertThrows(ServiceException.class, () -> CommonProsperityProjectValidation.validateCoverPair(
                "http://c", null));
    }

    private static BizCommonProsperityProjectOpImage sampleImage(int sortOrder)
    {
        BizCommonProsperityProjectOpImage img = new BizCommonProsperityProjectOpImage();
        img.setFileUrl("http://img");
        img.setFilePath(Constants.RESOURCE_PREFIX + "/upload/img" + sortOrder + ".jpg");
        img.setSortOrder(sortOrder);
        return img;
    }

    private static String longStr(int len)
    {
        StringBuilder sb = new StringBuilder(len);
        for (int i = 0; i < len; i++)
        {
            sb.append('x');
        }
        return sb.toString();
    }
}
