package cn.cordys.common.resolver.field;


import cn.cordys.common.util.JSON;
import cn.cordys.crm.system.dto.field.InputMultipleField;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Strings;

import java.util.List;

public class TextMultipleResolver extends AbstractModuleFieldResolver<InputMultipleField> {

    public static final String ARR_SQUARE = "[]";

    @Override
    public void validate(InputMultipleField inputMultipleField, Object value) {
        validateRequired(inputMultipleField, value);
        validateArray(inputMultipleField.getName(), value);
    }

    @Override
    public String convertToString(InputMultipleField inputMultipleField, Object value) {
        return getJsonString(value);
    }

    @Override
    public Object convertToValue(InputMultipleField inputMultipleField, String value) {
        return parse2Array(value);
    }

    @Override
	@SuppressWarnings("unchecked")
    public Object transformToValue(InputMultipleField inputMultipleField, String value) {
        if (StringUtils.isEmpty(value)) {
            return StringUtils.EMPTY;
        }
        return String.join(",", JSON.parseArray(value));
    }

    @Override
    public Object textToValue(InputMultipleField field, String text) {
        if (StringUtils.isBlank(text) || Strings.CS.equals(text, ARR_SQUARE)) {
            return null;
        }
        List<String> textList = parseFakeJsonArray(text);
        List<String> correctTags = getCorrectFormatInput(textList);
        return CollectionUtils.isEmpty(correctTags) ? null : correctTags;
    }

    /**
     * 要求标签最多10个, 且每个标签长度不超过64个字符
     * @param textList 标签列表
     * @return 符合要求的标签集合
     */
    public List<String> getCorrectFormatInput(List<String> textList) {
        return textList.stream()
                .map(String::trim)
                .filter(StringUtils::isNotBlank)
                .filter(text -> text.length() <= 64)
                .distinct()
                .limit(10)
                .toList();
    }
}
