// Copyright (C) 2003-2009 by Object Mentor, Inc. All rights reserved.
// Released under the terms of the CPL Common Public License version 1.0.
package fitnesse.responders;

import fitnesse.FitNesseContext;
import fitnesse.authentication.SecureOperation;
import fitnesse.authentication.SecureReadOperation;
import fitnesse.authentication.SecureResponder;
import fitnesse.html.HtmlUtil;
import fitnesse.http.Request;
import fitnesse.http.Response;
import fitnesse.http.SimpleResponse;
import fitnesse.responders.editing.EditResponder;
import fitnesse.responders.templateUtilities.HtmlPage;
import fitnesse.responders.templateUtilities.PageTitle;
import fitnesse.testsystems.TestPage;
import fitnesse.testsystems.TestPageWithSuiteSetUpAndTearDown;
import fitnesse.wiki.*;

public class WikiPageResponder implements SecureResponder {
  private WikiPage page;

  public Response makeResponse(FitNesseContext context, Request request) {
    loadPage(request.getResource(), context);
    if (page == null)
      return notFoundResponse(context, request);
    else
      return makePageResponse(context);
  }

  protected void loadPage(String pageName, FitNesseContext context) {
    WikiPagePath path = PathParser.parse(pageName);
    if (RecentChanges.RECENT_CHANGES.equals(path)) {
      page = context.recentChanges.toWikiPage(context.root);
    } else {
      PageCrawler crawler = context.root.getPageCrawler();
      page = crawler.getPage(context.root, path);
    }
  }

  private Response notFoundResponse(FitNesseContext context, Request request) {
    if (dontCreateNonExistentPage(request))
      return new NotFoundResponder().makeResponse(context, request);
    return new EditResponder().makeResponseForNonExistentPage(context, request);
  }

  private boolean dontCreateNonExistentPage(Request request) {
    String dontCreate = (String) request.getInput("dontCreatePage");
    return dontCreate != null && (dontCreate.length() == 0 || Boolean.parseBoolean(dontCreate));
  }

  private SimpleResponse makePageResponse(FitNesseContext context) {
      String html = makeHtml(context);

      SimpleResponse response = new SimpleResponse();
      response.setMaxAge(0);
      response.setContent(html);
      return response;
  }

  public String makeHtml(FitNesseContext context) {
    PageData pageData = page.getData();
    WikiPage page = pageData.getWikiPage();
    HtmlPage html = context.pageFactory.newPage();
    WikiPagePath fullPath = page.getPageCrawler().getFullPath(page);
    String fullPathName = PathParser.render(fullPath);
    PageTitle pt = new PageTitle(fullPath);
    
    String tags = "";
    if (pageData != null) {
      tags = pageData.getAttribute(PageData.PropertySUITES);
    }
    pt.setPageTags(tags);
    
    html.setTitle(fullPathName);
    html.setPageTitle(pt.notLinked());
    
    html.setNavTemplate("wikiNav.vm");
    html.put("actions", new WikiPageActions(page));
    html.put("helpText", pageData.getProperties().get(PageData.PropertyHELP));

    if (TestPage.isTestPage(pageData)) {
      TestPage testPage = new TestPageWithSuiteSetUpAndTearDown(page);
      html.put("content", new WikiPageRenderer(testPage.getDecoratedData()));
    } else {
      html.put("content", new WikiPageRenderer(page.getData()));
    }

    html.setMainTemplate("wikiPage");
    html.setFooterTemplate("wikiFooter");
    html.put("footerContent", new WikiPageFooterRenderer());
    handleSpecialProperties(html, page);
    return html.html();
  }

  private void handleSpecialProperties(HtmlPage html, WikiPage page) {
    WikiImportProperty.handleImportProperties(html, page);
  }

  public SecureOperation getSecureOperation() {
    return new SecureReadOperation();
  }

  public class WikiPageRenderer {
    private PageData data;
    WikiPageRenderer(PageData data) {
      this.data = data;
    }
    public String render() {
        return HtmlUtil.makePageHtml(data);
    }
  }

  public class WikiPageFooterRenderer {
    public String render() {
        return HtmlUtil.makePageFooterHtml(page.getData());
    }
  }

}

@@ -828,33 +828,8 @@
 er %7B
-%0A  private WikiPage page;
 %0A%0A  
@@ -905,25 +905,50 @@
 ) %7B%0A    
-loadPage(
+WikiPage page = loadPage(context, 
 request.
@@ -964,17 +964,8 @@
 ce()
-, context
 );%0A 
@@ -1080,16 +1080,22 @@
 (context
+, page
 );%0A  %7D%0A%0A
@@ -1106,20 +1106,24 @@
 otected 
-void
+WikiPage
  loadPag
@@ -1128,25 +1128,8 @@
 age(
-String pageName, 
 FitN
@@ -1139,32 +1139,49 @@
 eContext context
+, String pageName
 ) %7B%0A    WikiPage
@@ -1216,24 +1216,43 @@
 (pageName);%0A
+    WikiPage page;%0A
     if (Rece
@@ -1473,32 +1473,49 @@
 t, path);%0A    %7D%0A
+    return page;%0A
   %7D%0A%0A  private R
@@ -2073,32 +2073,47 @@
 eContext context
+, WikiPage page
 ) %7B%0A      String
@@ -2136,16 +2136,22 @@
 (context
+, page
 );%0A%0A    
@@ -2338,16 +2338,31 @@
  context
+, WikiPage page
 ) %7B%0A    
@@ -2401,52 +2401,8 @@
 ();%0A
-    WikiPage page = pageData.getWikiPage();%0A
     
@@ -3429,16 +3429,24 @@
 enderer(
+pageData
 ));%0A    
@@ -3794,24 +3794,25 @@
 eData data;%0A
+%0A
     WikiPage
@@ -3863,24 +3863,25 @@
 data;%0A    %7D%0A
+%0A
     public S
@@ -3988,24 +3988,127 @@
 rRenderer %7B%0A
+    private PageData data;%0A%0A    WikiPageFooterRenderer(PageData data) %7B%0A      this.data = data;%0A    %7D%0A%0A
     public S
@@ -4167,30 +4167,20 @@
 terHtml(
-page.getD
+d
 ata
-()
 );%0A    %7D

Refactor WikiPageResponder.