package tvark.beans;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.faces.model.SelectItem;

import tvark.jsf.JsfUtils;
import tvark.model.Lesson;

public class UtilsBean {
	
	private Map<Object, List<?>> mAsList = new HashMap<Object, List<?>>() {

		private static final long serialVersionUID = -5295535901468946129L;

		@Override
		public List<?> get(Object pKey) {
			if (pKey == null) {
				return Collections.EMPTY_LIST;
			}
			if (pKey instanceof Object[]) {
				List<Object> result = new ArrayList<Object>();
				for (Object o : (Object []) pKey) {
					if (o != null) {
						result.add(o);
					}					
				}
				return result;
			}
			throw new IllegalArgumentException();			
		}
				
	};
	
	private Map<List<String>, List<SelectItem>> mItems = new HashMap<List<String>, List<SelectItem>>() {

		private static final long serialVersionUID = -6295791856677217519L;

		@Override
		public List<SelectItem> get(Object pKey) {
			List<?> strings = (List<?>) pKey;
			List<SelectItem> result = new ArrayList<SelectItem>();
			for (Object o : strings) {
				String s = (String) o;
				result.add(new SelectItem(s, s));
			}
			return result;
		}};
	
	private Map<Lesson, String> mLessonTitle = new HashMap<Lesson, String>() {

		private static final long serialVersionUID = -3329323298244455355L;

		@Override
		public String get(Object pKey) {
			Lesson l = (Lesson) pKey;
			if (l == null) {
				return "";
			}
			return JsfUtils.format("formatLesson",
					JsfUtils.format("day" + l.getDay()),
					l.getLesson() + 1,
					l.getSubject().getSubjCode(),
					l.getTeacher().getName(),
					l.getGroup(),
					l.getRoom()
			);
		}};
		
	public Map<Object, List<?>> getAsList() {
		return mAsList;
	}
	
	public Map<List<String>, List<SelectItem>> getItems() {
		return mItems;
	}
	
	public Map<Lesson, String> getLessonTitle() {
		return mLessonTitle;
	}
}
