/*
 * Copyright 2008-2013 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.broadleafcommerce.core.web.processor;

import org.apache.commons.lang3.StringUtils;
import org.broadleafcommerce.core.catalog.domain.ProductOption;
import org.broadleafcommerce.core.order.domain.DiscreteOrderItem;
import org.springframework.stereotype.Component;
import org.thymeleaf.Arguments;
import org.thymeleaf.dom.Element;
import org.thymeleaf.processor.element.AbstractLocalVariableDefinitionElementProcessor;
import org.thymeleaf.standard.expression.StandardExpressionProcessor;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Priyesh Patel
 */
@Component("blProductOptionDisplayProcessor")
public class ProductOptionDisplayProcessor extends AbstractLocalVariableDefinitionElementProcessor {

    /**
     * Sets the name of this processor to be used in Thymeleaf template
     */
    public ProductOptionDisplayProcessor() {
        super("product_option_display");
    }

    @Override
    public int getPrecedence() {
        return 100;
    }

    protected void initServices(Arguments arguments) {


    }

    @Override
    protected Map<String, Object> getNewLocalVariables(Arguments arguments, Element element) {
        initServices(arguments);
        HashMap<String, String> productOptionDisplayValues = new HashMap<String, String>();

        DiscreteOrderItem orderItem = (DiscreteOrderItem) StandardExpressionProcessor.processExpression(arguments,
                element.getAttributeValue("orderItem"));

        Map<String, Object> newVars = new HashMap<String, Object>();
        for (String i : orderItem.getOrderItemAttributes().keySet()) {
            for (ProductOption option : orderItem.getProduct().getProductOptions()) {
                if (option.getAttributeName().equals(i) && !StringUtils.isEmpty(orderItem.getOrderItemAttributes().get(i).toString())) {
                    productOptionDisplayValues.put(option.getLabel(), orderItem.getOrderItemAttributes().get(i).toString());
                }
            }
        }
        newVars.put("productOptionDisplayValues", productOptionDisplayValues);
        
        return newVars;
    }

    @Override
    protected boolean removeHostElement(Arguments arguments, Element element) {
        return false;
    }
}

@@ -1900,17 +1900,247 @@
 ing%3E();%0A
-%0A
+        Map%3CString, Object%3E newVars = new HashMap%3CString, Object%3E();%0A        if (StandardExpressionProcessor.processExpression(arguments,%0A                element.getAttributeValue(%22orderItem%22)) instanceof DiscreteOrderItem) %7B%0A%0A    
         
@@ -2254,32 +2254,36 @@
                 
+    
 element.getAttri
@@ -2316,73 +2316,8 @@
     
-    Map%3CString, Object%3E newVars = new HashMap%3CString, Object%3E();%0A
     
@@ -2379,24 +2379,28 @@
 keySet()) %7B%0A
+    
             
@@ -2485,24 +2485,28 @@
             
+    
 if (option.g
@@ -2634,16 +2634,20 @@
         
+    
 productO
@@ -2743,16 +2743,38 @@
 ing());%0A
+                    %7D%0A
         
@@ -2886,24 +2886,16 @@
 alues);%0A
-        
 %0A       

check if that orderitem is discreteOrderItem