//
//  StyledImageCell.java
//  blogwt
//
//  Created by William Shakour (billy1380) on 11 Aug 2015.
//  Copyright © 2015 WillShex Limited. All rights reserved.
//
package com.willshex.blogwt.client.cell;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.cell.client.AbstractCell;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.safehtml.shared.SafeHtmlUtils;

/**
 * @author William Shakour (billy1380)
 *
 */
public class StyledImageCell extends AbstractCell<String> {

	private List<String> classNames = null;
	private Double width, height;

	public StyledImageCell addClassName (String className) {
		if (className != null && className.length() > 0) {
			ensureClassNames();
			classNames.add(className);
		}

		return this;
	}

	public StyledImageCell () {}

	public StyledImageCell (double width, double height) {
		this.width = Double.valueOf(width);
		this.height = Double.valueOf(height);
	}

	private List<String> ensureClassNames () {
		if (classNames == null) {
			classNames = new ArrayList<String>();
		}

		return classNames;
	}

	@Override
	public void render (Context context, String value, SafeHtmlBuilder sb) {
		if (value != null) {
			sb.appendHtmlConstant("<img ");

			if (classNames != null) {
				sb.appendHtmlConstant("class=\"");

				boolean first = true;
				for (String className : classNames) {
					if (first) {
						first = false;
					} else {
						sb.appendHtmlConstant(" ");
					}

					sb.appendEscaped(className);
				}
				sb.appendHtmlConstant("\" ");
			}

			if (width != null) {
				sb.appendHtmlConstant("width=\"");
				sb.appendEscaped(width.toString());
				sb.appendEscaped("px");
				sb.appendHtmlConstant("\" ");
			}

			if (height != null) {
				sb.appendHtmlConstant("height=\"");
				sb.appendEscaped(height.toString());
				sb.appendEscaped("px");
				sb.appendHtmlConstant("\" ");
			}

			sb.appendHtmlConstant("src=\"");
			sb.append(SafeHtmlUtils.fromString(value));
			sb.appendHtmlConstant("\">");
		}
	}
}

@@ -1544,24 +1544,84 @@
 %22 %22);%0A%09%09%09%7D%0A%0A
+%09%09%09sb.appendHtmlConstant(%22style=%5C%22object-fit: cover;%5C%22 %22);%0A%0A
 %09%09%09if (width
@@ -2075,11 +2075,211 @@
 ;%0A%09%09%7D%0A%09%7D
+%0A%0A%09public StyledImageCell width (double value) %7B%0A%09%09width = Double.valueOf(value);%0A%09%09return this;%0A%09%7D%0A%0A%09public StyledImageCell height (double value) %7B%0A%09%09height = Double.valueOf(value);%0A%09%09return this;%0A%09%7D
 %0A%7D%0A

set width and height separately