package org.optaconf.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

import org.optaconf.cdi.ScheduleManager;
import org.optaconf.domain.Day;
import org.optaconf.domain.Room;
import org.optaconf.domain.Schedule;
import org.optaconf.domain.Talk;
import org.optaconf.domain.Timeslot;

@Path("/{conferenceId}/day")
public class DayService {

    @Inject
    private ScheduleManager scheduleManager;

    @GET
    @Path("/")
    @Produces("application/json")
    public List<Day> getDayList(@PathParam("conferenceId") Long conferenceId) {
        Schedule schedule = scheduleManager.getSchedule();
        return schedule.getDayList();
    }
    @GET
    @Path("/{dayId}/timeslot")
    @Produces("application/json")
    public List<Timeslot> getTimeslotList(@PathParam("conferenceId") Long conferenceId,
            @PathParam("dayId") String dayId) {
        Schedule schedule = scheduleManager.getSchedule();
        // TODO do proper query to DB instead of filtering here
        List<Timeslot> globalTimeslotList = schedule.getTimeslotList();
        List<Timeslot> timeslotList = new ArrayList<Timeslot>(globalTimeslotList.size());
        for (Timeslot timeslot : globalTimeslotList) {
            if (timeslot.getDay().getId().equals(dayId)) {
                timeslotList.add(timeslot);
            }
        }
        return timeslotList;
    }

    @GET
    @Path("/{dayId}/talk")
    @Produces("application/json")
    public Map<Timeslot, Map<Room, Talk>> getTimeslotRoomToTalkMap(@PathParam("conferenceId") Long conferenceId,
            @PathParam("dayId") String dayId) {
        Schedule schedule = scheduleManager.getSchedule();
        Map<Timeslot, Map<Room, Talk>> timeslotRoomToTalkMap = new LinkedHashMap<Timeslot, Map<Room, Talk>>();
        List<Timeslot> timeslotList = getTimeslotList(conferenceId, dayId);
        for (Timeslot timeslot : timeslotList) {
            Map<Room, Talk> roomToTalkMap = new LinkedHashMap<Room, Talk>();
            List<Room> roomList = schedule.getRoomList();
            for (Room room : roomList) {
                Talk talk = null;
                // TODO refactor this performance drain
                for (Talk selectedTalk : schedule.getTalkList()) {
                    if (selectedTalk.getTimeslot() == timeslot && selectedTalk.getRoom() == room) {
                        talk = selectedTalk;
                        break;
                    }
                }
                roomToTalkMap.put(room, talk);
            }
            timeslotRoomToTalkMap.put(timeslot, roomToTalkMap);
        }
        return timeslotRoomToTalkMap;
    }


}

@@ -1659,34 +1659,34 @@
 lic Map%3C
-Timeslot, Map%3CRoom
+String, Map%3CString
 , Talk%3E%3E
@@ -1876,34 +1876,34 @@
     Map%3C
-Timeslot, Map%3CRoom
+String, Map%3CString
 , Talk%3E%3E
@@ -1949,26 +1949,26 @@
 Map%3C
-Timeslot, Map%3CRoom
+String, Map%3CString
 , Ta
@@ -2112,28 +2112,30 @@
         Map%3C
-Room
+String
 , Talk%3E room
@@ -2164,20 +2164,22 @@
 HashMap%3C
-Room
+String
 , Talk%3E(
@@ -2691,16 +2691,24 @@
 put(room
+.getId()
 , talk);
@@ -2768,16 +2768,24 @@
 timeslot
+.getId()
 , roomTo

use id's for map keys