package models;

import java.util.LinkedList;
import java.util.List;

import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

import utils.StringUtil;

@Entity
public class SlideContentComponent extends ModelUuid {
	public static enum ComponentType {
		BUTTON, CLICK_AREA, IMAGE, VIDEO, TEXT, RANGE, UPLOADS, CHECKABLETABLE,
		TEXTAREA, RADIO, CHECKBOX, TEXTBLOCK, AUDIO, TXTFILE;

		public static ComponentType tryBuildComponentTypeFromValue(String val) {
			if (StringUtil.isNil(val)) {
				return null;
			}

			for (ComponentType componentType : values()) {
				if (componentType.name().equals(val)) {
					return componentType;
				}
			}

			return null;
		}
	}

	@Basic
	@Enumerated(EnumType.STRING)
	private ComponentType componentType;

	@Basic
	@ManyToOne(cascade = { CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH })
	private Media media;

	@Basic
	private String pos;

	@Basic
	private String dim;

	@Basic
	@ManyToOne(cascade = { CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH })
	private Slide slide;

	@Basic
	@ManyToOne(cascade = { CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH })
	private Test test;

	@OneToMany(cascade = { CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH })
	private List<Option> options = new LinkedList<Option>();

	public ComponentType getComponentType() {
		return componentType;
	}

	public void setComponentType(ComponentType componentType) {
		this.componentType = componentType;
	}

	public Media getMedia() {
		return media;
	}

	public void setMedia(Media media) {
		this.media = media;
	}

	public String getPos() {
		return pos;
	}

	public void setPos(String pos) {
		this.pos = pos;
	}

	public String getDim() {
		return dim;
	}

	public void setDim(String dim) {
		this.dim = dim;
	}

	public List<Option> getOptions() {
		return options;
	}

	public void setOptions(List<Option> options) {
		this.options = options;
	}

	public Slide getSlide() {
		return slide;
	}

	public void setSlide(Slide slide) {
		this.slide = slide;
	}

	public Test getTest() {
		return test;
	}

	public void setTest(Test test) {
		this.test = test;
	}

	public boolean addOption(Option option) {
		if (option == null)
			return false;
		if (this.options.contains(option)) {
			return false;
		}
		return this.options.add(option);
	}

	public static int deleteBySlide(Slide slide) {
		int dels = delete("slide =?", slide);
		return dels;
	}
	
	public static List<SlideContentComponent> findBySlide(Slide slide) {
		List<SlideContentComponent> ret = find("slide =:slide").setParameter("slide", slide).fetch();
		return ret;
	}

	public static SlideContentComponent findBySlideAndMedia(Slide slide, Media media) {
		SlideContentComponent ret = find("slide =:slide AND media=:media").setParameter("slide", slide)
				.setParameter("media", media).first();
		return ret;
	}

	public static SlideContentComponent findBySlideAndMediaId(Slide slide, String mediaId) {
		SlideContentComponent ret = find("slide =:slide AND media.uuid=:mediaId").setParameter("slide", slide)
				.setParameter("mediaId", mediaId).first();
		return ret;
	}

	public static List<SlideContentComponent> findByTest(Test test) {
		List<SlideContentComponent> ret = find("test =:test").setParameter("test", test).fetch();
		return ret;
	}

	public static List<SlideContentComponent> findByTestId(String testId) {
		List<SlideContentComponent> ret = find("test.uuid =:testId").setParameter("testId", testId).fetch();
		return ret;
	}
}