/*
 * Copyright 2016 The original 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 io.sundr.codegen.generator;

import io.sundr.codegen.directives.ClassDirective;
import io.sundr.codegen.directives.FieldDirective;
import io.sundr.codegen.directives.MethodDirective;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.ParserPoolImpl;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.resource.loader.StringResourceLoader;
import org.apache.velocity.runtime.log.NullLogChute;
import org.apache.velocity.runtime.resource.ResourceManagerImpl;
import org.apache.velocity.runtime.resource.ResourceCacheImpl;
import org.apache.velocity.util.introspection.Uberspect;
import org.apache.velocity.util.introspection.UberspectImpl;
import io.sundr.codegen.directives.PluralizeDirective;
import io.sundr.codegen.directives.SingularizeDirective;

public class CodeGeneratorContext {

    private final VelocityEngine velocityEngine;
    private final VelocityContext velocityContext;

    public CodeGeneratorContext() {
        this(new VelocityEngine(), new VelocityContext());
    }

    public CodeGeneratorContext(VelocityEngine velocityEngine, VelocityContext velocityContext) {
        this.velocityEngine = velocityEngine;
        this.velocityContext = velocityContext;

        this.velocityEngine.setProperty(RuntimeConstants.RESOURCE_LOADER, "string");
        this.velocityEngine.setProperty("string.resource.loader.class", StringResourceLoader.class.getName());
        //We are going to use shading so we need to make sure that the following configuration will be shade friendly...
        this.velocityEngine.setProperty(RuntimeConstants.RESOURCE_MANAGER_CLASS, ResourceManagerImpl.class.getName());
        this.velocityEngine.setProperty(RuntimeConstants.RESOURCE_MANAGER_CACHE_CLASS, ResourceCacheImpl.class.getName());
        this.velocityEngine.setProperty(RuntimeConstants.PARSER_POOL_CLASS, ParserPoolImpl.class.getName());
        this.velocityEngine.setProperty(RuntimeConstants.UBERSPECT_CLASSNAME, UberspectImpl.class.getName());
        this.velocityEngine.setProperty("runtime.log.logsystem.class", NullLogChute.class.getName());
        this.velocityEngine.init();

        //Load standard directives
        this.velocityEngine.loadDirective(ClassDirective.class.getCanonicalName());
        this.velocityEngine.loadDirective(MethodDirective.class.getCanonicalName());
        this.velocityEngine.loadDirective(FieldDirective.class.getCanonicalName());
        //Load utility directives
        this.velocityEngine.loadDirective(PluralizeDirective.class.getCanonicalName());
        this.velocityEngine.loadDirective(SingularizeDirective.class.getCanonicalName());
    }

    public VelocityEngine getVelocityEngine() {
        return velocityEngine;
    }

    public VelocityContext getVelocityContext() {
        return velocityContext;
    }
}

@@ -1012,16 +1012,68 @@
 stants;%0A
+import org.apache.velocity.runtime.log.JdkLogChute;%0A
 import o
@@ -1176,20 +1176,22 @@
 ime.log.
-Null
+System
 LogChute
@@ -2837,39 +2837,235 @@
 s%22, 
-NullLogChute.class.getName
+SystemLogChute.class.getName());%0A%0A        ClassLoader current = Thread.currentThread().getContextClassLoader();%0A        try %7B%0A            Thread.currentThread().setContextClassLoader(VelocityEngine.class.getClassLoader
 ());%0A
+    
     
@@ -3101,24 +3101,28 @@
 );%0A%0A        
+   
+ 
 //Load stand
@@ -3128,32 +3128,36 @@
 dard directives%0A
+    
         this.vel
@@ -3224,32 +3224,36 @@
 ame());%0A        
+ 
+   
 this.velocityEng
@@ -3313,32 +3313,36 @@
 ame());%0A        
+  
+  
 this.velocityEng
@@ -3405,24 +3405,28 @@
 ));%0A        
+    
 //Load utili
@@ -3431,32 +3431,36 @@
 lity directives%0A
+    
         this.vel
@@ -3531,32 +3531,36 @@
 ame());%0A        
+    
 this.velocityEng
@@ -3621,24 +3621,121 @@
 calName());%0A
+        %7D finally %7B%0A            Thread.currentThread().setContextClassLoader(current);%0A        %7D%0A
     %7D%0A%0A    p

set TCCL when initializing VelocityContext.