package org.jerrymouse.chineseCalendar.controller;

import java.util.Calendar;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.jerrymouse.chineseCalendar.model.birthday.Birthday;
import org.jerrymouse.chineseCalendar.service.BirthdayService;
import org.jerrymouse.chineseCalendar.service.SiteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
@Component("birthdaysAction")
public class BirthdaysAction extends ActionSupport {
	Log logger = LogFactory.getLog(BirthdaysAction.class);

	@Autowired
	BirthdayService birthdayService;

	@Autowired
	SiteService siteService;

	String data;

	List<List<Birthday>> birthdays;

	String feedURL;

	int month;

	private int getCurrentMonth() {
		Calendar calendar = Calendar.getInstance();
		return calendar.get(Calendar.MONTH);
	}

	@Override
	@Action(value = "/birthdays", results = { @Result(name = ActionSupport.SUCCESS, type = "freemarker", location = "/WEB-INF/freemarker/birthdays.ftl") })
	public String execute() throws Exception {
		logger.trace("data:" + data);
		logger.trace("birthdayService:" + birthdayService);
		logger.trace("siteService:" + siteService);

		List<Birthday> birthdaySource = birthdayService.decode(data);
		birthdays = birthdayService.calculateOneYear(birthdaySource);
		feedURL = siteService.encodeFeedURL(birthdaySource);
		month = getCurrentMonth();
		logger.trace("feedURL:" + feedURL);
		logger.trace(siteService.encodeFeedURL(birthdaySource));
		return super.execute();
	}

	public Log getLogger() {
		return logger;
	}

	public void setLogger(Log logger) {
		this.logger = logger;
	}

	public BirthdayService getBirthdayService() {
		return birthdayService;
	}

	public void setBirthdayService(BirthdayService birthdayService) {
		this.birthdayService = birthdayService;
	}

	public SiteService getSiteService() {
		return siteService;
	}

	public void setSiteService(SiteService siteService) {
		this.siteService = siteService;
	}

	public String getData() {
		return data;
	}

	public void setData(String data) {
		this.data = data;
	}

	public List<List<Birthday>> getBirthdays() {
		return birthdays;
	}

	public void setBirthdays(List<List<Birthday>> birthdays) {
		this.birthdays = birthdays;
	}

	public String getFeedURL() {
		return feedURL;
	}

	public void setFeedURL(String feedURL) {
		this.feedURL = feedURL;
	}

	public int getMonth() {
		return month;
	}

	public void setMonth(int month) {
		this.month = month;
	}
}
