/*
 * Copyright (c) Andrew Grant of Carrick Software 19/03/2025, 09:52. All rights reserved.
 *
 */

package scot.carricksoftware.grants.validators.places;

import org.springframework.stereotype.Component;
import org.springframework.validation.BindingResult;
import scot.carricksoftware.grants.commands.places.regions.RegionCommand;
import scot.carricksoftware.grants.constants.ApplicationConstants;
import scot.carricksoftware.grants.constants.ValidationConstants;

@Component
public class RegionCommandValidator {

    public void validate(RegionCommand regionCommand, BindingResult bindingResult) {
        validateName(regionCommand, bindingResult);
        validateCountry(regionCommand, bindingResult);
    }

    private void validateCountry(RegionCommand regionCommand, BindingResult bindingResult) {
        if (regionCommand.getCountry() == null) {
            bindingResult.rejectValue("country", ApplicationConstants.EMPTY_STRING,
                    null,
                    ValidationConstants.COUNTRY_IS_NULL);
        }
    }

    public void validateName(RegionCommand regionCommand, BindingResult bindingResult) {
        if (regionCommand.getName().length() < ApplicationConstants.MINIMUM_NAME_LENGTH) {
            bindingResult.rejectValue("name", ApplicationConstants.EMPTY_STRING,
                    null,
                    ValidationConstants.NAME_IS_TOO_SHORT);
        } else {
            if (regionCommand.getName().length() > ApplicationConstants.MAXIMUM_NAME_LENGTH) {
                bindingResult.rejectValue("name", ApplicationConstants.EMPTY_STRING,
                        null,
                        ValidationConstants.NAME_IS_TOO_LONG);
            }
        }
    }
}

