/*
 *  Copyright 2024 Anyware Services
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.ametys.web;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import org.ametys.cms.data.RichText;
import org.ametys.runtime.i18n.I18nizableText;
import org.ametys.runtime.parameter.ValidationResult;
import org.ametys.runtime.parameter.Validator;

/**
 * Implementation of {@link Validator} for tests purposes
 * Validation result will contain
 * <ol>
 * <li>An error if the given value contains "ERR" char sequence</li>
 * <li>A warning if the given value contains "WARN" char sequence</li>
 * <li>An info if the given value contains "INFO" char sequence</li>
 * </ol>
 * This {@link Validator} only works with single string data
 */
public class TestStringValidator implements Validator
{
    
    public ValidationResult validate(Object value)
    {
        ValidationResult result = new ValidationResult();
        
        if (value instanceof RichText)
        {
            result.addWarning(new I18nizableText("Test validator - Alerte généralisée sur les rich texts"));
        }
        else if (value != null)
        {
            String str = value.toString();
            
            if (StringUtils.containsIgnoreCase(str, "ERR"))
            {
                result.addError(new I18nizableText("Test validator - la valeur contient ERR"));
            }
            
            if (StringUtils.containsIgnoreCase(str, "RATE"))
            {
                result.addError(new I18nizableText("Test validator - la valeur contient RATE"));
            }
            
            if (StringUtils.containsIgnoreCase(str, "WARN"))
            {
                result.addWarning(new I18nizableText("Test validator - la valeur contient WARN"));
            }
            
            if (StringUtils.containsIgnoreCase(str, "TWO"))
            {
                result.addWarning(new I18nizableText("Test validator - la valeur contient TWO"));
            }
            
            if (StringUtils.containsIgnoreCase(str, "INFO"))
            {
                result.addInfo(new I18nizableText("Test validator - la valeur contient INFO"));
            }
        }
        
        return result;
    }

    public Map<String, Object> getConfiguration()
    {
        Map<String, Object> confg = new HashMap<>();
        confg.put("mandatory", true);
        return confg;
    }

}
