/*
 * Copyright (C) 2013 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.docs;

import org.auraframework.Aura;
import org.auraframework.def.DefDescriptor;
import org.auraframework.def.DefDescriptor.DefType;
import org.auraframework.def.Definition;
import org.auraframework.def.IncludeDefRef;
import org.auraframework.def.LibraryDef;
import org.auraframework.instance.BaseComponent;
import org.auraframework.system.Annotations.AuraEnabled;
import org.auraframework.system.Annotations.Model;
import org.auraframework.system.AuraContext;
import org.auraframework.system.Source;
import org.auraframework.throwable.quickfix.QuickFixException;

/**
 */
@Model
public class EditorPanelModel {

    private final String code;
    private final String format;

    public EditorPanelModel() throws QuickFixException {

        AuraContext context = Aura.getContextService().getCurrentContext();
        BaseComponent<?, ?> component = context.getCurrentComponent();

        String desc = (String) component.getAttributes().getValue("descriptor");
        DefType defType = DefType.valueOf(((String) component.getAttributes().getValue("defType")).toUpperCase());
        
        DefDescriptor<? extends Definition> descriptor = null;
        if (defType != DefType.INCLUDE) {
            // Nominal case:
            descriptor = Aura.getDefinitionService().getDefDescriptor(desc, defType.getPrimaryInterface());
        } else {
            // Include case: since included .js files load source differently we have to manually
            // look up the include defs. If there is a usecase for looking up the defs in a non-doc 
            // setting, a lookup method should be added to the includeDef class.
            String name = (String) component.getAttributes().getValue("includeDefName");
            DefDescriptor<? extends LibraryDef> library = Aura.getDefinitionService().getDefDescriptor(
                desc, LibraryDef.class
            );
            
            for (IncludeDefRef includeDef : library.getDef().getIncludes()) {
               if (includeDef.getName().equals(name)) {
                   descriptor = includeDef.getDescriptor();
               }
            }
        }

        Source<?> source = context.getDefRegistry().getSource(descriptor);
        if (source != null && source.exists()) {
            code = source.getContents();
            format = String.valueOf(source.getFormat());
        } else {
            code = null;
            format = null;
        }
    }

    /**
     * @return Returns the code.
     */
    @AuraEnabled
    public String getCode() {
        return this.code;
    }

    /**
     * @return Returns the format.
     */
    @AuraEnabled
    public String getFormat() {
        return this.format;
    }
}

@@ -1712,24 +1712,16 @@
 ase());%0A
-        
 %0A       
@@ -1725,62 +1725,94 @@
     
-DefDescriptor%3C? extends Definition%3E descriptor = null;
+// TODO descriptor for js files in libraries needs to be changed to include its parent
 %0A   
@@ -1828,17 +1828,17 @@
 defType 
-!
+=
 = DefTyp
@@ -1866,518 +1866,93 @@
     
-// Nominal case:%0A            descriptor = Aura.getDefinitionService().getDefDescriptor(desc, defType.getPrimaryInterface());%0A        %7D else %7B%0A            // Include case: since included .js files load source differently we have to ma
+code = null;%0A            format = 
 nu
-a
 ll
-y%0A            // look up the include defs. If there is a usecase for looking up the defs in a non-doc %0A            // setting, a lookup method should be added to the includeDef
+;%0A        %7D else %7B%0A            // Nominal
  c
-l
 as
-s.%0A            String name = (String) component.getAttributes().getValue(%22includeDefName%22);
+e:
 %0A   
@@ -1988,27 +1988,30 @@
 nds 
-LibraryDef%3E library
+Definition%3E descriptor
  = A
@@ -2058,312 +2058,50 @@
 tor(
-%0A                desc, LibraryDef.class%0A            );%0A            %0A            for (IncludeDefRef includeDef : library.getDef().getIncludes()) %7B%0A               if (includeDef.getName().equals(name)) %7B%0A                   descriptor = includeDef.getDescriptor();%0A               %7D%0A            %7D%0A        %7D%0A%0A
+desc, defType.getPrimaryInterface());%0A    
     
@@ -2171,16 +2171,20 @@
 iptor);%0A
+    
         
@@ -2232,24 +2232,28 @@
             
+    
 code = sourc
@@ -2273,32 +2273,36 @@
 ();%0A            
+   
+ 
 format = String.
@@ -2330,32 +2330,36 @@
 mat());%0A        
+    
 %7D else %7B%0A       
@@ -2355,32 +2355,36 @@
 e %7B%0A            
+  
+  
 code = null;%0A   
@@ -2372,32 +2372,36 @@
    code = null;%0A
+    
             form
@@ -2403,32 +2403,46 @@
  format = null;%0A
+            %7D%0A
         %7D%0A    %7D%0A

fix blank aura docs pages @bug W-2517763@