/*
 *  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.Map;

import org.apache.commons.lang3.StringUtils;

import org.ametys.cms.contenttype.validation.AbstractContentValidator;
import org.ametys.cms.repository.Content;
import org.ametys.plugins.repository.data.holder.values.SynchronizableValue;
import org.ametys.runtime.i18n.I18nizableText;
import org.ametys.runtime.model.View;
import org.ametys.runtime.parameter.ValidationResult;

/**
 * Test global validator
 */
public class TestGlobalValidator extends AbstractContentValidator
{
    public ValidationResult validate(Content content)
    {
        return _validate(content.getTitle());
    }

    public ValidationResult validate(Content content, Map<String, Object> values, View view)
    {
        SynchronizableValue value = (SynchronizableValue) values.get("title");
        return _validate((String) value.getLocalValue());
    }
    
    private ValidationResult _validate(String value)
    {
        ValidationResult result = new ValidationResult();
        if (value != null)
        {
            String str = value.toString();
            
            if (StringUtils.containsIgnoreCase(str, "GLOB1"))
            {
                result.addError(new I18nizableText("Test validator - la valeur contient GLOB1"));
            }
            
            if (StringUtils.containsIgnoreCase(str, "GLOB2"))
            {
                result.addError(new I18nizableText("Test validator - la valeur contient GLOB2"));
            }
            
            if (StringUtils.containsIgnoreCase(str, "GLOB3"))
            {
                result.addWarning(new I18nizableText("Test validator - la valeur contient GLOB3"));
            }
            
            if (StringUtils.containsIgnoreCase(str, "GLOB4"))
            {
                result.addWarning(new I18nizableText("Test validator - la valeur contient GLOB4"));
            }
            
            if (StringUtils.containsIgnoreCase(str, "GLOB5"))
            {
                result.addInfo(new I18nizableText("Test validator - la valeur contient GLOB5"));
            }
        }
        
        return result;
    }
}
