package ch.zhaw.gartenverwaltung.backgroundtasks;

import ch.zhaw.gartenverwaltung.Settings;
import ch.zhaw.gartenverwaltung.backgroundtasks.email.EMailSender;
import ch.zhaw.gartenverwaltung.io.CropList;
import ch.zhaw.gartenverwaltung.io.HardinessZoneNotSetException;
import ch.zhaw.gartenverwaltung.io.PlantList;
import ch.zhaw.gartenverwaltung.io.TaskList;
import ch.zhaw.gartenverwaltung.types.Crop;
import ch.zhaw.gartenverwaltung.types.Task;

import javax.mail.MessagingException;
import java.io.IOException;
import java.time.LocalDate;

/**
 * Class to send Notifications to the user
 */
public class Notifier {
    private final TaskList taskList;
    private final CropList cropList;
    private final PlantList plantList;
    private final EMailSender eMailSender = new EMailSender();

    public Notifier(TaskList taskList, PlantList plantList, CropList cropList) {
        this.taskList = taskList;
        this.cropList = cropList;
        this.plantList = plantList;
    }

    /**
     * Method to send a Notification to the user for e specific Task
     * @param task The task to use for notification
     * @throws IOException if tasklist can not be read
     * @throws MessagingException if E-Mail can not be sent for any reason
     * @throws HardinessZoneNotSetException if hardiness zone is not set in plant list.
     */
    private void sendNotification(Task task) throws IOException, MessagingException, HardinessZoneNotSetException {
        String plantName = "unkown plant";
        if(cropList.getCropById((task.getCropId())).isPresent()){
            Crop crop = cropList.getCropById(task.getCropId()).get();
            if(plantList.getPlantById(Settings.getInstance().getCurrentHardinessZone(), crop.getPlantId()).isPresent()) {
                plantName = plantList.getPlantById(Settings.getInstance().getCurrentHardinessZone(), crop.getPlantId()).get().name();
            }
        }
        String messageSubject = String.format(Settings.getInstance().getMailNotificationSubjectTemplate(), task.getName());
        String messageText = String.format(Settings.getInstance().getMailNotificationTextTemplate(), task.getName(), plantName, task.getNextExecution(), task.getDescription());
        eMailSender.sendMails(Settings.getInstance().getMailNotificationReceivers(), messageSubject, messageText);
    }

    /**
     * Sends a notification to the user for each task which is due and not done every day as long as it's not done.
     * @throws IOException if tasklist can not be read
     * @throws MessagingException if E-Mail can not be sent for any reason
     * @throws HardinessZoneNotSetException if hardiness zone is not set in plant list.
     */
    public void sendNotifications() throws IOException, MessagingException, HardinessZoneNotSetException {
        for (Task task : taskList.getTaskList(LocalDate.MIN, LocalDate.MAX)) {
            if (task.getNextNotification() != null && task.getNextNotification().isBefore(LocalDate.now().minusDays(1))) {
                sendNotification(task);
                task.setNextNotification(LocalDate.now().plusDays(1));
                taskList.saveTask(task);
            }
        }
    }
}
