
package org.mule.module.paypal.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.module.paypal.config.MassPayMessageProcessor;
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.config.RuntimeBeanReference;
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.springframework.util.xml.DomUtils;
import org.w3c.dom.Element;

public class MassPayDefinitionParser
    implements BeanDefinitionParser
{

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

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

    public BeanDefinition parse(Element element, ParserContext parserContent) {
        BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(MassPayMessageProcessor.class.getName());
        String configRef = element.getAttribute("config-ref");
        if ((configRef!= null)&&(!StringUtils.isBlank(configRef))) {
            builder.addPropertyValue("moduleObject", configRef);
        }
        if ((element.getAttribute("emailSubject")!= null)&&(!StringUtils.isBlank(element.getAttribute("emailSubject")))) {
            builder.addPropertyValue("emailSubject", element.getAttribute("emailSubject"));
        }
        Element massPayItemsListElement = null;
        massPayItemsListElement = DomUtils.getChildElementByTagName(element, "mass-pay-items");
        List<Element> massPayItemsListChilds = null;
        if (massPayItemsListElement!= null) {
            String massPayItemsRef = massPayItemsListElement.getAttribute("ref");
            if ((massPayItemsRef!= null)&&(!StringUtils.isBlank(massPayItemsRef))) {
                if ((!massPayItemsRef.startsWith(patternInfo.getPrefix()))&&(!massPayItemsRef.endsWith(patternInfo.getSuffix()))) {
                    builder.addPropertyValue("massPayItems", new RuntimeBeanReference(massPayItemsRef));
                } else {
                    builder.addPropertyValue("massPayItems", massPayItemsRef);
                }
            } else {
                ManagedList massPayItems = new ManagedList();
                massPayItemsListChilds = DomUtils.getChildElementsByTagName(massPayItemsListElement, "mass-pay-item");
                if (massPayItemsListChilds!= null) {
                    for (Element massPayItemsChild: massPayItemsListChilds) {
                        String valueRef = massPayItemsChild.getAttribute("value-ref");
                        if ((valueRef!= null)&&(!StringUtils.isBlank(valueRef))) {
                            massPayItems.add(new RuntimeBeanReference(valueRef));
                        } else {
                            massPayItems.add(massPayItemsChild.getTextContent());
                        }
                    }
                }
                builder.addPropertyValue("massPayItems", massPayItems);
            }
        }
        if (element.hasAttribute("receiverType")) {
            builder.addPropertyValue("receiverType", element.getAttribute("receiverType"));
        }
        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;
        }
    }

}
