package org.jerrymouse.chineseCalendar.model.birthday;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Component;

@Component
public class BirthdayEncoder {
	public List<Birthday> decode(String code) {
		List<Birthday> birthdays = new ArrayList<Birthday>();
		try {
			if (code != null) {
				code = URLDecoder.decode(code, "utf-8");
				for (String str : code.split(",")) {
					birthdays.add(new Birthday(str));
				}
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return birthdays;
	}

	public String encode(List<Birthday> birthdays) {
		StringBuilder arg = new StringBuilder();
		for (Birthday birthday : birthdays) {
			String temp = birthday.toString();
			arg.append(temp + ",");
		}
		String temp = null;
		try {
			temp = URLEncoder.encode(arg.toString(), "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return temp;
	}
}
