
package org.mule.modules.quickbooks.online.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.quickbooks.online.config.CreateCustomerMessageProcessor;
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 CreateCustomerDefinitionParser
    implements BeanDefinitionParser
{

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

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

    public BeanDefinition parse(Element element, ParserContext parserContent) {
        BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(CreateCustomerMessageProcessor.class.getName());
        String configRef = element.getAttribute("config-ref");
        if ((configRef!= null)&&(!StringUtils.isBlank(configRef))) {
            builder.addPropertyValue("moduleObject", configRef);
        }
        if ((element.getAttribute("realmId")!= null)&&(!StringUtils.isBlank(element.getAttribute("realmId")))) {
            builder.addPropertyValue("realmId", element.getAttribute("realmId"));
        }
        if ((element.getAttribute("appKey")!= null)&&(!StringUtils.isBlank(element.getAttribute("appKey")))) {
            builder.addPropertyValue("appKey", element.getAttribute("appKey"));
        }
        if ((element.getAttribute("realmIdPseudonym")!= null)&&(!StringUtils.isBlank(element.getAttribute("realmIdPseudonym")))) {
            builder.addPropertyValue("realmIdPseudonym", element.getAttribute("realmIdPseudonym"));
        }
        if ((element.getAttribute("authIdPseudonym")!= null)&&(!StringUtils.isBlank(element.getAttribute("authIdPseudonym")))) {
            builder.addPropertyValue("authIdPseudonym", element.getAttribute("authIdPseudonym"));
        }
        if ((element.getAttribute("name")!= null)&&(!StringUtils.isBlank(element.getAttribute("name")))) {
            builder.addPropertyValue("name", element.getAttribute("name"));
        }
        if ((element.getAttribute("givenName")!= null)&&(!StringUtils.isBlank(element.getAttribute("givenName")))) {
            builder.addPropertyValue("givenName", element.getAttribute("givenName"));
        }
        if ((element.getAttribute("middleName")!= null)&&(!StringUtils.isBlank(element.getAttribute("middleName")))) {
            builder.addPropertyValue("middleName", element.getAttribute("middleName"));
        }
        if ((element.getAttribute("familyName")!= null)&&(!StringUtils.isBlank(element.getAttribute("familyName")))) {
            builder.addPropertyValue("familyName", element.getAttribute("familyName"));
        }
        if ((element.getAttribute("suffix")!= null)&&(!StringUtils.isBlank(element.getAttribute("suffix")))) {
            builder.addPropertyValue("suffix", element.getAttribute("suffix"));
        }
        if ((element.getAttribute("dBAName")!= null)&&(!StringUtils.isBlank(element.getAttribute("dBAName")))) {
            builder.addPropertyValue("dBAName", element.getAttribute("dBAName"));
        }
        if ((element.getAttribute("showAs")!= null)&&(!StringUtils.isBlank(element.getAttribute("showAs")))) {
            builder.addPropertyValue("showAs", element.getAttribute("showAs"));
        }
        Element webSitesListElement = null;
        webSitesListElement = DomUtils.getChildElementByTagName(element, "web-sites");
        List<Element> webSitesListChilds = null;
        if (webSitesListElement!= null) {
            String webSitesRef = webSitesListElement.getAttribute("ref");
            if ((webSitesRef!= null)&&(!StringUtils.isBlank(webSitesRef))) {
                if ((!webSitesRef.startsWith(patternInfo.getPrefix()))&&(!webSitesRef.endsWith(patternInfo.getSuffix()))) {
                    builder.addPropertyValue("webSites", new RuntimeBeanReference(webSitesRef));
                } else {
                    builder.addPropertyValue("webSites", webSitesRef);
                }
            } else {
                ManagedList webSites = new ManagedList();
                webSitesListChilds = DomUtils.getChildElementsByTagName(webSitesListElement, "web-site");
                if (webSitesListChilds!= null) {
                    for (Element webSitesChild: webSitesListChilds) {
                        String valueRef = webSitesChild.getAttribute("value-ref");
                        if ((valueRef!= null)&&(!StringUtils.isBlank(valueRef))) {
                            webSites.add(new RuntimeBeanReference(valueRef));
                        } else {
                            List<Element> innerwebsiteListChilds = null;
                            if (webSitesChild!= null) {
                                String innerwebsiteRef = webSitesChild.getAttribute("ref");
                                if ((innerwebsiteRef!= null)&&(!StringUtils.isBlank(innerwebsiteRef))) {
                                    if ((!innerwebsiteRef.startsWith(patternInfo.getPrefix()))&&(!innerwebsiteRef.endsWith(patternInfo.getSuffix()))) {
                                        builder.addPropertyValue("inner-web-site", new RuntimeBeanReference(innerwebsiteRef));
                                    } else {
                                        builder.addPropertyValue("inner-web-site", innerwebsiteRef);
                                    }
                                } else {
                                    ManagedMap innerwebsite = new ManagedMap();
                                    innerwebsiteListChilds = DomUtils.getChildElementsByTagName(webSitesChild, "inner-web-site");
                                    if (innerwebsiteListChilds!= null) {
                                        if (innerwebsiteListChilds.size() == 0) {
                                            innerwebsiteListChilds = DomUtils.getChildElements(webSitesChild);
                                        }
                                        for (Element innerwebsiteChild: innerwebsiteListChilds) {
                                            String innerwebsiteValueRef = innerwebsiteChild.getAttribute("value-ref");
                                            String innerwebsiteKeyRef = innerwebsiteChild.getAttribute("key-ref");
                                            Object valueObject = null;
                                            Object keyObject = null;
                                            if ((innerwebsiteValueRef!= null)&&(!StringUtils.isBlank(innerwebsiteValueRef))) {
                                                valueObject = new RuntimeBeanReference(innerwebsiteValueRef);
                                            } else {
                                                valueObject = innerwebsiteChild.getTextContent();
                                            }
                                            if ((innerwebsiteKeyRef!= null)&&(!StringUtils.isBlank(innerwebsiteKeyRef))) {
                                                keyObject = new RuntimeBeanReference(innerwebsiteKeyRef);
                                            } else {
                                                keyObject = innerwebsiteChild.getAttribute("key");
                                            }
                                            if ((keyObject == null)||((keyObject instanceof String)&&StringUtils.isBlank(((String) keyObject)))) {
                                                keyObject = innerwebsiteChild.getTagName();
                                            }
                                            innerwebsite.put(keyObject, valueObject);
                                        }
                                    }
                                    webSites.add(innerwebsite);
                                }
                            }
                        }
                    }
                }
                builder.addPropertyValue("webSites", webSites);
            }
        }
        Element salesTermIdListElement = null;
        salesTermIdListElement = DomUtils.getChildElementByTagName(element, "sales-term-id");
        List<Element> salesTermIdListChilds = null;
        if (salesTermIdListElement!= null) {
            String salesTermIdRef = salesTermIdListElement.getAttribute("ref");
            if ((salesTermIdRef!= null)&&(!StringUtils.isBlank(salesTermIdRef))) {
                if ((!salesTermIdRef.startsWith(patternInfo.getPrefix()))&&(!salesTermIdRef.endsWith(patternInfo.getSuffix()))) {
                    builder.addPropertyValue("salesTermId", new RuntimeBeanReference(salesTermIdRef));
                } else {
                    builder.addPropertyValue("salesTermId", salesTermIdRef);
                }
            } else {
                ManagedMap salesTermId = new ManagedMap();
                salesTermIdListChilds = DomUtils.getChildElementsByTagName(salesTermIdListElement, "sales-term-id");
                if (salesTermIdListChilds!= null) {
                    if (salesTermIdListChilds.size() == 0) {
                        salesTermIdListChilds = DomUtils.getChildElements(salesTermIdListElement);
                    }
                    for (Element salesTermIdChild: salesTermIdListChilds) {
                        String salesTermIdValueRef = salesTermIdChild.getAttribute("value-ref");
                        String salesTermIdKeyRef = salesTermIdChild.getAttribute("key-ref");
                        Object valueObject = null;
                        Object keyObject = null;
                        if ((salesTermIdValueRef!= null)&&(!StringUtils.isBlank(salesTermIdValueRef))) {
                            valueObject = new RuntimeBeanReference(salesTermIdValueRef);
                        } else {
                            valueObject = salesTermIdChild.getTextContent();
                        }
                        if ((salesTermIdKeyRef!= null)&&(!StringUtils.isBlank(salesTermIdKeyRef))) {
                            keyObject = new RuntimeBeanReference(salesTermIdKeyRef);
                        } else {
                            keyObject = salesTermIdChild.getAttribute("key");
                        }
                        if ((keyObject == null)||((keyObject instanceof String)&&StringUtils.isBlank(((String) keyObject)))) {
                            keyObject = salesTermIdChild.getTagName();
                        }
                        salesTermId.put(keyObject, valueObject);
                    }
                }
                builder.addPropertyValue("salesTermId", salesTermId);
            }
        }
        if ((element.getAttribute("salesTaxCodeId")!= null)&&(!StringUtils.isBlank(element.getAttribute("salesTaxCodeId")))) {
            builder.addPropertyValue("salesTaxCodeId", element.getAttribute("salesTaxCodeId"));
        }
        Element emailsListElement = null;
        emailsListElement = DomUtils.getChildElementByTagName(element, "emails");
        List<Element> emailsListChilds = null;
        if (emailsListElement!= null) {
            String emailsRef = emailsListElement.getAttribute("ref");
            if ((emailsRef!= null)&&(!StringUtils.isBlank(emailsRef))) {
                if ((!emailsRef.startsWith(patternInfo.getPrefix()))&&(!emailsRef.endsWith(patternInfo.getSuffix()))) {
                    builder.addPropertyValue("emails", new RuntimeBeanReference(emailsRef));
                } else {
                    builder.addPropertyValue("emails", emailsRef);
                }
            } else {
                ManagedList emails = new ManagedList();
                emailsListChilds = DomUtils.getChildElementsByTagName(emailsListElement, "email");
                if (emailsListChilds!= null) {
                    for (Element emailsChild: emailsListChilds) {
                        String valueRef = emailsChild.getAttribute("value-ref");
                        if ((valueRef!= null)&&(!StringUtils.isBlank(valueRef))) {
                            emails.add(new RuntimeBeanReference(valueRef));
                        } else {
                            List<Element> inneremailListChilds = null;
                            if (emailsChild!= null) {
                                String inneremailRef = emailsChild.getAttribute("ref");
                                if ((inneremailRef!= null)&&(!StringUtils.isBlank(inneremailRef))) {
                                    if ((!inneremailRef.startsWith(patternInfo.getPrefix()))&&(!inneremailRef.endsWith(patternInfo.getSuffix()))) {
                                        builder.addPropertyValue("inner-email", new RuntimeBeanReference(inneremailRef));
                                    } else {
                                        builder.addPropertyValue("inner-email", inneremailRef);
                                    }
                                } else {
                                    ManagedMap inneremail = new ManagedMap();
                                    inneremailListChilds = DomUtils.getChildElementsByTagName(emailsChild, "inner-email");
                                    if (inneremailListChilds!= null) {
                                        if (inneremailListChilds.size() == 0) {
                                            inneremailListChilds = DomUtils.getChildElements(emailsChild);
                                        }
                                        for (Element inneremailChild: inneremailListChilds) {
                                            String inneremailValueRef = inneremailChild.getAttribute("value-ref");
                                            String inneremailKeyRef = inneremailChild.getAttribute("key-ref");
                                            Object valueObject = null;
                                            Object keyObject = null;
                                            if ((inneremailValueRef!= null)&&(!StringUtils.isBlank(inneremailValueRef))) {
                                                valueObject = new RuntimeBeanReference(inneremailValueRef);
                                            } else {
                                                valueObject = inneremailChild.getTextContent();
                                            }
                                            if ((inneremailKeyRef!= null)&&(!StringUtils.isBlank(inneremailKeyRef))) {
                                                keyObject = new RuntimeBeanReference(inneremailKeyRef);
                                            } else {
                                                keyObject = inneremailChild.getAttribute("key");
                                            }
                                            if ((keyObject == null)||((keyObject instanceof String)&&StringUtils.isBlank(((String) keyObject)))) {
                                                keyObject = inneremailChild.getTagName();
                                            }
                                            inneremail.put(keyObject, valueObject);
                                        }
                                    }
                                    emails.add(inneremail);
                                }
                            }
                        }
                    }
                }
                builder.addPropertyValue("emails", emails);
            }
        }
        Element phonesListElement = null;
        phonesListElement = DomUtils.getChildElementByTagName(element, "phones");
        List<Element> phonesListChilds = null;
        if (phonesListElement!= null) {
            String phonesRef = phonesListElement.getAttribute("ref");
            if ((phonesRef!= null)&&(!StringUtils.isBlank(phonesRef))) {
                if ((!phonesRef.startsWith(patternInfo.getPrefix()))&&(!phonesRef.endsWith(patternInfo.getSuffix()))) {
                    builder.addPropertyValue("phones", new RuntimeBeanReference(phonesRef));
                } else {
                    builder.addPropertyValue("phones", phonesRef);
                }
            } else {
                ManagedList phones = new ManagedList();
                phonesListChilds = DomUtils.getChildElementsByTagName(phonesListElement, "phone");
                if (phonesListChilds!= null) {
                    for (Element phonesChild: phonesListChilds) {
                        String valueRef = phonesChild.getAttribute("value-ref");
                        if ((valueRef!= null)&&(!StringUtils.isBlank(valueRef))) {
                            phones.add(new RuntimeBeanReference(valueRef));
                        } else {
                            List<Element> innerphoneListChilds = null;
                            if (phonesChild!= null) {
                                String innerphoneRef = phonesChild.getAttribute("ref");
                                if ((innerphoneRef!= null)&&(!StringUtils.isBlank(innerphoneRef))) {
                                    if ((!innerphoneRef.startsWith(patternInfo.getPrefix()))&&(!innerphoneRef.endsWith(patternInfo.getSuffix()))) {
                                        builder.addPropertyValue("inner-phone", new RuntimeBeanReference(innerphoneRef));
                                    } else {
                                        builder.addPropertyValue("inner-phone", innerphoneRef);
                                    }
                                } else {
                                    ManagedMap innerphone = new ManagedMap();
                                    innerphoneListChilds = DomUtils.getChildElementsByTagName(phonesChild, "inner-phone");
                                    if (innerphoneListChilds!= null) {
                                        if (innerphoneListChilds.size() == 0) {
                                            innerphoneListChilds = DomUtils.getChildElements(phonesChild);
                                        }
                                        for (Element innerphoneChild: innerphoneListChilds) {
                                            String innerphoneValueRef = innerphoneChild.getAttribute("value-ref");
                                            String innerphoneKeyRef = innerphoneChild.getAttribute("key-ref");
                                            Object valueObject = null;
                                            Object keyObject = null;
                                            if ((innerphoneValueRef!= null)&&(!StringUtils.isBlank(innerphoneValueRef))) {
                                                valueObject = new RuntimeBeanReference(innerphoneValueRef);
                                            } else {
                                                valueObject = innerphoneChild.getTextContent();
                                            }
                                            if ((innerphoneKeyRef!= null)&&(!StringUtils.isBlank(innerphoneKeyRef))) {
                                                keyObject = new RuntimeBeanReference(innerphoneKeyRef);
                                            } else {
                                                keyObject = innerphoneChild.getAttribute("key");
                                            }
                                            if ((keyObject == null)||((keyObject instanceof String)&&StringUtils.isBlank(((String) keyObject)))) {
                                                keyObject = innerphoneChild.getTagName();
                                            }
                                            innerphone.put(keyObject, valueObject);
                                        }
                                    }
                                    phones.add(innerphone);
                                }
                            }
                        }
                    }
                }
                builder.addPropertyValue("phones", phones);
            }
        }
        Element addressesListElement = null;
        addressesListElement = DomUtils.getChildElementByTagName(element, "addresses");
        List<Element> addressesListChilds = null;
        if (addressesListElement!= null) {
            String addressesRef = addressesListElement.getAttribute("ref");
            if ((addressesRef!= null)&&(!StringUtils.isBlank(addressesRef))) {
                if ((!addressesRef.startsWith(patternInfo.getPrefix()))&&(!addressesRef.endsWith(patternInfo.getSuffix()))) {
                    builder.addPropertyValue("addresses", new RuntimeBeanReference(addressesRef));
                } else {
                    builder.addPropertyValue("addresses", addressesRef);
                }
            } else {
                ManagedList addresses = new ManagedList();
                addressesListChilds = DomUtils.getChildElementsByTagName(addressesListElement, "address");
                if (addressesListChilds!= null) {
                    for (Element addressesChild: addressesListChilds) {
                        String valueRef = addressesChild.getAttribute("value-ref");
                        if ((valueRef!= null)&&(!StringUtils.isBlank(valueRef))) {
                            addresses.add(new RuntimeBeanReference(valueRef));
                        } else {
                            List<Element> inneraddressListChilds = null;
                            if (addressesChild!= null) {
                                String inneraddressRef = addressesChild.getAttribute("ref");
                                if ((inneraddressRef!= null)&&(!StringUtils.isBlank(inneraddressRef))) {
                                    if ((!inneraddressRef.startsWith(patternInfo.getPrefix()))&&(!inneraddressRef.endsWith(patternInfo.getSuffix()))) {
                                        builder.addPropertyValue("inner-address", new RuntimeBeanReference(inneraddressRef));
                                    } else {
                                        builder.addPropertyValue("inner-address", inneraddressRef);
                                    }
                                } else {
                                    ManagedMap inneraddress = new ManagedMap();
                                    inneraddressListChilds = DomUtils.getChildElementsByTagName(addressesChild, "inner-addres");
                                    if (inneraddressListChilds!= null) {
                                        if (inneraddressListChilds.size() == 0) {
                                            inneraddressListChilds = DomUtils.getChildElements(addressesChild);
                                        }
                                        for (Element inneraddressChild: inneraddressListChilds) {
                                            String inneraddressValueRef = inneraddressChild.getAttribute("value-ref");
                                            String inneraddressKeyRef = inneraddressChild.getAttribute("key-ref");
                                            Object valueObject = null;
                                            Object keyObject = null;
                                            if ((inneraddressValueRef!= null)&&(!StringUtils.isBlank(inneraddressValueRef))) {
                                                valueObject = new RuntimeBeanReference(inneraddressValueRef);
                                            } else {
                                                valueObject = inneraddressChild.getTextContent();
                                            }
                                            if ((inneraddressKeyRef!= null)&&(!StringUtils.isBlank(inneraddressKeyRef))) {
                                                keyObject = new RuntimeBeanReference(inneraddressKeyRef);
                                            } else {
                                                keyObject = inneraddressChild.getAttribute("key");
                                            }
                                            if ((keyObject == null)||((keyObject instanceof String)&&StringUtils.isBlank(((String) keyObject)))) {
                                                keyObject = inneraddressChild.getTagName();
                                            }
                                            inneraddress.put(keyObject, valueObject);
                                        }
                                    }
                                    addresses.add(inneraddress);
                                }
                            }
                        }
                    }
                }
                builder.addPropertyValue("addresses", addresses);
            }
        }
        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;
        }
    }

}
