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

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

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.web.modules.circulation.domain.BizTradeMarket;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

@DisplayName("TradeMarketValidation")
class TradeMarketValidationTest
{
    private static final String VALID_PHONE = "13800138000";

    @Test
    @DisplayName("市场名称长度")
    void marketName()
    {
        assertThrows(ServiceException.class, () -> TradeMarketValidation.validateMarketName(""));
        assertThrows(ServiceException.class, () -> TradeMarketValidation.validateMarketName(longStr(11)));
        assertDoesNotThrow(() -> TradeMarketValidation.validateMarketName("巴青市场"));
    }

    @Test
    @DisplayName("详细地址")
    void detailAddress()
    {
        assertThrows(ServiceException.class, () -> TradeMarketValidation.validateDetailAddress(""));
        assertThrows(ServiceException.class, () -> TradeMarketValidation.validateDetailAddress(longStr(201)));
        assertDoesNotThrow(() -> TradeMarketValidation.validateDetailAddress(longStr(200)));
    }

    @Test
    @DisplayName("负责人")
    void personInCharge()
    {
        assertThrows(ServiceException.class, () -> TradeMarketValidation.validatePersonInCharge(""));
        assertThrows(ServiceException.class, () -> TradeMarketValidation.validatePersonInCharge(longStr(11)));
        assertDoesNotThrow(() -> TradeMarketValidation.validatePersonInCharge("王五"));
    }

    @Test
    @DisplayName("手机号")
    void phone()
    {
        assertThrows(ServiceException.class, () -> TradeMarketValidation.validateContactPhone("1234567890"));
        assertDoesNotThrow(() -> TradeMarketValidation.validateContactPhone(VALID_PHONE));
    }

    @Test
    @DisplayName("运营状态")
    void operateStatus()
    {
        assertEquals(TradeMarketRules.OPERATE_STATUS_NORMAL,
                TradeMarketValidation.resolveOperateStatus(null));
        assertDoesNotThrow(() -> TradeMarketValidation.validateOperateStatus(3));
        assertThrows(ServiceException.class, () -> TradeMarketValidation.validateOperateStatus(4));
    }

    @Test
    @DisplayName("照片成对")
    void photoPair()
    {
        assertThrows(ServiceException.class,
                () -> TradeMarketValidation.validatePhotoPair("http://x/a.jpg", null));
        assertDoesNotThrow(() -> TradeMarketValidation.validatePhotoPair(null, null));
        assertDoesNotThrow(() -> TradeMarketValidation.validatePhotoPair(
                "http://localhost/profile/a.jpg", Constants.RESOURCE_PREFIX + "/upload/a.jpg"));
    }

    @Test
    @DisplayName("整行校验默认运营状态")
    void validateRowDefaultStatus()
    {
        BizTradeMarket row = sampleRow();
        row.setOperateStatus(null);
        TradeMarketValidation.validateRowForSave(row);
        assertEquals(TradeMarketRules.OPERATE_STATUS_NORMAL, row.getOperateStatus());
    }

    private static BizTradeMarket sampleRow()
    {
        BizTradeMarket r = new BizTradeMarket();
        r.setMarketName("巴青交易市场");
        r.setDetailAddress("西藏那曲市巴青县交易路1号");
        r.setPersonInCharge("王五");
        r.setContactPhone(VALID_PHONE);
        r.setOperateStatus(TradeMarketRules.OPERATE_STATUS_NORMAL);
        return r;
    }

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