
package org.mule.modules.quickbooks.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.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("accessToken")!= null)&&(!StringUtils.isBlank(element.getAttribute("accessToken")))) {
            builder.addPropertyValue("accessToken", element.getAttribute("accessToken"));
        }
        if ((element.getAttribute("accessTokenSecret")!= null)&&(!StringUtils.isBlank(element.getAttribute("accessTokenSecret")))) {
            builder.addPropertyValue("accessTokenSecret", element.getAttribute("accessTokenSecret"));
        }
        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 webSiteListElement = null;
        webSiteListElement = DomUtils.getChildElementByTagName(element, "web-site");
        List<Element> webSiteListChilds = null;
        if (webSiteListElement!= null) {
            String webSiteRef = webSiteListElement.getAttribute("ref");
            if ((webSiteRef!= null)&&(!StringUtils.isBlank(webSiteRef))) {
                if ((!webSiteRef.startsWith(patternInfo.getPrefix()))&&(!webSiteRef.endsWith(patternInfo.getSuffix()))) {
                    builder.addPropertyValue("webSite", new RuntimeBeanReference(webSiteRef));
                } else {
                    builder.addPropertyValue("webSite", webSiteRef);
                }
            } else {
                ManagedList webSite = new ManagedList();
                webSiteListChilds = DomUtils.getChildElementsByTagName(webSiteListElement, "web-site");
                if (webSiteListChilds!= null) {
                    for (Element webSiteChild: webSiteListChilds) {
                        String valueRef = webSiteChild.getAttribute("value-ref");
                        if ((valueRef!= null)&&(!StringUtils.isBlank(valueRef))) {
                            webSite.add(new RuntimeBeanReference(valueRef));
                        } else {
                            List<Element> innerwebsiteListChilds = null;
                            if (webSiteChild!= null) {
                                String innerwebsiteRef = webSiteChild.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(webSiteChild, "inner-web-site");
                                    if (innerwebsiteListChilds!= null) {
                                        if (innerwebsiteListChilds.size() == 0) {
                                            innerwebsiteListChilds = DomUtils.getChildElements(webSiteChild);
                                        }
                                        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);
                                        }
                                    }
                                    webSite.add(innerwebsite);
                                }
                            }
                        }
                    }
                }
                builder.addPropertyValue("webSite", webSite);
            }
        }
        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 emailListElement = null;
        emailListElement = DomUtils.getChildElementByTagName(element, "email");
        List<Element> emailListChilds = null;
        if (emailListElement!= null) {
            String emailRef = emailListElement.getAttribute("ref");
            if ((emailRef!= null)&&(!StringUtils.isBlank(emailRef))) {
                if ((!emailRef.startsWith(patternInfo.getPrefix()))&&(!emailRef.endsWith(patternInfo.getSuffix()))) {
                    builder.addPropertyValue("email", new RuntimeBeanReference(emailRef));
                } else {
                    builder.addPropertyValue("email", emailRef);
                }
            } else {
                ManagedList email = new ManagedList();
                emailListChilds = DomUtils.getChildElementsByTagName(emailListElement, "email");
                if (emailListChilds!= null) {
                    for (Element emailChild: emailListChilds) {
                        String valueRef = emailChild.getAttribute("value-ref");
                        if ((valueRef!= null)&&(!StringUtils.isBlank(valueRef))) {
                            email.add(new RuntimeBeanReference(valueRef));
                        } else {
                            email.add(emailChild.getTextContent());
                        }
                    }
                }
                builder.addPropertyValue("email", email);
            }
        }
        Element phoneListElement = null;
        phoneListElement = DomUtils.getChildElementByTagName(element, "phone");
        List<Element> phoneListChilds = null;
        if (phoneListElement!= null) {
            String phoneRef = phoneListElement.getAttribute("ref");
            if ((phoneRef!= null)&&(!StringUtils.isBlank(phoneRef))) {
                if ((!phoneRef.startsWith(patternInfo.getPrefix()))&&(!phoneRef.endsWith(patternInfo.getSuffix()))) {
                    builder.addPropertyValue("phone", new RuntimeBeanReference(phoneRef));
                } else {
                    builder.addPropertyValue("phone", phoneRef);
                }
            } else {
                ManagedList phone = new ManagedList();
                phoneListChilds = DomUtils.getChildElementsByTagName(phoneListElement, "phone");
                if (phoneListChilds!= null) {
                    for (Element phoneChild: phoneListChilds) {
                        String valueRef = phoneChild.getAttribute("value-ref");
                        if ((valueRef!= null)&&(!StringUtils.isBlank(valueRef))) {
                            phone.add(new RuntimeBeanReference(valueRef));
                        } else {
                            List<Element> innerphoneListChilds = null;
                            if (phoneChild!= null) {
                                String innerphoneRef = phoneChild.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(phoneChild, "inner-phone");
                                    if (innerphoneListChilds!= null) {
                                        if (innerphoneListChilds.size() == 0) {
                                            innerphoneListChilds = DomUtils.getChildElements(phoneChild);
                                        }
                                        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);
                                        }
                                    }
                                    phone.add(innerphone);
                                }
                            }
                        }
                    }
                }
                builder.addPropertyValue("phone", phone);
            }
        }
        Element addressListElement = null;
        addressListElement = DomUtils.getChildElementByTagName(element, "address");
        List<Element> addressListChilds = null;
        if (addressListElement!= null) {
            String addressRef = addressListElement.getAttribute("ref");
            if ((addressRef!= null)&&(!StringUtils.isBlank(addressRef))) {
                if ((!addressRef.startsWith(patternInfo.getPrefix()))&&(!addressRef.endsWith(patternInfo.getSuffix()))) {
                    builder.addPropertyValue("address", new RuntimeBeanReference(addressRef));
                } else {
                    builder.addPropertyValue("address", addressRef);
                }
            } else {
                ManagedList address = new ManagedList();
                addressListChilds = DomUtils.getChildElementsByTagName(addressListElement, "addres");
                if (addressListChilds!= null) {
                    for (Element addressChild: addressListChilds) {
                        String valueRef = addressChild.getAttribute("value-ref");
                        if ((valueRef!= null)&&(!StringUtils.isBlank(valueRef))) {
                            address.add(new RuntimeBeanReference(valueRef));
                        } else {
                            List<Element> inneraddresListChilds = null;
                            if (addressChild!= null) {
                                String inneraddresRef = addressChild.getAttribute("ref");
                                if ((inneraddresRef!= null)&&(!StringUtils.isBlank(inneraddresRef))) {
                                    if ((!inneraddresRef.startsWith(patternInfo.getPrefix()))&&(!inneraddresRef.endsWith(patternInfo.getSuffix()))) {
                                        builder.addPropertyValue("inner-addres", new RuntimeBeanReference(inneraddresRef));
                                    } else {
                                        builder.addPropertyValue("inner-addres", inneraddresRef);
                                    }
                                } else {
                                    ManagedMap inneraddres = new ManagedMap();
                                    inneraddresListChilds = DomUtils.getChildElementsByTagName(addressChild, "inner-addre");
                                    if (inneraddresListChilds!= null) {
                                        if (inneraddresListChilds.size() == 0) {
                                            inneraddresListChilds = DomUtils.getChildElements(addressChild);
                                        }
                                        for (Element inneraddresChild: inneraddresListChilds) {
                                            String inneraddresValueRef = inneraddresChild.getAttribute("value-ref");
                                            String inneraddresKeyRef = inneraddresChild.getAttribute("key-ref");
                                            Object valueObject = null;
                                            Object keyObject = null;
                                            if ((inneraddresValueRef!= null)&&(!StringUtils.isBlank(inneraddresValueRef))) {
                                                valueObject = new RuntimeBeanReference(inneraddresValueRef);
                                            } else {
                                                valueObject = inneraddresChild.getTextContent();
                                            }
                                            if ((inneraddresKeyRef!= null)&&(!StringUtils.isBlank(inneraddresKeyRef))) {
                                                keyObject = new RuntimeBeanReference(inneraddresKeyRef);
                                            } else {
                                                keyObject = inneraddresChild.getAttribute("key");
                                            }
                                            if ((keyObject == null)||((keyObject instanceof String)&&StringUtils.isBlank(((String) keyObject)))) {
                                                keyObject = inneraddresChild.getTagName();
                                            }
                                            inneraddres.put(keyObject, valueObject);
                                        }
                                    }
                                    address.add(inneraddres);
                                }
                            }
                        }
                    }
                }
                builder.addPropertyValue("address", address);
            }
        }
        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;
        }
    }

}
