/**
 * Copyright (C) 2009 - present by OpenGamma Inc. and the OpenGamma group of companies
 *
 * Please see distribution for license.
 */
package com.opengamma.web.bundle;

import java.io.IOException;
import java.util.List;

import org.apache.commons.io.IOUtils;

import com.opengamma.DataNotFoundException;
import com.opengamma.util.ArgumentChecker;

/**
 * Utility to assist in the creation of bundles in HTML.
 */
public final class BundleUtils {

  /**
   * Builds the imports for a bundle.
   * 
   * @param bundle  the bundle, not null
   * @param webBundleUris  the URI helper, not null
   * @return the bundle HTML import text, not null
   */
  public static String buildImports(Bundle bundle, WebBundlesUris webBundleUris) {
    ArgumentChecker.notNull(bundle, "bundle");
    ArgumentChecker.notNull(webBundleUris, "webBundleUris");
    
    StringBuilder buf = new StringBuilder();
    List<BundleNode> childNodes = bundle.getChildNodes();
    for (BundleNode node : childNodes) {
      if (node instanceof Bundle) {
        Bundle nodeBundle = (Bundle) node;
        buf.append("@import url('");
        buf.append(webBundleUris.bundle(DeployMode.DEV, nodeBundle.getId()));
        buf.append("');\n");
      }
      if (node instanceof Fragment) {
        Fragment fragment = (Fragment) node;
        buf.append("@import url('" + fragment.getPath() + "');\n");
      }
    }
    return buf.toString();
  }

  /**
   * Reads and combines a bundle.
   * 
   * @param bundle  the bundle to read, not null
   * @return the combined source code, not null
   */
  public static String readBundleSource(Bundle bundle) {
    List<Fragment> allFragments = bundle.getAllFragments();
    StringBuilder buf = new StringBuilder(1024);
    for (Fragment fragment : allFragments) {
      try {
        buf.append(IOUtils.toString(fragment.getUri()));
        buf.append("\n");
      } catch (IOException ex) {
        throw new DataNotFoundException("IOException reading " + fragment.getUri());
      }
    }
    return buf.toString();
  }

}

@@ -255,16 +255,73 @@
 IOUtils;
+%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;
 %0A%0Aimport
@@ -400,16 +400,64 @@
 Checker;
+%0Aimport com.opengamma.web.sass.RubySassCompiler;
 %0A%0A/**%0A *
@@ -549,16 +549,104 @@
 tils %7B%0A%0A
+  private static final Logger s_logger = LoggerFactory.getLogger(BundleUtils.class);%0A  %0A
   /**%0A  
@@ -1483,32 +1483,361 @@
 Fragment) node;%0A
+        BundleType type = BundleType.getType(fragment.getUri().toString());%0A        if (type == BundleType.SCSS) %7B%0A          buf.append(%22@import url('%22);%0A          String fragmentPath = buildFragmentPath(fragment);%0A          buf.append(webBundleUris.sassfragment(fragmentPath));%0A          buf.append(%22');%5Cn%22);%0A        %7D else %7B%0A  
         buf.appe
@@ -1890,32 +1890,42 @@
 %5Cn%22);%0A      
-%7D%0A
+  %7D%0A  
     %7D%0A
+    %7D%0A
     return b
@@ -1944,16 +1944,371 @@
 );%0A  %7D%0A%0A
+  private static String buildFragmentPath(Fragment fragment) %7B%0A    String fragmentPath = fragment.getPath().toLowerCase();%0A    fragmentPath = fragmentPath.startsWith(%22/%22) ? fragmentPath.substring(1) : fragmentPath;%0A    fragmentPath = fragmentPath.replace(%22.%22 + BundleType.SCSS.getSuffix(), %22.%22 + BundleType.CSS.getSuffix());%0A    return fragmentPath;%0A  %7D%0A%0A
   /**%0A  
@@ -2501,24 +2501,102 @@
 e bundle) %7B%0A
+    %0A    RubySassCompiler sassCompiler = RubySassCompiler.getInstance();%0A    %0A
     List%3CFra
@@ -2757,27 +2757,41 @@
         
-buf.append(
+String fragmentContent = 
 IOUtils.
@@ -2817,16 +2817,379 @@
 etUri())
+;%0A        BundleType type = BundleType.getType(fragment.getUri().toString());%0A        if (type == BundleType.SCSS) %7B%0A          s_logger.debug(%22raw sass:%5Cn %7B%7D%5Cn%22, fragmentContent);%0A          fragmentContent = sassCompiler.sassConvert(fragmentContent);%0A          s_logger.debug(%22compiled sass:%5Cn %7B%7D%5Cn%22, fragmentContent);%0A        %7D%0A        buf.append(fragmentContent
 );%0A     

Build @import for dev scss files