package ee.pardiralli.util;

import ee.pardiralli.feedback.Feedback;
import ee.pardiralli.feedback.FeedbackType;
import org.springframework.ui.Model;

import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;

public class ControllerUtil {

    /**
     * Adds feedback list to model. This can be later used via Thymeleaf to generate Bootstrap alert dialogs.
     * <p>
     * For example <code>ControllerUtil.setFeedback(FeedbackType.SUCCESS, "Hello world!")</code> will add a list
     * containing one {@link Feedback} object that can be used to generate
     * HTML tag <code><div class="alert alert-success">Hello world!</div></code>
     * <p>
     *
     * @param model    to add feedback
     * @param type     of the feedback
     * @param feedback as message
     * @return model which is updated
     */
    public static Model setFeedback(Model model, FeedbackType type, String feedback) {
        if (model.containsAttribute(Feedback.FEEDBACK_VARIABLE_NAME)) {
            List<Feedback> feedbacks = (List<Feedback>) model.asMap().get(Feedback.FEEDBACK_VARIABLE_NAME);
            feedbacks.add(new Feedback(feedback, type));
            model.addAttribute(Feedback.FEEDBACK_VARIABLE_NAME, feedbacks);
        } else {
            List<Feedback> lst = new ArrayList<>();
            lst.add(new Feedback(feedback, type));
            model.addAttribute(Feedback.FEEDBACK_VARIABLE_NAME, lst);
        }
        return model;
    }

    public static String currentDatetime() {
        String dateTime = ZonedDateTime.now(ZoneId.of("Europe/Helsinki"))
                .truncatedTo(ChronoUnit.MINUTES)
                .format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
        return dateTime.substring(0, dateTime.lastIndexOf(":")) + dateTime.substring(dateTime.lastIndexOf(":") + 1, dateTime.length());
    }
}

@@ -148,146 +148,8 @@
 l;%0A%0A
-import java.time.ZoneId;%0Aimport java.time.ZonedDateTime;%0Aimport java.time.format.DateTimeFormatter;%0Aimport java.time.temporal.ChronoUnit;%0A
 impo
@@ -194,16 +194,52 @@
 il.List;
+%0Aimport java.util.stream.Collectors;
 %0A%0Apublic
@@ -432,19 +432,19 @@
 lerUtil.
-set
+add
 Feedback
@@ -795,46 +795,8 @@
 age%0A
-     * @return model which is updated%0A
     
@@ -821,17 +821,16 @@
 tic 
-Model set
+void add
 Feed
@@ -996,26 +996,23 @@
 back
-s
+List
  = 
+(
 (List%3C
-Feedback
+?
 %3E) m
@@ -1060,17 +1060,122 @@
 LE_NAME)
-;
+).stream()%0A                    .map(e -%3E (Feedback) e)%0A                    .collect(Collectors.toList());%0A
 %0A       
@@ -1187,17 +1187,20 @@
 feedback
-s
+List
 .add(new
@@ -1299,17 +1299,20 @@
 feedback
-s
+List
 );%0A     
@@ -1510,406 +1510,8 @@
   %7D%0A
-        return model;%0A    %7D%0A%0A    public static String currentDatetime() %7B%0A        String dateTime = ZonedDateTime.now(ZoneId.of(%22Europe/Helsinki%22))%0A                .truncatedTo(ChronoUnit.MINUTES)%0A                .format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);%0A        return dateTime.substring(0, dateTime.lastIndexOf(%22:%22)) + dateTime.substring(dateTime.lastIndexOf(%22:%22) + 1, dateTime.length());%0A
     

Remove unchecked warning by checking objects