package eu.dnetlib.client.shared;

import javax.xml.bind.annotation.XmlRootElement;

import com.google.gwt.event.dom.client.MouseDownEvent;
import com.google.gwt.event.dom.client.MouseDownHandler;
import com.google.gwt.user.client.ui.FocusPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.sencha.gxt.widget.core.client.container.AbstractHtmlLayoutContainer.HtmlData;
import com.sencha.gxt.widget.core.client.container.HtmlLayoutContainer;

@XmlRootElement(name = "resourcethumb")
public class ResourceThumb {
	private final FocusPanel thumb = new FocusPanel();
	private final HtmlLayoutContainer con = new HtmlLayoutContainer(getTableMarkup());
	private final HorizontalPanel thumbPanel = new HorizontalPanel();
	private Label id;
	private boolean edit;
	private boolean published = false;
	private HTML title;
	private HTML originalTitle;
	private HTML affiliation;
	private Image image = new Image();
	private HTML locked = new HTML("<span class='glyphicon glyphicon-lock'></span>");
	private HTML publishedImage = new HTML("<span class='glyphicon glyphicon-star-empty'></span>");
	private HTML type = new HTML();
	private String imageType = "<span class='glyphicon glyphicon-camera'></span>";
	private String videoType = "<span class='glyphicon glyphicon-facetime-video'></span>";

	private RightClickMenu thumbwithRightClickMenu;

	// for items
	public ResourceThumb(String id, String title, String originalTitle, String affiliation, String miniImage) {
		this.id = new Label(id);
		this.title = new HTML(title);
		this.title.setTitle(title);
		this.type.setStyleName("typeSpan");
		this.originalTitle = new HTML(originalTitle);
		this.originalTitle.setTitle(originalTitle);

		this.affiliation = new HTML(affiliation);
		this.affiliation.setTitle(affiliation);

		this.id.setVisible(false);
		if (miniImage == null || miniImage.isEmpty()) {
			setImage("images/NewImages/item_thumb.png");

		} else {

			setImage(miniImage);
		}

		createThumb();
		setBackroundNotClicked();
	}

	// for Frames
	public ResourceThumb(String id, String title, String template) {
		this.id = new Label(id);
		this.id.setVisible(false);
		this.title = new HTML(title);
		this.title.setTitle(title);

		setFrameTemplateImage(template);

		createThumbFrame();
		setBackroundNotClicked();
	}

	public void setFrameTemplateImage(String template) {
		if (template.equals("ft2")) {
			setImage("images/NewImages/t2.png");

		} else if (template.equals("ft3")) {
			setImage("images/NewImages/t3.png");

		} else if (template.equals("ft5")) {
			setImage("images/NewImages/t5.png");

		} else {
			setImage("images/NewImages/t1.png");

		}
	}

	// for topics-themes
	public ResourceThumb(String id, String title) {
		this.id = new Label(id);
		this.title = new HTML(title);
		this.title.setTitle(title);
		this.id.setVisible(false);
		setImage("images/small1.png");
		createThumbFrame();
		setBackroundNotClicked();
	}

	// for users
	public ResourceThumb(String id, String userName, int donothing) {
		this.id = new Label(id);
		this.title = new HTML(userName);
		this.originalTitle = new HTML();
		this.affiliation = new HTML();
		this.id.setVisible(false);
		setImage("images/small1.png");
		createThumb();
		setBackroundNotClicked();
	}

	public FocusPanel getThumb() {
		return thumb;
	}

	public void createThumb() {
		con.add(title, new HtmlData(".title"));
		con.add(id);

		image.setStyleName("resourceThumbImage");
		locked.setStyleName("lockImage");
		publishedImage.setStyleName("publishedImage");
		locked.setVisible(false);
		publishedImage.setVisible(false);
		con.add(originalTitle, new HtmlData(".originalTitle"));
		con.add(affiliation, new HtmlData(".affiliation"));

		con.add(image, new HtmlData(".image"));
		con.add(publishedImage, new HtmlData(".published"));
		con.add(locked, new HtmlData(".lock"));
		con.add(type, new HtmlData(".type"));

		thumb.add(con);

	}

	public void createThumbFrame() {
		con.add(title, new HtmlData(".title"));
		image.setStyleName("resourceThumbImage");
		locked.setStyleName("lockImage");
		publishedImage.setStyleName("publishedImage");

		locked.setVisible(false);
		publishedImage.setVisible(false);
		con.add(id);

		con.add(image, new HtmlData(".image"));
		con.add(publishedImage, new HtmlData(".published"));
		con.add(locked, new HtmlData(".lock"));

		thumb.add(con);
	}

	public void setTypeForItems(String itemType) {
		if (itemType != null) {
			if (itemType.contains("video") || itemType.contains("embedded")) {
				type.setHTML(videoType);
			} else if (itemType.contains("image")) {
				type.setHTML(imageType);
			} else {
				type.setHTML("");
			}
		} else {
			type.setHTML("");
		}
	}

	public void setImage(String imageSource) {
		image.setUrl(imageSource);
		image.addMouseDownHandler(new MouseDownHandler() {
			public void onMouseDown(MouseDownEvent event) {
				event.preventDefault();
			}

		});
	}

	public String getId() {
		return id.getText();
	}

	public HTML getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title.setHTML(title);
		this.title.setTitle(title);

	}

	public HTML getOriginalTitle() {
		return originalTitle;
	}

	public void setOriginalTitle(String originalTitle) {
		this.originalTitle.setHTML(originalTitle);
		this.originalTitle.setTitle(originalTitle);

		;
	}

	public HTML getAffiliation() {
		return affiliation;
	}

	public void setAffiliation(String affiliation) {
		this.affiliation.setHTML(affiliation);
		this.affiliation.setTitle(affiliation);

	}

	public void setAffiliation(String akronym, String affiliation) {
		this.affiliation.setHTML(akronym);
		this.affiliation.setTitle(affiliation);

	}

	public String getImage() {
		return image.getUrl();
	}

	public boolean isEdit() {
		return edit;
	}

	public void setEdit(boolean edit) {
		this.edit = edit;
	}

	private native String getTableMarkup() /*-{
											
											return [ '<div class="resourceThumb">',
											'<div>',
											'<div class=type ></div>',
											'<div class=image style="float:left;"></div>',
																						

											'</div>',
											'<div class=titlesAndGlyf >',
											'<div class=titles style="float:left;">',
											' <p class=title></p>',
											'<p class=originalTitle></p>',
											'<p class=affiliation></p>',
											'</div>',
											'<div class=glyphicons style="float:right;">',
											'<div class=lock  ></div>',
											'<div class=published ></div>',
											 
											'</div>',
											'</div> ',
											'</div> '
											].join("");
											}-*/;

	public void setPublished(boolean published) {
		this.published = published;
		this.publishedImage.setVisible(published);
	}

	public void setLocked(boolean locked) {
		this.locked.setVisible(locked);
	}

	public void setBackroundClicked() {
		con.setStyleName("resourceThumbClicked");
	}

	public void setBackroundNotClicked() {
		con.setStyleName("resourceThumbNotClicked");
	}

	public void setThumbwithRightClickMenu(RightClickMenu thumbwithRightClickMenu) {
		this.thumbwithRightClickMenu = thumbwithRightClickMenu;
	}

	public RightClickMenu getThumbwithRightClickMenu() {
		return thumbwithRightClickMenu;
	}

}
