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.BizQuarantineStation;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

/**
 * ZCZX-JYZGL-UT-001～007
 */
@DisplayName("QuarantineStationValidation")
class QuarantineStationValidationTest
{
    private static final String VALID_PHONE = "13800138000";

    @Test
    @DisplayName("ZCZX-JYZGL-UT-001 检疫站名称")
    void ut001_stationName()
    {
        assertThrows(ServiceException.class, () -> QuarantineStationValidation.validateStationName(""));
        assertThrows(ServiceException.class, () -> QuarantineStationValidation.validateStationName(longStr(11)));
        assertDoesNotThrow(() -> QuarantineStationValidation.validateStationName("巴青站"));
        assertDoesNotThrow(() -> QuarantineStationValidation.validateStationName(longStr(10)));
    }

    @Test
    @DisplayName("ZCZX-JYZGL-UT-002 详细地址")
    void ut002_detailAddress()
    {
        assertThrows(ServiceException.class, () -> QuarantineStationValidation.validateDetailAddress(""));
        assertThrows(ServiceException.class, () -> QuarantineStationValidation.validateDetailAddress(longStr(201)));
        assertDoesNotThrow(() -> QuarantineStationValidation.validateDetailAddress(longStr(200)));
    }

    @Test
    @DisplayName("ZCZX-JYZGL-UT-003 负责人")
    void ut003_personInCharge()
    {
        assertThrows(ServiceException.class, () -> QuarantineStationValidation.validatePersonInCharge(""));
        assertThrows(ServiceException.class, () -> QuarantineStationValidation.validatePersonInCharge(longStr(11)));
        assertDoesNotThrow(() -> QuarantineStationValidation.validatePersonInCharge("张三"));
    }

    @Test
    @DisplayName("ZCZX-JYZGL-UT-004 手机号")
    void ut004_phone()
    {
        assertThrows(ServiceException.class, () -> QuarantineStationValidation.validateContactPhone("1234567890"));
        assertDoesNotThrow(() -> QuarantineStationValidation.validateContactPhone(VALID_PHONE));
        assertThrows(ServiceException.class, () -> QuarantineStationValidation.validateContactPhone("12345"));
    }

    @Test
    @DisplayName("ZCZX-JYZGL-UT-005 运营状态")
    void ut005_operateStatus()
    {
        assertEquals(QuarantineStationRules.OPERATE_STATUS_NORMAL,
                QuarantineStationValidation.resolveOperateStatus(null));
        assertDoesNotThrow(() -> QuarantineStationValidation.validateOperateStatus(1));
        assertDoesNotThrow(() -> QuarantineStationValidation.validateOperateStatus(3));
        assertThrows(ServiceException.class, () -> QuarantineStationValidation.validateOperateStatus(0));
        assertThrows(ServiceException.class, () -> QuarantineStationValidation.validateOperateStatus(4));
    }

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

    @Test
    @DisplayName("运营状态中文名")
    void operateStatusName()
    {
        assertEquals("正常", QuarantineStationRules.operateStatusName(1));
        assertEquals("停业", QuarantineStationRules.operateStatusName(2));
        assertEquals("注销", QuarantineStationRules.operateStatusName(3));
        assertTrue(QuarantineStationRules.operateStatusName(9).isEmpty());
    }

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

    private static BizQuarantineStation sampleRow()
    {
        BizQuarantineStation r = new BizQuarantineStation();
        r.setStationName("巴青东站");
        r.setDetailAddress("西藏那曲市巴青县某某路1号");
        r.setPersonInCharge("李四");
        r.setContactPhone(VALID_PHONE);
        r.setOperateStatus(QuarantineStationRules.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();
    }
}
