/**
 * 
 */
package org.richfaces.realworld.ui;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;

/**
 * @author Andrey Markavtsov
 * 
 */
@Name("imageSizeHelper")
@Scope(ScopeType.CONVERSATION)
public class ImageSizeHelper {

	static final int DEFAULT_VALUE = 120;

	//TODO nick - why not use dimension.x?
	int value = DEFAULT_VALUE;

	public static enum ImageDimension {

		SIZE_80(80), SIZE_120(120), SIZE_160(160), SIZE_200(200);

		final static String CSS_CLASS = "preview_box_photo_";
		final static String FILE_POSTFIX = "_small";
		final static String IMAGE_BG = "/img/shell/frame_photo_%1$d.png";
		final static String IMAGE_BG_STYLE = "width: %1$dpx; height: %1$dpx";
		
		int x;
		String bgStyle;
		String cssClass;
		String imageBgSrc;
		String filePostfix;

		private ImageDimension(int x) {
			this.x = x;
			this.bgStyle = String.format(IMAGE_BG_STYLE, x + 20);
			cssClass = CSS_CLASS + x;
			//TODO nick - what if x == 160?
			imageBgSrc = String.format(IMAGE_BG, (x == 160) ? 200 : x);
			filePostfix = FILE_POSTFIX + x;

		}

		public int getX() {
			return x;
		}

		public String getCssClass() {
			return cssClass;
		}

		public String getImageBg() {
			return imageBgSrc;
		}
		
		public String getImageBgStyle() {
			return bgStyle;
		}

		public String getFilePostfix() {
			return filePostfix;
		}
		
		public static ImageDimension getInstance(int x) {
			ImageDimension [] all = values();
			for (int i=0; i<all.length; i++) {
				if (all[i].x == x) {
					return all[i];
				}
			}
			
			//TODO nick - does enum really have 120th value?
			return values()[DEFAULT_VALUE];
		}
	};
	
	ImageDimension currentDimension = ImageDimension.getInstance(DEFAULT_VALUE);
	
	public ImageDimension getCurrentDimension() {
		return currentDimension;
	}

	public int getValue() {
		return value;
	}

	public void setValue(int value) {
		currentDimension = ImageDimension.getInstance(value);
		this.value = currentDimension.x;
	}

}
