package srct.whatsopen.model import com.google.gson.annotations.SerializedName import java.util.* data class Facility( @SerializedName("facility_name") val name: String, @SerializedName("main_schedule") val mainSchedule: Schedule, @SerializedName("special_schedules") val specialSchedules: List, @SerializedName("facility_location") val location: Location, val isFavorite: Boolean, val statusDuration: String ) { fun currentSchedule(): Schedule { val now: Date = Calendar.getInstance(TimeZone.getTimeZone("EST5EDT")).time for (schedule in specialSchedules) { if (schedule.validStart < now && now < schedule.validEnd) { return schedule } } return mainSchedule } fun isOpen(): Boolean { return currentSchedule().isOpen24Hours || currentSchedule().openTimesList.any { time -> time.isOpen() } } fun nextOpenTime(): OpenTimes? { // TODO handle special schedules possibly? var leastSeconds: Int = Int.MAX_VALUE var nextOpening: OpenTimes? = null for (time in currentSchedule().openTimesList) { if (time.timeTillOpen() < leastSeconds) { leastSeconds = time.timeTillOpen() nextOpening = time } } return nextOpening } fun nextCloseTime(): OpenTimes? { var leastSeconds: Int = Int.MAX_VALUE var nextClosing: OpenTimes? = null for (time in currentSchedule().openTimesList) { if (time.timeTillClose() < leastSeconds) { leastSeconds = time.timeTillClose() nextClosing = time } } return nextClosing } fun currentHours(): OpenTimes? { return if (isOpen()) { nextCloseTime() } else { nextOpenTime() } } }