//
//  StaticTemplate.java
//  blogwt
//
//  Created by William Shakour (billy1380) on 18 Sep 2015.
//  Copyright © 2015 WillShex Limited. All rights reserved.
//
package com.willshex.blogwt.server.page;

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

import org.markdown4j.server.IncludePlugin;
import org.markdown4j.server.MarkdownProcessor;

import com.willshex.blogwt.shared.api.Request;
import com.willshex.blogwt.shared.api.datatype.Page;
import com.willshex.blogwt.shared.page.Stack;

/**
 * @author William Shakour (billy1380)
 *
 */
abstract class StaticTemplate implements PageMarkup {

	public static final String ACCESS_CODE = "ded02740-5e12-11e5-b0c2-7054d251af02";

	protected Stack stack;

	protected Page home;
	protected List<Page> headerPages;

	private MarkdownProcessor processor;

	public StaticTemplate (Stack stack) {
		this.stack = stack;

		processor = new MarkdownProcessor();
		processor.registerPlugins(new IncludePlugin());
	}

	protected String process (String markdown) {
		String processed = "";
		try {
			processed = processor.process(markdown);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}

		return processed;
	}

	protected <T extends Request> T input (Class<T> type) {
		T input = null;
		try {
			input = type.newInstance();
		} catch (InstantiationException | IllegalAccessException e) {
			throw new RuntimeException(e);
		}

		input.accessCode = ACCESS_CODE;

		return input;
	}

	protected void appendHeader (StringBuffer markup) {
		markup.append("<html><head><link rel=\"alternate\" type=\"application/rss+xml\" title=\"");
		markup.append("{title}");
		markup.append("(RSS feed)\" href=\"/feed\"><link rel=\"icon\" href=\"");
		markup.append("{favicon}");
		markup.append("\" type=\"image/x-icon\"><title>");
		markup.append("{title}");
		markup.append("</title><body>");
		markup.append("<h1>");
		markup.append("{title}");
		markup.append("</h1>");
	}

	protected void appendFooter (StringBuffer markup) {
		markup.append("<footer class=\"footer\"> <div class=\"container\">Copyright &copy; <a href=\"");
		markup.append("{copyright}");
		markup.append("\" target=\"_blank\">");
		markup.append("{holder}");
		markup.append("</a> ");
		markup.append(Calendar.getInstance().get(Calendar.YEAR));
		markup.append(". All rights reserved - ");
		markup.append("{title}");
		markup.append(".</div> </footer></div></div></body></html>");
	}

	public String asString () {
		StringBuffer markup = new StringBuffer();

		appendHeader(markup);

		appendPage(markup);

		appendFooter(markup);

		return markup.toString();
	}

	protected abstract void appendPage (StringBuffer markup);

}

@@ -370,16 +370,92 @@
 essor;%0A%0A
+import com.willshex.blogwt.server.service.property.PropertyServiceProvider;%0A
 import c
@@ -546,16 +546,73 @@
 e.Page;%0A
+import com.willshex.blogwt.shared.helper.PropertyHelper;%0A
 import c
@@ -1646,32 +1646,254 @@
 uffer markup) %7B%0A
+%09%09String title = PropertyServiceProvider.provide().getNamedProperty(%0A%09%09%09%09PropertyHelper.TITLE).value;%0A%09%09String extendedTitle = PropertyServiceProvider.provide()%0A%09%09%09%09.getNamedProperty(PropertyHelper.EXTENDED_TITLE).value;%0A%0A
 %09%09markup.append(
@@ -1982,33 +1982,29 @@
 rkup.append(
-%22%7B
 title
-%7D%22
 );%0A%09%09markup.
@@ -2164,33 +2164,29 @@
 rkup.append(
-%22%7B
 title
-%7D%22
 );%0A%09%09markup.
@@ -2248,33 +2248,112 @@
 rkup.append(
-%22%7Bt
+title);%0A%09%09markup.append(%22%3C/h1%3E%22);%0A%09%09markup.append(%22%3Ch2%3E%22);%0A%09%09markup.append(extendedT
 itle
-%7D%22
 );%0A%09%09markup.
@@ -2359,25 +2359,25 @@
 .append(%22%3C/h
-1
+2
 %3E%22);%0A%09%7D%0A%0A%09pr
@@ -2426,16 +2426,357 @@
 rkup) %7B%0A
+%09%09String copyright = PropertyServiceProvider.provide().getNamedProperty(%0A%09%09%09%09PropertyHelper.COPYRIGHT_URL).value;%0A%09%09String copyrightHolder = PropertyServiceProvider.provide()%0A%09%09%09%09.getNamedProperty(PropertyHelper.COPYRIGHT_HOLDER).value;%0A%0A%09%09String title = PropertyServiceProvider.provide().getNamedProperty(%0A%09%09%09%09PropertyHelper.TITLE).value;%0A%0A
 %09%09markup
@@ -2882,18 +2882,16 @@
 .append(
-%22%7B
 copyrigh
@@ -2891,18 +2891,16 @@
 opyright
-%7D%22
 );%0A%09%09mar
@@ -2956,18 +2956,23 @@
 end(
-%22%7Bh
+copyrightH
 older
-%7D%22
 );%0A%09
@@ -3121,17 +3121,13 @@
 end(
-%22%7B
 title
-%7D%22
 );%0A%09

resolve properties in header and footer