package me.chyxion.tigon.webmvc.test.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import me.chyxion.tigon.model.ListResult;
import me.chyxion.tigon.model.ViewModel;
import me.chyxion.tigon.model.ViewModelable;
import org.springframework.stereotype.Controller;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * @version 0.0.1
 * @since 0.0.1
 * @author chyxion <br>
 * chyxion@163.com <br>
 * May 31, 2015 3:53:04 PM
 */
@Controller
public class SiteController {

	@RequestMapping("/")
	public void index() {
		
	}

	@RequestMapping("/raw")
	public Map<String, Object> raw() {
		Map<String, Object> mapRaw = new HashMap<String, Object>();
		mapRaw.put("data", "Hello!");
		return mapRaw;
	}

	@RequestMapping("/int")
	public int integer() {
		return 99;
	}

	@RequestMapping("/test")
	public ModelAndView test() {
		return new ModelAndView("test").addObject("name", "FreeMarker");
	}

	@RequestMapping("/view-models")
	public List<ViewModel<Integer>> viewModels() {
		return Arrays.asList(
			new ViewModel<Integer>(1)
				.setAttr("name", "foo"),
			new ViewModel<Integer>(2)
				.setAttr("name", "bar")
                .setAttr("other",
                    new ViewModel<Integer>(3)
                        .setAttr("name", "foobar")));
	}

	@RequestMapping("/list-result")
	public ViewModelable<List<Integer>> listResult() {
		return new ListResult<Integer>().setTotal(10).setData(Arrays.asList(1, 2, 3)).setAttr("attr", "foo");
	}

	@RequestMapping("/list-result1")
	public List<ViewModelable<List<Integer>>> listResult1() {
		return Arrays.asList(
			new ListResult<Integer>()
				.setTotal(10)
				.setData(Arrays.asList(1, 2, 3))
				.setAttr("attr", "foo"));
	}

	@RequestMapping("/post")
	public String post(@Validated Form form) {
		return form.getId();
	}
	
	public static class Form {
		@NotBlank
		private String id;
		@NotBlank
        private String name;
		private String gender;

		/**
		 * @return the id
		 */
		public String getId() {
			return id;
		}

		/**
		 * @param id the id to set
		 */
		public void setId(String id) {
			this.id = id;
		}

		/**
		 * @return the name
		 */
		public String getName() {
			return name;
		}

		/**
		 * @param name the name to set
		 */
		public void setName(String name) {
			this.name = name;
		}

		/**
		 * @return the gender
		 */
		public String getGender() {
			return gender;
		}

		/**
		 * @param gender the gender to set
		 */
		public void setGender(String gender) {
			this.gender = gender;
		}
	}
}
