Facility.kt 1.87 KB
Newer Older
Robert Hitt's avatar
Robert Hitt committed
1 2 3 4
package srct.whatsopen.model

import com.google.gson.annotations.SerializedName

Dylan Jones's avatar
Dylan Jones committed
5 6 7 8 9 10 11
import java.util.*

data class Facility(
    @SerializedName("facility_name") val name: String,
    @SerializedName("main_schedule") val mainSchedule: Schedule,
    @SerializedName("special_schedules") val specialSchedules: List<Schedule>,
    @SerializedName("facility_location") val location: Location,
12 13
    @SerializedName("slug") val slug: String,
    val isFavorite: Boolean
Dylan Jones's avatar
Dylan Jones committed
14 15 16 17 18 19 20 21 22
) {
    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
Robert Hitt's avatar
Robert Hitt committed
23 24
    }

Dylan Jones's avatar
Dylan Jones committed
25
    fun isOpen(): Boolean {
Dylan Jones's avatar
Dylan Jones committed
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
        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()
        }
Dylan Jones's avatar
Dylan Jones committed
60
    }
Robert Hitt's avatar
Robert Hitt committed
61
}