/**
 * ScheduleFormPopulator.java
 * Created on Aug 14, 2008
 * (C) Copyright TANDBERG Television Ltd.
 */
package com.tandbergtv.watchpoint.pmm.web.schedule;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;

import org.apache.log4j.Logger;

import com.tandbergtv.watchpoint.pmm.entities.DistributionSchedule;
import com.tandbergtv.watchpoint.pmm.entities.Schedule;
import com.tandbergtv.watchpoint.pmm.schedule.ISchedulePersistenceService;
import com.tandbergtv.watchpoint.pmm.web.title.search.PitchDestinationDataProvider;
import com.tandbergtv.watchpoint.pmm.web.title.search.PlannerSourceDataProvider;
import com.tandbergtv.watchpoint.pmm.web.util.CommonUtils;
import com.tandbergtv.workflow.core.service.ServiceRegistry;
import com.tandbergtv.workflow.web.page.ISearchFieldDataProvider;

/**
 * This is a helper class used by the action and validator classes to populate the schedule form.
 * 
 * @author spuranik
 * 
 */
public class ScheduleFormPopulator {
	
	private static final Logger logger = Logger.getLogger(ScheduleFormPopulator.class);

	/**
	 * This method populates the source and destination lists in the schedule form.
	 * 
	 * @param form
	 */
	public static void populateScheduleForm(ScheduleForm form) {
		form.setSources(getSources());
		form.setDestinations(getDestinations());
	}

	/**
	 * This method re-populates fields in the schedule form after the validations have been
	 * performed. If a new distribution schedule is being created the pitch date is set in the
	 * schedule only if the user provided a valid formatted date.
	 * 
	 * @param scheduleForm
	 */
	public static void repopulateFormFields(ScheduleForm scheduleForm) {
		// set the source and destination partner lists
		ScheduleFormPopulator.populateScheduleForm(scheduleForm);

		// set the schedule entity if this is an update.
		String userDate = scheduleForm.getDate();
		String userSourcePartnerId = scheduleForm.getSourcePartnerId();
		if (scheduleForm.getScheduleId() != null
				&& scheduleForm.getScheduleId().trim().length() > 0) {
			ISchedulePersistenceService service = ServiceRegistry.getDefault().lookup(
					ISchedulePersistenceService.class);
			Schedule schedule = service.get(Long.parseLong(scheduleForm.getScheduleId()));
			// show the date, source partnerId entered by the user.
			scheduleForm.setSchedule(schedule);
			scheduleForm.setSourcePartnerId(userSourcePartnerId);
			scheduleForm.setDate(userDate);
		} else {
			// create a new schedule entity
			DistributionSchedule s = new DistributionSchedule();
			if (scheduleForm.getSourcePartnerId() != null
					&& scheduleForm.getSourcePartnerId().trim().length() > 0) {
				s.setSourcePartnerID(Long.parseLong(scheduleForm.getSourcePartnerId()));
			}
			s.setContextID(Long.parseLong(scheduleForm.getContextId()));

			// set the schedule's pitch date only if the format is correct.
			try {
				s.setPitchDate(CommonUtils.getDate(userDate));
			} catch (RuntimeException e) {
				logger.warn("Not setting schedule's pitch date because of error: " + e);
			}
			scheduleForm.setSchedule(s);
			// show the date entered by the user (this will not be populated in the schedule entity 
			// if the format is not right. So set it here.
			scheduleForm.setDate(userDate);
		}
	}
	
	private static Collection<PartnerBean> getDestinations() {
		Collection<PartnerBean> destinations = new ArrayList<PartnerBean>();
		ISearchFieldDataProvider dataprovider = new PitchDestinationDataProvider();
		Map<String, String> data = dataprovider.getData();

		for (String key : data.keySet())
			destinations.add(new PartnerBean(Long.valueOf(key), data.get(key)));

		return destinations;
	}

	private static Collection<PartnerBean> getSources() {
		Collection<PartnerBean> sources = new ArrayList<PartnerBean>();
		ISearchFieldDataProvider dataprovider = new PlannerSourceDataProvider();
		Map<String, String> data = dataprovider.getData();

		for (String key : data.keySet())
			sources.add(new PartnerBean(Long.valueOf(key), data.get(key)));

		return sources;
	}
}
