
package org.mule.module.magento.config;

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.magento.processors.GetProductMessageProcessor;
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 GetProductDefinitionParser
    implements BeanDefinitionParser
{

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

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

    public BeanDefinition parse(Element element, ParserContext parserContent) {
        BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(GetProductMessageProcessor.class.getName());
        String configRef = element.getAttribute("config-ref");
        if ((configRef!= null)&&(!StringUtils.isBlank(configRef))) {
            builder.addPropertyValue("moduleObject", configRef);
        }
        if ((element.getAttribute("productId")!= null)&&(!StringUtils.isBlank(element.getAttribute("productId")))) {
            builder.addPropertyValue("productId", element.getAttribute("productId"));
        }
        if ((element.getAttribute("productSku")!= null)&&(!StringUtils.isBlank(element.getAttribute("productSku")))) {
            builder.addPropertyValue("productSku", element.getAttribute("productSku"));
        }
        if ((element.getAttribute("productIdOrSku")!= null)&&(!StringUtils.isBlank(element.getAttribute("productIdOrSku")))) {
            builder.addPropertyValue("productIdOrSku", element.getAttribute("productIdOrSku"));
        }
        if ((element.getAttribute("storeViewIdOrCode")!= null)&&(!StringUtils.isBlank(element.getAttribute("storeViewIdOrCode")))) {
            builder.addPropertyValue("storeViewIdOrCode", element.getAttribute("storeViewIdOrCode"));
        }
        Element attributesNamesListElement = null;
        attributesNamesListElement = DomUtils.getChildElementByTagName(element, "attributes-names");
        List<Element> attributesNamesListChilds = null;
        if (attributesNamesListElement!= null) {
            String attributesNamesRef = attributesNamesListElement.getAttribute("ref");
            if ((attributesNamesRef!= null)&&(!StringUtils.isBlank(attributesNamesRef))) {
                if ((!attributesNamesRef.startsWith(patternInfo.getPrefix()))&&(!attributesNamesRef.endsWith(patternInfo.getSuffix()))) {
                    builder.addPropertyValue("attributesNames", new RuntimeBeanReference(attributesNamesRef));
                } else {
                    builder.addPropertyValue("attributesNames", attributesNamesRef);
                }
            } else {
                ManagedList attributesNames = new ManagedList();
                attributesNamesListChilds = DomUtils.getChildElementsByTagName(attributesNamesListElement, "attributes-name");
                if (attributesNamesListChilds!= null) {
                    for (Element attributesNamesChild: attributesNamesListChilds) {
                        String valueRef = attributesNamesChild.getAttribute("value-ref");
                        if ((valueRef!= null)&&(!StringUtils.isBlank(valueRef))) {
                            if ((!valueRef.startsWith(patternInfo.getPrefix()))&&(!valueRef.endsWith(patternInfo.getSuffix()))) {
                                attributesNames.add(new RuntimeBeanReference(valueRef));
                            } else {
                                attributesNames.add(valueRef);
                            }
                        } else {
                            attributesNames.add(attributesNamesChild.getTextContent());
                        }
                    }
                }
                builder.addPropertyValue("attributesNames", attributesNames);
            }
        }
        Element additionalAttributeNamesListElement = null;
        additionalAttributeNamesListElement = DomUtils.getChildElementByTagName(element, "additional-attribute-names");
        List<Element> additionalAttributeNamesListChilds = null;
        if (additionalAttributeNamesListElement!= null) {
            String additionalAttributeNamesRef = additionalAttributeNamesListElement.getAttribute("ref");
            if ((additionalAttributeNamesRef!= null)&&(!StringUtils.isBlank(additionalAttributeNamesRef))) {
                if ((!additionalAttributeNamesRef.startsWith(patternInfo.getPrefix()))&&(!additionalAttributeNamesRef.endsWith(patternInfo.getSuffix()))) {
                    builder.addPropertyValue("additionalAttributeNames", new RuntimeBeanReference(additionalAttributeNamesRef));
                } else {
                    builder.addPropertyValue("additionalAttributeNames", additionalAttributeNamesRef);
                }
            } else {
                ManagedList additionalAttributeNames = new ManagedList();
                additionalAttributeNamesListChilds = DomUtils.getChildElementsByTagName(additionalAttributeNamesListElement, "additional-attribute-name");
                if (additionalAttributeNamesListChilds!= null) {
                    for (Element additionalAttributeNamesChild: additionalAttributeNamesListChilds) {
                        String valueRef = additionalAttributeNamesChild.getAttribute("value-ref");
                        if ((valueRef!= null)&&(!StringUtils.isBlank(valueRef))) {
                            if ((!valueRef.startsWith(patternInfo.getPrefix()))&&(!valueRef.endsWith(patternInfo.getSuffix()))) {
                                additionalAttributeNames.add(new RuntimeBeanReference(valueRef));
                            } else {
                                additionalAttributeNames.add(valueRef);
                            }
                        } else {
                            additionalAttributeNames.add(additionalAttributeNamesChild.getTextContent());
                        }
                    }
                }
                builder.addPropertyValue("additionalAttributeNames", additionalAttributeNames);
            }
        }
        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 {
            if (parserContent.getContainingBeanDefinition().getBeanClassName().equals("org.mule.enricher.MessageEnricher")) {
                propertyValues.addPropertyValue("enrichmentMessageProcessor", 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;
        }
    }

}
