package info.bukova.isspst.validators;

import info.bukova.isspst.StringUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zkoss.bind.Property;
import org.zkoss.bind.ValidationContext;

public class UserFormValidator extends BaseValidator
{
	private final static Logger log = LoggerFactory.getLogger(UserFormValidator.class.getName());

	@Override
	protected Logger getLogger()
	{
		return log;
	}

	private boolean recordIsNew;

	public UserFormValidator(boolean recordIsNew)
	{
		this.recordIsNew = recordIsNew;
	}

	@Override
	public void validate(ValidationContext ctx)
	{
		Property propertyOrig = ctx.getProperties("password")[0];
		String passwordOrig = (String) propertyOrig.getValue();
		
		if (passwordOrig == null)
		{
			passwordOrig = "";
		}

		if (passwordOrig.isEmpty())
		{
			if (this.recordIsNew)
			{
				this.errorMsg(ctx, StringUtils.localize("UserPasswordIsEmpty"), "idUserPasswordOriginal");
				return;
			}
		}

		Property propertyDupl = ctx.getProperties("retPasswd")[0];
		String passwordDupl = (String) propertyDupl.getValue();

		if (passwordDupl == null)
		{
			passwordDupl = "";
		}

		if (!passwordOrig.equals(passwordDupl))
		{
			this.errorMsg(ctx, StringUtils.localize("UserPasswordsAreNotSame"), "idUserPasswordDuplicate");
			return;
		}
	}
}
