package org.jerrymouse.chineseCalendar.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.jerrymouse.chineseCalendar.model.atom.BirthdayEntity;
import org.jerrymouse.chineseCalendar.model.birthday.Birthday;
import org.jerrymouse.chineseCalendar.model.birthday.BirthdayEncoder;
import org.jerrymouse.chineseCalendar.model.birthday.LunarCacluator;
import org.jerrymouse.chineseCalendar.model.birthday.Period;
import org.jerrymouse.chineseCalendar.model.birthday.PeriodUpdater;
import org.jerrymouse.chineseCalendar.model.birthday.SimpleCalendar;
import org.jerrymouse.chineseCalendar.service.BirthdayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class BirthdayServiceImpl implements BirthdayService {
	@Autowired
	BirthdayEncoder birthdayEncoder;

	@Autowired
	LunarCacluator lunarCacluator;

	@Autowired
	PeriodUpdater periodUpdater;

	public List<Birthday> calculate(Birthday birthday, Period period) {
		List<Birthday> birthdaysNow = new ArrayList<Birthday>();
		SimpleCalendar lundar = lunarCacluator.toLunar(birthday.getDate());
		for (int i = period.begin.getYear() - 1; i <= period.end.getYear(); i++) {
			lundar.setYear(i);
			SimpleCalendar temp = lunarCacluator.toNormal(lundar);
			if (period.isAround(temp)) {
				birthdaysNow.add(new Birthday(birthday.getName(), temp));
			}
		}
		return birthdaysNow;
	}

	@Override
	public List<Birthday> calculate(List<Birthday> birthdays, Period period) {
		List<Birthday> birthdaysNow = new ArrayList<Birthday>();
		for (Birthday birthday : birthdays) {
			birthdaysNow.addAll(calculate(birthday, period));
		}
		return birthdaysNow;
	}

	@Override
	public List<Birthday> decode(String data) {
		return birthdayEncoder.decode(data);
	}

	@Override
	public List<BirthdayEntity> calculateNextTwoWeeks(List<Birthday> birthdays) {
		List<Birthday> birthdaysTheWeek = calculate(birthdays, periodUpdater
				.getThisWeek());
		List<Birthday> birthdaysNextWeek = calculate(birthdays, periodUpdater
				.getNextWeek());
		List<BirthdayEntity> birthdayEntitys = new ArrayList<BirthdayEntity>();

		birthdayEntitys.add(new BirthdayEntity(
				periodUpdater.getThisWeek().begin, birthdaysTheWeek));
		birthdayEntitys.add(new BirthdayEntity(
				periodUpdater.getNextWeek().begin, birthdaysNextWeek));
		return birthdayEntitys;
	}

	@Override
	public List<List<Birthday>> calculateOneYear(List<Birthday> birthdays) {
		List<List<Birthday>> list=new ArrayList<List<Birthday>>();
		for (int i = 0; i < 12; i++) {
			list.add(new ArrayList<Birthday>());
		}
		return list;
	}


}
