
package io.github.jeddict.orm.generator.compiler.constraints;

import io.github.jeddict.bv.constraints.Email;
import io.github.jeddict.bv.constraints.Flag;
import static io.github.jeddict.jcode.BeanVaildationConstants.BV_CONSTRAINTS_PACKAGE;
import io.github.jeddict.orm.generator.compiler.InvalidDataException;
import static io.github.jeddict.orm.generator.util.ORMConverterUtil.AT;
import static io.github.jeddict.orm.generator.util.ORMConverterUtil.CLOSE_BRACES;
import static io.github.jeddict.orm.generator.util.ORMConverterUtil.CLOSE_PARANTHESES;
import static io.github.jeddict.orm.generator.util.ORMConverterUtil.COMMA;
import static io.github.jeddict.orm.generator.util.ORMConverterUtil.OPEN_BRACES;
import static io.github.jeddict.orm.generator.util.ORMConverterUtil.OPEN_PARANTHESES;
import static io.github.jeddict.orm.generator.util.ORMConverterUtil.QUOTE;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
import static io.github.jeddict.util.StringUtils.isBlank;
import static io.github.jeddict.util.StringUtils.isNotBlank;

/**
 *
 * @author Gaurav Gupta
 */
public class EmailSnippet extends ConstraintSnippet<Email> {

    private List<Flag> flags = Collections.<Flag>emptyList();

    public EmailSnippet(Email email) {
        super(email);
        if (isNotBlank(constraint.getFlags())) {
            flags = Arrays.stream(constraint.getFlags().split(","))
                    .map(flag -> Flag.fromValue(flag.trim()))
                    .filter(flag -> flag != null)
                    .collect(toList());
        }
    }

    @Override
    protected String getAPI() {
        return "Email";
    }

    @Override
    public String getSnippet() throws InvalidDataException {
        StringBuilder builder = new StringBuilder(AT);
        builder.append(getAPI());

        if (isBlank(constraint.getMessage())
                && isBlank(constraint.getRegexp())
                && flags.isEmpty()) {
            return builder.toString();
        }

        builder.append(OPEN_PARANTHESES);

        if (isNotBlank(constraint.getRegexp())) {
            builder.append("regexp=")
                    .append(QUOTE)
                    .append(constraint.getRegexp())
                    .append(QUOTE)
                    .append(COMMA);
        }

        if (!flags.isEmpty()) {
            builder.append("flags=")
                    .append(OPEN_BRACES)
                    .append(flags.stream().map(Flag::name).collect(joining(", ")))
                    .append(CLOSE_BRACES)
                    .append(COMMA);
        }

        if (isNotBlank(constraint.getMessage())) {
            builder.append("message=")
                    .append(QUOTE)
                    .append(constraint.getMessage())
                    .append(QUOTE)
                    .append(COMMA);
        }

        return builder.substring(0, builder.length() - 1) + CLOSE_PARANTHESES;
    }

    @Override
    public Collection<String> getImportSnippets() throws InvalidDataException {
        Collection<String> imports = super.getImportSnippets();
        imports.addAll(flags.stream()
                        .map(Flag::name)
                        .map(flag -> "static " + BV_CONSTRAINTS_PACKAGE + ".Email.Flag." + flag)
                        .collect(toList())
        );
        return imports;
    }

}
