
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.MoveShoppingCartProductToCustomerQuoteMessageProcessor;
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.support.ManagedMap;
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 MoveShoppingCartProductToCustomerQuoteDefinitionParser
    implements BeanDefinitionParser
{

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

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

    public BeanDefinition parse(Element element, ParserContext parserContent) {
        BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(MoveShoppingCartProductToCustomerQuoteMessageProcessor.class.getName());
        String configRef = element.getAttribute("config-ref");
        if ((configRef!= null)&&(!StringUtils.isBlank(configRef))) {
            builder.addPropertyValue("moduleObject", configRef);
        }
        if ((element.getAttribute("quoteId")!= null)&&(!StringUtils.isBlank(element.getAttribute("quoteId")))) {
            builder.addPropertyValue("quoteId", element.getAttribute("quoteId"));
        }
        Element productsAttributesListElement = null;
        productsAttributesListElement = DomUtils.getChildElementByTagName(element, "products-attributes");
        List<Element> productsAttributesListChilds = null;
        if (productsAttributesListElement!= null) {
            String productsAttributesRef = productsAttributesListElement.getAttribute("ref");
            if ((productsAttributesRef!= null)&&(!StringUtils.isBlank(productsAttributesRef))) {
                if ((!productsAttributesRef.startsWith(patternInfo.getPrefix()))&&(!productsAttributesRef.endsWith(patternInfo.getSuffix()))) {
                    builder.addPropertyValue("productsAttributes", new RuntimeBeanReference(productsAttributesRef));
                } else {
                    builder.addPropertyValue("productsAttributes", productsAttributesRef);
                }
            } else {
                ManagedList productsAttributes = new ManagedList();
                productsAttributesListChilds = DomUtils.getChildElementsByTagName(productsAttributesListElement, "products-attribute");
                if (productsAttributesListChilds!= null) {
                    for (Element productsAttributesChild: productsAttributesListChilds) {
                        String valueRef = productsAttributesChild.getAttribute("value-ref");
                        if ((valueRef!= null)&&(!StringUtils.isBlank(valueRef))) {
                            if ((!valueRef.startsWith(patternInfo.getPrefix()))&&(!valueRef.endsWith(patternInfo.getSuffix()))) {
                                productsAttributes.add(new RuntimeBeanReference(valueRef));
                            } else {
                                productsAttributes.add(valueRef);
                            }
                        } else {
                            List<Element> innerproductsattributeMapChilds = null;
                            if (productsAttributesChild!= null) {
                                String innerproductsattributeRef = productsAttributesChild.getAttribute("ref");
                                if ((innerproductsattributeRef!= null)&&(!StringUtils.isBlank(innerproductsattributeRef))) {
                                    if ((!innerproductsattributeRef.startsWith(patternInfo.getPrefix()))&&(!innerproductsattributeRef.endsWith(patternInfo.getSuffix()))) {
                                        builder.addPropertyValue("inner-products-attribute", new RuntimeBeanReference(innerproductsattributeRef));
                                    } else {
                                        builder.addPropertyValue("inner-products-attribute", innerproductsattributeRef);
                                    }
                                } else {
                                    ManagedMap innerproductsattribute = new ManagedMap();
                                    innerproductsattributeMapChilds = DomUtils.getChildElementsByTagName(productsAttributesChild, "inner-products-attribute");
                                    if (innerproductsattributeMapChilds!= null) {
                                        if (innerproductsattributeMapChilds.size() == 0) {
                                            innerproductsattributeMapChilds = DomUtils.getChildElements(productsAttributesChild);
                                        }
                                        for (Element innerproductsattributeChild: innerproductsattributeMapChilds) {
                                            String innerproductsattributeValueRef = innerproductsattributeChild.getAttribute("value-ref");
                                            String innerproductsattributeKeyRef = innerproductsattributeChild.getAttribute("key-ref");
                                            Object valueObject = null;
                                            Object keyObject = null;
                                            if ((innerproductsattributeValueRef!= null)&&(!StringUtils.isBlank(innerproductsattributeValueRef))) {
                                                if ((!innerproductsattributeValueRef.startsWith(patternInfo.getPrefix()))&&(!innerproductsattributeValueRef.endsWith(patternInfo.getSuffix()))) {
                                                    valueObject = new RuntimeBeanReference(innerproductsattributeValueRef);
                                                } else {
                                                    valueObject = innerproductsattributeValueRef;
                                                }
                                            } else {
                                                valueObject = innerproductsattributeChild.getTextContent();
                                            }
                                            if ((innerproductsattributeKeyRef!= null)&&(!StringUtils.isBlank(innerproductsattributeKeyRef))) {
                                                keyObject = new RuntimeBeanReference(innerproductsattributeKeyRef);
                                            } else {
                                                keyObject = innerproductsattributeChild.getAttribute("key");
                                            }
                                            if ((keyObject == null)||((keyObject instanceof String)&&StringUtils.isBlank(((String) keyObject)))) {
                                                keyObject = innerproductsattributeChild.getTagName();
                                            }
                                            innerproductsattribute.put(keyObject, valueObject);
                                        }
                                    }
                                    productsAttributes.add(innerproductsattribute);
                                }
                            }
                        }
                    }
                }
                builder.addPropertyValue("productsAttributes", productsAttributes);
            }
        }
        Element productsOptionsListElement = null;
        productsOptionsListElement = DomUtils.getChildElementByTagName(element, "products-options");
        List<Element> productsOptionsListChilds = null;
        if (productsOptionsListElement!= null) {
            String productsOptionsRef = productsOptionsListElement.getAttribute("ref");
            if ((productsOptionsRef!= null)&&(!StringUtils.isBlank(productsOptionsRef))) {
                if ((!productsOptionsRef.startsWith(patternInfo.getPrefix()))&&(!productsOptionsRef.endsWith(patternInfo.getSuffix()))) {
                    builder.addPropertyValue("productsOptions", new RuntimeBeanReference(productsOptionsRef));
                } else {
                    builder.addPropertyValue("productsOptions", productsOptionsRef);
                }
            } else {
                ManagedList productsOptions = new ManagedList();
                productsOptionsListChilds = DomUtils.getChildElementsByTagName(productsOptionsListElement, "products-option");
                if (productsOptionsListChilds!= null) {
                    for (Element productsOptionsChild: productsOptionsListChilds) {
                        String valueRef = productsOptionsChild.getAttribute("value-ref");
                        if ((valueRef!= null)&&(!StringUtils.isBlank(valueRef))) {
                            if ((!valueRef.startsWith(patternInfo.getPrefix()))&&(!valueRef.endsWith(patternInfo.getSuffix()))) {
                                productsOptions.add(new RuntimeBeanReference(valueRef));
                            } else {
                                productsOptions.add(valueRef);
                            }
                        } else {
                            List<Element> innerproductsoptionMapChilds = null;
                            if (productsOptionsChild!= null) {
                                String innerproductsoptionRef = productsOptionsChild.getAttribute("ref");
                                if ((innerproductsoptionRef!= null)&&(!StringUtils.isBlank(innerproductsoptionRef))) {
                                    if ((!innerproductsoptionRef.startsWith(patternInfo.getPrefix()))&&(!innerproductsoptionRef.endsWith(patternInfo.getSuffix()))) {
                                        builder.addPropertyValue("inner-products-option", new RuntimeBeanReference(innerproductsoptionRef));
                                    } else {
                                        builder.addPropertyValue("inner-products-option", innerproductsoptionRef);
                                    }
                                } else {
                                    ManagedMap innerproductsoption = new ManagedMap();
                                    innerproductsoptionMapChilds = DomUtils.getChildElementsByTagName(productsOptionsChild, "inner-products-option");
                                    if (innerproductsoptionMapChilds!= null) {
                                        if (innerproductsoptionMapChilds.size() == 0) {
                                            innerproductsoptionMapChilds = DomUtils.getChildElements(productsOptionsChild);
                                        }
                                        for (Element innerproductsoptionChild: innerproductsoptionMapChilds) {
                                            String innerproductsoptionValueRef = innerproductsoptionChild.getAttribute("value-ref");
                                            String innerproductsoptionKeyRef = innerproductsoptionChild.getAttribute("key-ref");
                                            Object valueObject = null;
                                            Object keyObject = null;
                                            if ((innerproductsoptionValueRef!= null)&&(!StringUtils.isBlank(innerproductsoptionValueRef))) {
                                                if ((!innerproductsoptionValueRef.startsWith(patternInfo.getPrefix()))&&(!innerproductsoptionValueRef.endsWith(patternInfo.getSuffix()))) {
                                                    valueObject = new RuntimeBeanReference(innerproductsoptionValueRef);
                                                } else {
                                                    valueObject = innerproductsoptionValueRef;
                                                }
                                            } else {
                                                valueObject = innerproductsoptionChild.getTextContent();
                                            }
                                            if ((innerproductsoptionKeyRef!= null)&&(!StringUtils.isBlank(innerproductsoptionKeyRef))) {
                                                keyObject = new RuntimeBeanReference(innerproductsoptionKeyRef);
                                            } else {
                                                keyObject = innerproductsoptionChild.getAttribute("key");
                                            }
                                            if ((keyObject == null)||((keyObject instanceof String)&&StringUtils.isBlank(((String) keyObject)))) {
                                                keyObject = innerproductsoptionChild.getTagName();
                                            }
                                            innerproductsoption.put(keyObject, valueObject);
                                        }
                                    }
                                    productsOptions.add(innerproductsoption);
                                }
                            }
                        }
                    }
                }
                builder.addPropertyValue("productsOptions", productsOptions);
            }
        }
        Element productsBundleOptionsListElement = null;
        productsBundleOptionsListElement = DomUtils.getChildElementByTagName(element, "products-bundle-options");
        List<Element> productsBundleOptionsListChilds = null;
        if (productsBundleOptionsListElement!= null) {
            String productsBundleOptionsRef = productsBundleOptionsListElement.getAttribute("ref");
            if ((productsBundleOptionsRef!= null)&&(!StringUtils.isBlank(productsBundleOptionsRef))) {
                if ((!productsBundleOptionsRef.startsWith(patternInfo.getPrefix()))&&(!productsBundleOptionsRef.endsWith(patternInfo.getSuffix()))) {
                    builder.addPropertyValue("productsBundleOptions", new RuntimeBeanReference(productsBundleOptionsRef));
                } else {
                    builder.addPropertyValue("productsBundleOptions", productsBundleOptionsRef);
                }
            } else {
                ManagedList productsBundleOptions = new ManagedList();
                productsBundleOptionsListChilds = DomUtils.getChildElementsByTagName(productsBundleOptionsListElement, "products-bundle-option");
                if (productsBundleOptionsListChilds!= null) {
                    for (Element productsBundleOptionsChild: productsBundleOptionsListChilds) {
                        String valueRef = productsBundleOptionsChild.getAttribute("value-ref");
                        if ((valueRef!= null)&&(!StringUtils.isBlank(valueRef))) {
                            if ((!valueRef.startsWith(patternInfo.getPrefix()))&&(!valueRef.endsWith(patternInfo.getSuffix()))) {
                                productsBundleOptions.add(new RuntimeBeanReference(valueRef));
                            } else {
                                productsBundleOptions.add(valueRef);
                            }
                        } else {
                            List<Element> innerproductsbundleoptionMapChilds = null;
                            if (productsBundleOptionsChild!= null) {
                                String innerproductsbundleoptionRef = productsBundleOptionsChild.getAttribute("ref");
                                if ((innerproductsbundleoptionRef!= null)&&(!StringUtils.isBlank(innerproductsbundleoptionRef))) {
                                    if ((!innerproductsbundleoptionRef.startsWith(patternInfo.getPrefix()))&&(!innerproductsbundleoptionRef.endsWith(patternInfo.getSuffix()))) {
                                        builder.addPropertyValue("inner-products-bundle-option", new RuntimeBeanReference(innerproductsbundleoptionRef));
                                    } else {
                                        builder.addPropertyValue("inner-products-bundle-option", innerproductsbundleoptionRef);
                                    }
                                } else {
                                    ManagedMap innerproductsbundleoption = new ManagedMap();
                                    innerproductsbundleoptionMapChilds = DomUtils.getChildElementsByTagName(productsBundleOptionsChild, "inner-products-bundle-option");
                                    if (innerproductsbundleoptionMapChilds!= null) {
                                        if (innerproductsbundleoptionMapChilds.size() == 0) {
                                            innerproductsbundleoptionMapChilds = DomUtils.getChildElements(productsBundleOptionsChild);
                                        }
                                        for (Element innerproductsbundleoptionChild: innerproductsbundleoptionMapChilds) {
                                            String innerproductsbundleoptionValueRef = innerproductsbundleoptionChild.getAttribute("value-ref");
                                            String innerproductsbundleoptionKeyRef = innerproductsbundleoptionChild.getAttribute("key-ref");
                                            Object valueObject = null;
                                            Object keyObject = null;
                                            if ((innerproductsbundleoptionValueRef!= null)&&(!StringUtils.isBlank(innerproductsbundleoptionValueRef))) {
                                                if ((!innerproductsbundleoptionValueRef.startsWith(patternInfo.getPrefix()))&&(!innerproductsbundleoptionValueRef.endsWith(patternInfo.getSuffix()))) {
                                                    valueObject = new RuntimeBeanReference(innerproductsbundleoptionValueRef);
                                                } else {
                                                    valueObject = innerproductsbundleoptionValueRef;
                                                }
                                            } else {
                                                valueObject = innerproductsbundleoptionChild.getTextContent();
                                            }
                                            if ((innerproductsbundleoptionKeyRef!= null)&&(!StringUtils.isBlank(innerproductsbundleoptionKeyRef))) {
                                                keyObject = new RuntimeBeanReference(innerproductsbundleoptionKeyRef);
                                            } else {
                                                keyObject = innerproductsbundleoptionChild.getAttribute("key");
                                            }
                                            if ((keyObject == null)||((keyObject instanceof String)&&StringUtils.isBlank(((String) keyObject)))) {
                                                keyObject = innerproductsbundleoptionChild.getTagName();
                                            }
                                            innerproductsbundleoption.put(keyObject, valueObject);
                                        }
                                    }
                                    productsBundleOptions.add(innerproductsbundleoption);
                                }
                            }
                        }
                    }
                }
                builder.addPropertyValue("productsBundleOptions", productsBundleOptions);
            }
        }
        Element productsBundleOptionsQtyListElement = null;
        productsBundleOptionsQtyListElement = DomUtils.getChildElementByTagName(element, "products-bundle-options-qty");
        List<Element> productsBundleOptionsQtyListChilds = null;
        if (productsBundleOptionsQtyListElement!= null) {
            String productsBundleOptionsQtyRef = productsBundleOptionsQtyListElement.getAttribute("ref");
            if ((productsBundleOptionsQtyRef!= null)&&(!StringUtils.isBlank(productsBundleOptionsQtyRef))) {
                if ((!productsBundleOptionsQtyRef.startsWith(patternInfo.getPrefix()))&&(!productsBundleOptionsQtyRef.endsWith(patternInfo.getSuffix()))) {
                    builder.addPropertyValue("productsBundleOptionsQty", new RuntimeBeanReference(productsBundleOptionsQtyRef));
                } else {
                    builder.addPropertyValue("productsBundleOptionsQty", productsBundleOptionsQtyRef);
                }
            } else {
                ManagedList productsBundleOptionsQty = new ManagedList();
                productsBundleOptionsQtyListChilds = DomUtils.getChildElementsByTagName(productsBundleOptionsQtyListElement, "products-bundle-options-qty");
                if (productsBundleOptionsQtyListChilds!= null) {
                    for (Element productsBundleOptionsQtyChild: productsBundleOptionsQtyListChilds) {
                        String valueRef = productsBundleOptionsQtyChild.getAttribute("value-ref");
                        if ((valueRef!= null)&&(!StringUtils.isBlank(valueRef))) {
                            if ((!valueRef.startsWith(patternInfo.getPrefix()))&&(!valueRef.endsWith(patternInfo.getSuffix()))) {
                                productsBundleOptionsQty.add(new RuntimeBeanReference(valueRef));
                            } else {
                                productsBundleOptionsQty.add(valueRef);
                            }
                        } else {
                            List<Element> innerproductsbundleoptionsqtyMapChilds = null;
                            if (productsBundleOptionsQtyChild!= null) {
                                String innerproductsbundleoptionsqtyRef = productsBundleOptionsQtyChild.getAttribute("ref");
                                if ((innerproductsbundleoptionsqtyRef!= null)&&(!StringUtils.isBlank(innerproductsbundleoptionsqtyRef))) {
                                    if ((!innerproductsbundleoptionsqtyRef.startsWith(patternInfo.getPrefix()))&&(!innerproductsbundleoptionsqtyRef.endsWith(patternInfo.getSuffix()))) {
                                        builder.addPropertyValue("inner-products-bundle-options-qty", new RuntimeBeanReference(innerproductsbundleoptionsqtyRef));
                                    } else {
                                        builder.addPropertyValue("inner-products-bundle-options-qty", innerproductsbundleoptionsqtyRef);
                                    }
                                } else {
                                    ManagedMap innerproductsbundleoptionsqty = new ManagedMap();
                                    innerproductsbundleoptionsqtyMapChilds = DomUtils.getChildElementsByTagName(productsBundleOptionsQtyChild, "inner-products-bundle-options-qty");
                                    if (innerproductsbundleoptionsqtyMapChilds!= null) {
                                        if (innerproductsbundleoptionsqtyMapChilds.size() == 0) {
                                            innerproductsbundleoptionsqtyMapChilds = DomUtils.getChildElements(productsBundleOptionsQtyChild);
                                        }
                                        for (Element innerproductsbundleoptionsqtyChild: innerproductsbundleoptionsqtyMapChilds) {
                                            String innerproductsbundleoptionsqtyValueRef = innerproductsbundleoptionsqtyChild.getAttribute("value-ref");
                                            String innerproductsbundleoptionsqtyKeyRef = innerproductsbundleoptionsqtyChild.getAttribute("key-ref");
                                            Object valueObject = null;
                                            Object keyObject = null;
                                            if ((innerproductsbundleoptionsqtyValueRef!= null)&&(!StringUtils.isBlank(innerproductsbundleoptionsqtyValueRef))) {
                                                if ((!innerproductsbundleoptionsqtyValueRef.startsWith(patternInfo.getPrefix()))&&(!innerproductsbundleoptionsqtyValueRef.endsWith(patternInfo.getSuffix()))) {
                                                    valueObject = new RuntimeBeanReference(innerproductsbundleoptionsqtyValueRef);
                                                } else {
                                                    valueObject = innerproductsbundleoptionsqtyValueRef;
                                                }
                                            } else {
                                                valueObject = innerproductsbundleoptionsqtyChild.getTextContent();
                                            }
                                            if ((innerproductsbundleoptionsqtyKeyRef!= null)&&(!StringUtils.isBlank(innerproductsbundleoptionsqtyKeyRef))) {
                                                keyObject = new RuntimeBeanReference(innerproductsbundleoptionsqtyKeyRef);
                                            } else {
                                                keyObject = innerproductsbundleoptionsqtyChild.getAttribute("key");
                                            }
                                            if ((keyObject == null)||((keyObject instanceof String)&&StringUtils.isBlank(((String) keyObject)))) {
                                                keyObject = innerproductsbundleoptionsqtyChild.getTagName();
                                            }
                                            innerproductsbundleoptionsqty.put(keyObject, valueObject);
                                        }
                                    }
                                    productsBundleOptionsQty.add(innerproductsbundleoptionsqty);
                                }
                            }
                        }
                    }
                }
                builder.addPropertyValue("productsBundleOptionsQty", productsBundleOptionsQty);
            }
        }
        if ((element.getAttribute("storeId")!= null)&&(!StringUtils.isBlank(element.getAttribute("storeId")))) {
            builder.addPropertyValue("storeId", element.getAttribute("storeId"));
        }
        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;
        }
    }

}
