//
//  FooterPart.java
//  blogwt
//
//  Created by billy1380 on 31 Jul 2013.
//  Copyright © 2015 WillShex Limited. All rights reserved.
//
package com.willshex.blogwt.client.part;

import java.util.Date;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.DivElement;
import com.google.gwt.safehtml.client.SafeHtmlTemplates;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.safehtml.shared.SafeUri;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.willshex.blogwt.client.Resources;
import com.willshex.blogwt.client.controller.PropertyController;
import com.willshex.blogwt.client.helper.ApiHelper;
import com.willshex.blogwt.shared.helper.PropertyHelper;

public class FooterPart extends Composite {

	private static FooterPartUiBinder uiBinder = GWT
			.create(FooterPartUiBinder.class);

	interface FooterPartUiBinder extends UiBinder<Widget, FooterPart> {}

	public interface FooterTemplates extends SafeHtmlTemplates {
		FooterTemplates FOOTER_TEMPLATES = GWT.create(FooterTemplates.class);

		@Template("{5}Copyright &copy; <a href=\"{0}\" class=\"{4}\" target=\"_blank\" rel=\"noopener\">{1}</a> {2}. All rights reserved - {3}.")
		SafeHtml copyrightNotice (SafeUri uri, String holder, String years,
				String name, String styleNames, String version);

		@Template("{4}Copyright &copy; <a href=\"{0}\">{1}</a> {2}. All rights reserved - {3}.")
		SafeHtml copyrightNoticeInternal (SafeUri uri, String holder,
				String years, String name, String version);
	}

	@UiField DivElement divCopyright;
	@UiField BackToTop btnBackToTop;

	private FooterPart () {
		initWidget(uiBinder.createAndBindUi(this));

		PropertyController controller = PropertyController.get();

		SafeUri url = controller.copyrightHolderUrl();
		String urlAsString = url.asString();
		if (urlAsString.contains(ApiHelper.HOST)
				|| urlAsString.startsWith("#")) {
			divCopyright.setInnerSafeHtml(FooterTemplates.FOOTER_TEMPLATES
					.copyrightNoticeInternal(url, controller.copyrightHolder(),
							years(controller), controller.title(), version()));
		} else {
			divCopyright.setInnerSafeHtml(FooterTemplates.FOOTER_TEMPLATES
					.copyrightNotice(url, controller.copyrightHolder(),
							years(controller), controller.title(),
							Resources.RES.styles().externalLink(), version()));
		}
	}

	private String version () {
		return PropertyController.get()
				.booleanProperty(PropertyHelper.FOOTER_SHOW_VERSION, false)
						? (PropertyController.get()
								.stringProperty(PropertyHelper.VERSION) + " - ")
						: "";
	}

	@SuppressWarnings("deprecation")
	private String years (PropertyController controller) {
		Date started = controller.started();
		Date now = new Date();

		if (started.getYear() == now.getYear()) {
			return Integer.toString(1900 + now.getYear());
		} else {
			return Integer.toString(1900 + started.getYear()) + "-"
					+ Integer.toString(1900 + now.getYear());
		}
	}

	private static FooterPart one;

	/**
	 * @return
	 */
	public static FooterPart get () {
		if (one == null) {
			one = new FooterPart();
		}

		return one;
	}

	public void scrollToTop () {
		btnBackToTop.go();
	}

}

@@ -2495,22 +2495,32 @@
  () %7B%0A%09%09
-return
+String version =
  Propert
@@ -2686,16 +2686,8 @@
 ION)
- + %22 - %22
 )%0A%09%09
@@ -2694,16 +2694,162 @@
 %09%09%09%09: %22%22
+;%0A%0A%09%09if (version.length() %3E 7) %7B%0A%09%09%09version = version.substring(0, 7);%0A%09%09%7D%0A%0A%09%09if (!version.isEmpty()) %7B%0A%09%09%09version += %22 - %22;%0A%09%09%7D%0A%0A%09%09return version
 ;%0A%09%7D%0A%0A%09@

truncate displayed version if more than 7 characters long