
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.PostTaxMessageProcessor;
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 PostTaxDefinitionParser
    implements BeanDefinitionParser
{

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

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

    public BeanDefinition parse(Element element, ParserContext parserContent) {
        BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(PostTaxMessageProcessor.class.getName());
        String configRef = element.getAttribute("config-ref");
        if ((configRef!= null)&&(!StringUtils.isBlank(configRef))) {
            builder.addPropertyValue("moduleObject", configRef);
        }
        if ((element.getAttribute("docId")!= null)&&(!StringUtils.isBlank(element.getAttribute("docId")))) {
            builder.addPropertyValue("docId", element.getAttribute("docId"));
        }
        if ((element.getAttribute("companyCode")!= null)&&(!StringUtils.isBlank(element.getAttribute("companyCode")))) {
            builder.addPropertyValue("companyCode", element.getAttribute("companyCode"));
        }
        if (element.hasAttribute("docType")) {
            builder.addPropertyValue("docType", element.getAttribute("docType"));
        }
        if ((element.getAttribute("docCode")!= null)&&(!StringUtils.isBlank(element.getAttribute("docCode")))) {
            builder.addPropertyValue("docCode", element.getAttribute("docCode"));
        }
        if ((element.getAttribute("docDate")!= null)&&(!StringUtils.isBlank(element.getAttribute("docDate")))) {
            builder.addPropertyValue("docDate", element.getAttribute("docDate"));
        }
        if ((element.getAttribute("totalAmount")!= null)&&(!StringUtils.isBlank(element.getAttribute("totalAmount")))) {
            builder.addPropertyValue("totalAmount", element.getAttribute("totalAmount"));
        }
        if ((element.getAttribute("totalTax")!= null)&&(!StringUtils.isBlank(element.getAttribute("totalTax")))) {
            builder.addPropertyValue("totalTax", element.getAttribute("totalTax"));
        }
        if ((element.getAttribute("commit")!= null)&&(!StringUtils.isBlank(element.getAttribute("commit")))) {
            builder.addPropertyValue("commit", element.getAttribute("commit"));
        }
        if ((element.getAttribute("newDocCode")!= null)&&(!StringUtils.isBlank(element.getAttribute("newDocCode")))) {
            builder.addPropertyValue("newDocCode", element.getAttribute("newDocCode"));
        }
        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;
        }
    }

}
