/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ecbeta.view.tag;

import com.ecbeta.common.core.viewer.bean.NavigationBean;
import static com.ecbeta.common.core.viewer.bean.NavigationBean.getNavTierID;
import java.util.List;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyContent;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 *
 * @author Ronghai Wei <ronghai.wei@outlook.com>
 */
public class NavigationTag extends AbstractTag {

    public NavigationTag() {
        super();
    }

    private List<NavigationBean> navigationBeans;
    private String bodyString;
    private int[] navTier;
    private String navPrefix = "level";

    public String getNavPrefix() {
        return navPrefix;
    }

    public void setNavPrefix(String navPrefix) {
        this.navPrefix = navPrefix;
    }

    public int[] getNavTier() {
        return navTier;
    }

    public void setNavTier(int[] navTier) {
        this.navTier = navTier;
    }
    

    public List<NavigationBean> getNavigationBeans() {
        return navigationBeans;
    }

    public void setNavigationBeans(List<NavigationBean> navigationBeans) {
        this.navigationBeans = navigationBeans;
    }

    @Override
    public int doAfterBody() throws JspException {
        BodyContent bc = this.getBodyContent();
        this.bodyString = bc.getString();
        bc.clearBody();
        return SKIP_BODY;
    }

    @Override
    public int doEndTag() throws JspException {
        int returnValue = super.doEndTag();
        this.println(this.renderHTML(this.getContextPath(), this.bodyString));
        return returnValue;
    }

    @Override
    public int doStartTag() throws JspException {
        int returnValue = super.doStartTag();
        if(this.getId() == null || "".equals(this.getId())){
            this.setId("navigationbar");
        }
        return returnValue;
    }
 

    private String render(List<NavigationBean> navigationBeans, int level) {
        if (navigationBeans == null || navigationBeans.isEmpty()) {
            return "";
        }
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < navigationBeans.size(); i++) {
            NavigationBean bean = navigationBeans.get(i);
            sb.append("<div ");
            sb.append(" data-level='").append(level).append("' ");
            sb.append(" > ");
            String url = bean.getUrl(this.getContextPath());
            sb.append("<a href='").append(url).append("' ");
            sb.append(" > ");
            sb.append(bean.getLabel()).append("</a>"); 
            if (bean.getChildren() != null) { 
                sb.append(render(bean.getChildren(), level+1)); 
            }
            sb.append("</div>");
        }
        return sb.toString();
    }

    private Object renderHTML(String contextPath, String bodyString) throws JspException {
        StringBuilder sb = new StringBuilder();
        String id = this.getId();
        
        
        
        JSONObject json = new JSONObject();
        json.put("nodes", NavigationBean.toJson(this.navigationBeans, navPrefix, contextPath, true));
        json.put("name", id);
        //json.put("onClick", "function(event){}");
        
        sb.append("<div id=\"").append(id).append("\" style=\"height: 300px; width: 180px; float: left\"></div>");
        sb.append("<div style=\"clear: both\"></div>");
        sb.append("<script type=\"text/javascript\">//<![CDATA[ \n");
        sb.append("$(document).ready(function () {\n" ); 
        sb.append("	$('#").append(id).append("').w2sidebar(");
        sb.append("             ").append(json.toString());
        sb.append("     );\n" );
        sb.append("});\n" );
        if(navTier != null){
              sb.append("w2ui.").append(id).append(".select('").append(getNavTierID(navPrefix, navTier)).append("');");
        }
        sb.append("w2ui.").append(id).append(".on('*', function (event) {\n");
        sb.append("         if(event.type === 'click'){   \n");
        sb.append("                 var url = w2ui.").append(id).append(".get(event.target)['data-url'];\n");
        sb.append("                 sa.runApp(url);\n");
        sb.append("         };\n");
        sb.append("});\n");
        sb.append("//]]>\n");
        sb.append("</script>\n");
        sb.append("<div id='navigation' > ");
        sb.append(render(this.navigationBeans, 0));
        sb.append("</div>");
        return sb.toString();
    }
    
    
   
 
}

@@ -2059,889 +2059,9 @@
     
-private String render(List%3CNavigationBean%3E navigationBeans, int level) %7B%0A        if (navigationBeans == null %7C%7C navigationBeans.isEmpty()) %7B%0A            return %22%22;%0A        %7D%0A        StringBuilder sb = new StringBuilder();%0A%0A        for (int i = 0; i %3C navigationBeans.size(); i++) %7B%0A            NavigationBean bean = navigationBeans.get(i);%0A            sb.append(%22%3Cdiv %22);%0A            sb.append(%22 data-level='%22).append(level).append(%22' %22);%0A            sb.append(%22 %3E %22);%0A            String url = bean.getUrl(this.getContextPath());%0A            sb.append(%22%3Ca href='%22).append(url).append(%22' %22);%0A            sb.append(%22 %3E %22);%0A            sb.append(bean.getLabel()).append(%22%3C/a%3E%22); %0A            if (bean.getChildren() != null) %7B %0A                sb.append(render(bean.getChildren(), level+1)); %0A            %7D%0A            sb.append(%22%3C/div%3E%22);%0A        %7D%0A        return sb.toString();%0A    %7D
+ 
 %0A%0A  
@@ -2197,23 +2197,25 @@
 lder();%0A
-
        
+//
  String 
@@ -3590,126 +3590,9 @@
     
-sb.append(%22%3Cdiv id='navigation' %3E %22);%0A        sb.append(render(this.navigationBeans, 0));%0A        sb.append(%22%3C/div%3E%22);
+ 
 %0A   

remove old codes