
package org.mule.modules.avalara.config.spring;

import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.mule.config.spring.MuleHierarchicalBeanDefinitionParserDelegate;
import org.mule.config.spring.util.SpringXMLUtils;
import org.mule.modules.avalara.config.ValidateAddressMessageProcessor;
import org.mule.util.TemplateParser;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.PropertyValue;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.ManagedList;
import org.springframework.beans.factory.xml.BeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.w3c.dom.Element;

public class ValidateAddressDefinitionParser
    implements BeanDefinitionParser
{

    /**
     * Mule Pattern Info
     * 
     */
    private TemplateParser.PatternInfo patternInfo;

    public ValidateAddressDefinitionParser() {
        patternInfo = TemplateParser.createMuleStyleParser().getStyle();
    }

    public BeanDefinition parse(Element element, ParserContext parserContent) {
        BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(ValidateAddressMessageProcessor.class.getName());
        String configRef = element.getAttribute("config-ref");
        if ((configRef!= null)&&(!StringUtils.isBlank(configRef))) {
            builder.addPropertyValue("moduleObject", configRef);
        }
        if ((element.getAttribute("line1")!= null)&&(!StringUtils.isBlank(element.getAttribute("line1")))) {
            builder.addPropertyValue("line1", element.getAttribute("line1"));
        }
        if ((element.getAttribute("line2")!= null)&&(!StringUtils.isBlank(element.getAttribute("line2")))) {
            builder.addPropertyValue("line2", element.getAttribute("line2"));
        }
        if ((element.getAttribute("line3")!= null)&&(!StringUtils.isBlank(element.getAttribute("line3")))) {
            builder.addPropertyValue("line3", element.getAttribute("line3"));
        }
        if ((element.getAttribute("city")!= null)&&(!StringUtils.isBlank(element.getAttribute("city")))) {
            builder.addPropertyValue("city", element.getAttribute("city"));
        }
        if ((element.getAttribute("region")!= null)&&(!StringUtils.isBlank(element.getAttribute("region")))) {
            builder.addPropertyValue("region", element.getAttribute("region"));
        }
        if ((element.getAttribute("country")!= null)&&(!StringUtils.isBlank(element.getAttribute("country")))) {
            builder.addPropertyValue("country", element.getAttribute("country"));
        }
        if ((element.getAttribute("postalCode")!= null)&&(!StringUtils.isBlank(element.getAttribute("postalCode")))) {
            builder.addPropertyValue("postalCode", element.getAttribute("postalCode"));
        }
        if ((element.getAttribute("addressCode")!= null)&&(!StringUtils.isBlank(element.getAttribute("addressCode")))) {
            builder.addPropertyValue("addressCode", element.getAttribute("addressCode"));
        }
        if ((element.getAttribute("taxRegionId")!= null)&&(!StringUtils.isBlank(element.getAttribute("taxRegionId")))) {
            builder.addPropertyValue("taxRegionId", element.getAttribute("taxRegionId"));
        }
        if ((element.getAttribute("latitude")!= null)&&(!StringUtils.isBlank(element.getAttribute("latitude")))) {
            builder.addPropertyValue("latitude", element.getAttribute("latitude"));
        }
        if ((element.getAttribute("longitude")!= null)&&(!StringUtils.isBlank(element.getAttribute("longitude")))) {
            builder.addPropertyValue("longitude", element.getAttribute("longitude"));
        }
        if (element.hasAttribute("textCase")) {
            builder.addPropertyValue("textCase", element.getAttribute("textCase"));
        }
        if ((element.getAttribute("coordinates")!= null)&&(!StringUtils.isBlank(element.getAttribute("coordinates")))) {
            builder.addPropertyValue("coordinates", element.getAttribute("coordinates"));
        }
        if ((element.getAttribute("taxability")!= null)&&(!StringUtils.isBlank(element.getAttribute("taxability")))) {
            builder.addPropertyValue("taxability", element.getAttribute("taxability"));
        }
        if ((element.getAttribute("date")!= null)&&(!StringUtils.isBlank(element.getAttribute("date")))) {
            builder.addPropertyValue("date", element.getAttribute("date"));
        }
        BeanDefinition definition = builder.getBeanDefinition();
        definition.setAttribute(MuleHierarchicalBeanDefinitionParserDelegate.MULE_NO_RECURSE, Boolean.TRUE);
        MutablePropertyValues propertyValues = parserContent.getContainingBeanDefinition().getPropertyValues();
        if (parserContent.getContainingBeanDefinition().getBeanClassName().equals("org.mule.config.spring.factories.PollingMessageSourceFactoryBean")) {
            propertyValues.addPropertyValue("messageProcessor", definition);
        } else {
            PropertyValue messageProcessors = propertyValues.getPropertyValue("messageProcessors");
            if ((messageProcessors == null)||(messageProcessors.getValue() == null)) {
                propertyValues.addPropertyValue("messageProcessors", new ManagedList());
            }
            List listMessageProcessors = ((List) propertyValues.getPropertyValue("messageProcessors").getValue());
            listMessageProcessors.add(definition);
        }
        return definition;
    }

    protected String getAttributeValue(Element element, String attributeName) {
        if (!StringUtils.isEmpty(element.getAttribute(attributeName))) {
            return element.getAttribute(attributeName);
        }
        return null;
    }

    private String generateChildBeanName(Element element) {
        String id = SpringXMLUtils.getNameOrId(element);
        if (StringUtils.isBlank(id)) {
            String parentId = SpringXMLUtils.getNameOrId(((Element) element.getParentNode()));
            return ((("."+ parentId)+":")+ element.getLocalName());
        } else {
            return id;
        }
    }

}
