package jwebform.element.structure;

import java.util.ArrayList;
import java.util.List;
import jwebform.element.SelectType;

/**
 * Common selects. Like gender, months...
 * @author jochen
 *
 */
public class CommonSelects {
	
    private CommonSelects() {}
  
	public static CommonSelects build() {
		return new CommonSelects();
	}

	static String months [] = {"january", "february", "march", "april", "may", "june", "july"
			, "august", "september", "october", "november", "december"};

	
	public List<SelectType.SelectInputEntry> buildDays() { 
		return buildMap(1, 31, "jformchecker.select.day");
	}

	public  List<SelectType.SelectInputEntry> buildMonths() { 
	  List<SelectType.SelectInputEntry> monthsList = new ArrayList<>();
		for (int i=1; i<=12; i++) {
		  monthsList.add(new SelectType.SelectInputEntry(Integer.toString(i), "jformchecker.select."+ months[i-1]));
		}
		return monthsList;
	}

	public List<SelectType.SelectInputEntry>  getYears(int startYear, int endYear) { 
		return builReverseMap(startYear, endYear, "jformchecker.select.year");
	}

	
	public List<SelectType.SelectInputEntry> getGenderSelect() { 
      List<SelectType.SelectInputEntry> genderSelect = new ArrayList<>();
      addToList(genderSelect, "f", "jformchecker.select.female");
      addToList(genderSelect, "m", "jformchecker.select.male");
		return genderSelect;
	}
	
	
	private List<SelectType.SelectInputEntry> buildMap(int start, int end, String first) {
      List<SelectType.SelectInputEntry> dayMap = new ArrayList<>();
      addToList(dayMap, "", first);
		for (int i = start; i <= end; i++) {
		      addToList(dayMap, Integer.toString(i), Integer.toString(i));
		}
		return dayMap;
	}

	private List<SelectType.SelectInputEntry> builReverseMap(int start, int end, String first) {
	  List<SelectType.SelectInputEntry> entries = new ArrayList<>();
	  entries.add(new SelectType.SelectInputEntry("", first));
		for (int i = start; i >= end; i--) {
	      entries.add(new SelectType.SelectInputEntry(Integer.toString(i), Integer.toString(i)));
		}
		return entries;
	}
	
	private void addToList(List<SelectType.SelectInputEntry> list, String key, String value) {
	  list.add(new SelectType.SelectInputEntry(key, value));
	}

}

@@ -77,24 +77,25 @@
 .util.List;%0A
+%0A
 import jwebf
@@ -978,24 +978,136 @@
 endYear) %7B %0A
+    if (startYear %3C endYear) %7B%0A      int tmp = startYear;%0A      startYear = endYear;%0A      endYear = tmp;%0A    %7D%0A
 %09%09return bui

allow both smaller and bigger values for year inputs