package org.gcube.portlets.widgets.dataminermanagerwidget.client.custom.progress;

import com.google.gwt.core.client.GWT;
import com.google.gwt.resources.client.ClientBundle;
import com.google.gwt.resources.client.CssResource;
import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.resources.client.ImageResource.ImageOptions;
import com.google.gwt.resources.client.ImageResource.RepeatStyle;
import com.google.gwt.safecss.shared.SafeStyles;
import com.google.gwt.safecss.shared.SafeStylesUtils;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.safehtml.shared.SafeHtmlUtils;
import com.sencha.gxt.cell.core.client.ProgressBarCell.ProgressBarAppearance;
import com.sencha.gxt.cell.core.client.ProgressBarCell.ProgressBarAppearanceOptions;
import com.sencha.gxt.core.client.GXT;
import com.sencha.gxt.core.client.XTemplates;
import com.sencha.gxt.core.client.util.Format;

/**
 * 
 * @author Giancarlo Panichi
 *
 *
 */
public class RedProgressBarAppearance implements ProgressBarAppearance {

	public interface RedProgressBarResources {

		ImageResource barRed();

		ImageResource innerBarRed();

		RedProgressBarStyle style();

	}

	public interface RedProgressBarStyle extends CssResource {

		String progressBarRed();

		String progressInnerRed();

		String progressTextRed();

		String progressTextBackRed();

		String progressWrapRed();

	}

	public interface RedProgressBarTemplate extends XTemplates {

		@XTemplate(source = "RedProgressBar.html")
		SafeHtml render(SafeHtml text, RedProgressBarStyle style,
				SafeStyles wrapStyles, SafeStyles progressBarStyles,
				SafeStyles progressTextStyles, SafeStyles widthStyles);

	}

	public interface RedProgressBarDefaultResources extends
			RedProgressBarResources, ClientBundle {

		@Source({ "RedProgressBar.css" })
		@Override
		RedProgressBarStyle style();

		@Source("red-progress-bg.gif")
		@ImageOptions(repeatStyle = RepeatStyle.Horizontal)
		@Override
		ImageResource barRed();

		@Source("red-bg.gif")
		@ImageOptions(repeatStyle = RepeatStyle.Horizontal)
		@Override
		ImageResource innerBarRed();
	}

	private final RedProgressBarStyle style;
	private RedProgressBarTemplate template;

	
	public RedProgressBarAppearance() {
		this(
				GWT.<RedProgressBarDefaultResources> create(RedProgressBarDefaultResources.class),
				GWT.<RedProgressBarTemplate> create(RedProgressBarTemplate.class));
	}

	public RedProgressBarAppearance(RedProgressBarResources resources,
			RedProgressBarTemplate template) {
		this.style = resources.style();
		this.style.ensureInjected();
		this.template = template;
	}

	@Override
	public void render(SafeHtmlBuilder sb, Double value,
			ProgressBarAppearanceOptions options) {
		value = value == null ? 0 : value;
		double valueWidth = value * options.getWidth();

		int vw = new Double(valueWidth).intValue();

		String text = options.getProgressText();

		if (text != null) {
			int v = (int) Math.round(value * 100);
			text = Format.substitute(text, v);
		}

		SafeHtml txt;
		if (text == null) {
			txt = SafeHtmlUtils.fromSafeConstant("&#160;");
		} else {
			txt = SafeHtmlUtils.fromString(text);
		}

		int adj = GXT.isIE() ? 4 : 2;

		SafeStyles wrapStyles = SafeStylesUtils.fromTrustedString("width:"
				+ (options.getWidth() - adj) + "px;");
		SafeStyles progressBarStyles = SafeStylesUtils
				.fromTrustedString("width:" + vw + "px;");
		SafeStyles progressTextStyles = SafeStylesUtils
				.fromTrustedString("width:" + Math.max(vw - 8, 0) + "px;");
		SafeStyles widthStyles = SafeStylesUtils.fromTrustedString("width:"
				+ (Math.max(0, options.getWidth() - adj)) + "px;");
		sb.append(template.render(txt, style, wrapStyles, progressBarStyles,
				progressTextStyles, widthStyles));
	}

}
