package org.jerrymouse.chineseCalendar.model.atom;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import org.jerrymouse.chineseCalendar.model.birthday.Birthday;
import org.jerrymouse.chineseCalendar.model.birthday.PeriodUpdater;
import org.jerrymouse.chineseCalendar.model.birthday.SimpleCalendar;

import com.sun.syndication.feed.synd.SyndContent;
import com.sun.syndication.feed.synd.SyndContentImpl;
import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndEntryImpl;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.feed.synd.SyndFeedImpl;
import com.sun.syndication.feed.synd.SyndLink;
import com.sun.syndication.feed.synd.SyndLinkImpl;

public class BirthdayAtom {
	private static final String KAIYAN = "kaiyan";
	private static final String DESCRIPTION = "每周更新您好友的生日情报";
	private SyndFeed feed;
	private List<SyndEntry> entries;
	private static final String FEED_TYPE = "atom_1.0";
	private static final String TITLE = "农历生日提醒";

	public BirthdayAtom(SimpleCalendar begin, String url, String selfLinkURL) {
		feed = new SyndFeedImpl();
		entries = new ArrayList<SyndEntry>();
		feed.setEntries(entries);
		feed.setAuthor(KAIYAN);
		feed.setFeedType(FEED_TYPE);
		feed.setTitle(TITLE);
		feed.setPublishedDate(begin.getTime());
		feed.setLink(url);
		feed.setUri(url);
		feed.setEncoding("utf-8");
		feed.setDescription(DESCRIPTION);
		SyndLink selfLink = new SyndLinkImpl();
		selfLink.setRel("self");
		selfLink.setHref(selfLinkURL);
		feed.setLinks(Collections.singletonList(selfLink));
	}

	public SyndFeed getFeed() {
		return feed;
	}

	public void add(BirthdayEntity birthdayEntity) {
		if (birthdayEntity.birthdays.size() == 0)
			return;
		SyndEntry entry = new SyndEntryImpl();
		SyndContent content = new SyndContentImpl();
		entry.setDescription(content);

		entry.setTitle(creatTitle(birthdayEntity.updateDate));
		entry.setLink(feed.getUri() + "&time="
				+ birthdayEntity.updateDate.getTime().getTime());
		entry.setUri(entry.getLink());
		entry.setUpdatedDate(new Date(birthdayEntity.updateDate.getTime()
				.getTime()
				- PeriodUpdater.WEEK_M));
		entry.setAuthor("kaiyan");
		content.setType("text/html");
		content.setValue(creatContent(birthdayEntity.birthdays));
		entries.add(entry);
	}

	private String creatContent(List<Birthday> birthdays) {
		String content = "";
		Collections.sort(birthdays);
		for (Birthday birthday : birthdays) {
			content += birthday.getDate().toString();
			content += ":";
			content += birthday.getName();
			content += "<br/>";
		}
		return content;
	}

	private String creatTitle(SimpleCalendar updateDate) {
		long dateEnd = updateDate.getTime().getTime() + 6 * PeriodUpdater.DAY_M;
		return "从" + dateFormate(updateDate.getTime()) + "到"
				+ dateFormate(new Date(dateEnd)) + "好友的生日";
	}

	public String dateFormate(Date date) {
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		return dateFormat.format(date);
	}

}
