package io.cfp.dto;

import io.cfp.entity.Room;
import io.cfp.entity.Talk;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author <a href="mailto:nicolas.deloof@gmail.com">Nicolas De Loof</a>
 */
@Data @NoArgsConstructor
public class FullCalendar {

    private List<Resource> resources;

    private List<Event> events;


    public FullCalendar(List<Talk> talks, List<Room> rooms) {

        resources = rooms.stream()
            .map(Resource::new)
            .collect(Collectors.toList());

        events = talks.stream()
            .filter(t -> t.getDate() != null && t.getRoom() != null)
            .map(Event::new)
            .collect(Collectors.toList());
    }

    @Data @NoArgsConstructor
    public static class Resource {
        private String id;
        private String title;

        public Resource(Room r) {
            this.id = String.valueOf(r.getId());
            this.title = r.getName();
        }
    }

    @Data @NoArgsConstructor
    public static class Event {
        private String id;
        private String resourceId;
        private String start;
        private String end;
        private String title;

        public Event(Talk talk) {
            this.id = String.valueOf(talk.getId());
            this.title = talk.getName();
            this.resourceId = String.valueOf(talk.getRoom().getId());
            this.start = DateTimeFormatter.ISO_INSTANT.format(talk.getDate().toInstant());
            this.end = DateTimeFormatter.ISO_INSTANT.format(talk.getDate().toInstant().plus(talk.getDuree(), ChronoUnit.MINUTES));
        }
    }

}

@@ -1238,16 +1238,46 @@
  start;%0A
+        private int duration;%0A
         
@@ -1296,16 +1296,16 @@
 ng end;%0A
-
         
@@ -1446,32 +1446,78 @@
 talk.getName();%0A
+            if (talk.getRoom() != null) %7B%0A    
             this
@@ -1562,32 +1562,92 @@
 oom().getId());%0A
+            %7D%0A            if (talk.getDate() != null) %7B%0A    
             this
@@ -1717,24 +1717,28 @@
 Instant());%0A
+    
             
@@ -1852,24 +1852,83 @@
 .MINUTES));%0A
+            %7D%0A            this.duration = talk.getDuree();%0A
         %7D%0A  

support unscheduled events