/*
 * Copyright (c) 2004-2017, by Alex Leigh.
 * All rights reserved.
 *
 * THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE
 * The copyright notice above does not evidence any
 * actual or intended publication of such source code.
 */

package leigh.eo.schema.dao;

import leigh.eo.EO;
import leigh.eo.EODataType;

/**
 * Utility class for interacting with field elements.
 *
 * @author Alex Leigh
 */
public final class FieldUtil {
    public static String API_NAME = FieldElementDAO.API_TYPE;
    public static String KEY_API_NAME = "apiName";
    public static String KEY_LABEL = "label";
    public static String KEY_TYPE = "type";
    public static String KEY_FIXED_CHOICES = "fixedChoices";

    public static EO help(EO field, String help) {
        FieldElementDAO.setHelp(field, help);
        return field;
    }

    public static EO create(final String apiName, final String label) {
        return create(apiName, label, EODataType.string);
    }

    public static EO createNotNull(final String apiName, final String label) {
        return createNotNull(apiName, label, EODataType.string);
    }

    public static EO createNotNull(final String apiName, final String label, final EODataType type) {
        final EO field = create(apiName, label);
        FieldElementDAO.getValidators(field).add(ValidatorNotEmptyElementDAO.create());
        FieldElementDAO.setType(field, type.toString());
        return field;
    }


    public static EO create(final String apiName, final String label, final EODataType type) {
        final EO field = FieldElementDAO.create();
        FieldElementDAO.setApiName(field, apiName);
        FieldElementDAO.appendLabel(field, TextUtil.defaultLabel(label));
        FieldElementDAO.setType(field, type.toString());

        return field;
    }

    public static EO createCaseInsensitive(final String apiName, final String label) {
        final EO field = create(apiName, label);
        FieldElementDAO.setIsCaseInsensitive(field, true);
        return field;
    }


    public static EO createProtectedCaseInsensitive(final String apiName, final String label) {
        final EO field = create(apiName, label);
        FieldElementDAO.setIsCaseInsensitive(field, true);
        FieldElementDAO.setIsProtected(field, true);
        return field;
    }

    public static EO createProtectedCaseInsensitiveNotNull(final String apiName, final String label) {
        final EO field = createNotNull(apiName, label);
        FieldElementDAO.setIsCaseInsensitive(field, true);
        FieldElementDAO.setIsProtected(field, true);
        return field;
    }


    public static EO createNotNullCaseInsensitive(final String apiName, final String label) {
        final EO field = createNotNull(apiName, label);
        FieldElementDAO.setIsCaseInsensitive(field, true);
        return field;
    }

    /**
     * Given a field element, returns the Java Class as denoted by <code>EODataType</code>
     * for the field. This is useful if you need to know what sort of a class the field
     * is expected to provide.
     *
     * @param field
     * @return
     */
    public static Class getTypeClass(final EO field) {
        return EODataType.getType(EODataType.valueOf(FieldElementDAO.getType(field)));
    }

    public static EO addNotNullValidator(final EO field) {
        FieldElementDAO.getValidators(field).add(ValidatorNotEmptyElementDAO.create());
        return field;
    }
}