/*
 * Copyright (C) 2012 salesforce.com, inc.
 *
 * 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.auraframework.impl.css.parser;

import java.util.Set;

import org.auraframework.Aura;
import org.auraframework.def.DefDescriptor;
import org.auraframework.def.DefDescriptor.DefType;
import org.auraframework.def.Definition;
import org.auraframework.def.ThemeDef;
import org.auraframework.impl.css.theme.ThemeDefImpl;
import org.auraframework.system.Client;
import org.auraframework.system.Parser;
import org.auraframework.system.Source;
import org.auraframework.throwable.quickfix.ThemeParserException;
import org.auraframework.util.AuraTextUtil;

import com.google.common.collect.ImmutableSet;
import com.google.common.css.compiler.ast.GssParserException;

/**
 */
public class ThemeParser implements Parser {

    private static ThemeParser instance = new ThemeParser(true);
    private static ThemeParser nonValidatingInstance = new ThemeParser(false);

    private static Set<String> allowedConditions;

    // build list of conditional permutations and allowed conditionals
    static {
        ImmutableSet.Builder<String> acBuilder = ImmutableSet.builder();
        for (Client.Type type : Client.Type.values()) {
            acBuilder.add(type.toString());
        }
        allowedConditions = acBuilder.build();
    }

    public static ThemeParser getInstance() {
        return Aura.getConfigAdapter().validateCss() ? instance : nonValidatingInstance;
    }

    public static ThemeParser getNonValidatingInstance() {
        return nonValidatingInstance;
    }

    private final boolean doValidation;

    protected ThemeParser(boolean doValidation) {
        this.doValidation = doValidation;
    }

    @SuppressWarnings("unchecked")
    @Override
    public <D extends Definition> D parse(DefDescriptor<D> descriptor, Source<?> source) throws ThemeParserException {

        if (descriptor.getDefType() == DefType.STYLE) {
            String className = descriptor.getNamespace() + AuraTextUtil.initCap(descriptor.getName());
            ThemeDefImpl.Builder builder = new ThemeDefImpl.Builder();
            builder.setDescriptor((DefDescriptor<ThemeDef>) descriptor);
            builder.setLocation(source.getSystemId(), source.getLastModified());
            builder.setClassName(className);

            if (descriptor.getName().toLowerCase().endsWith("template")) {
                builder.setCode(source.getContents());
            } else {
                CSSParser parser = new CSSParser(doValidation, className, source.getContents(), allowedConditions);
                ThemeParserResultHolder resultHolder;
                try {
                    resultHolder = parser.parse();
                } catch (GssParserException e) {
                    throw new ThemeParserException(e.getMessage(), builder.getLocation());
                }

                // scram if we found errors
                if (parser.hasErrors()) {
                    throw new ThemeParserException(parser.getErrorMessage(), builder.getLocation());
                }

                builder.setCode(resultHolder.getDefaultCss());
                builder.setCode(resultHolder.getBrowserCssMap());
                builder.setImageURLs(resultHolder.getImageURLs());
            }

            return (D) builder.build();
        }
        return null;
    }

}

@@ -2828,16 +2828,46 @@
 ssName);
+%0A            CSSParser parser;
 %0A%0A      
@@ -2951,32 +2951,57 @@
         
-builder.setCode(
+parser = new CSSParser(false, className, 
 source.g
@@ -3012,16 +3012,35 @@
 ntents()
+, allowedConditions
 );%0A     
@@ -3067,34 +3067,24 @@
             
-CSSParser 
 parser = new
@@ -3157,32 +3157,42 @@
 onditions);%0A    
+        %7D%0A
             Them
@@ -3241,22 +3241,14 @@
     
-    
 try %7B%0A
-    
     
@@ -3302,20 +3302,16 @@
         
- 
-   
 %7D catch 
@@ -3331,28 +3331,24 @@
 eption e) %7B%0A
-    
             
@@ -3430,35 +3430,27 @@
             
-    %7D%0A%0A    
+%7D%0A%0A
             
@@ -3485,28 +3485,24 @@
             
-   
- 
 if (parser.h
@@ -3511,28 +3511,24 @@
 Errors()) %7B%0A
-    
             
@@ -3604,36 +3604,32 @@
 getLocation());%0A
-    
             %7D%0A%0A 
@@ -3631,36 +3631,32 @@
  %7D%0A%0A            
-    
 builder.setCode(
@@ -3682,28 +3682,24 @@
 aultCss());%0A
-    
             
@@ -3752,36 +3752,32 @@
 ));%0A            
- 
-   
 builder.setImage
@@ -3807,39 +3807,24 @@
 ageURLs());%0A
-            %7D%0A%0A
             
@@ -3888,11 +3888,10 @@
 ;%0A    %7D%0A
-%0A
 %7D%0A

Fix template css handling.