package de.kif.common.model

import de.kif.common.SearchElement
import kotlinx.serialization.Serializable

@Serializable
data class Schedule(
    override val id: Long?,
    val workGroup: WorkGroup,
    val room: Room,
    val day: Int,
    val time: Int,
    val lockRoom: Boolean = false,
    val lockTime: Boolean = false,
    override val createdAt: Long = 0,
    override val updateAt: Long = 0
) : Model {


    override fun createSearch() = SearchElement(
        mapOf(
            "workgroup" to workGroup.name,
            "room" to room.name,
            "track" to (workGroup.track?.name ?: ""),
            "language" to workGroup.language.localeName
        ), mapOf(), mapOf(
            "day" to day.toDouble()
        )
    )

    fun getAbsoluteStartTime(): Int = day * 60 * 24 + time
    fun getAbsoluteEndTime(): Int = getAbsoluteStartTime() + workGroup.length
    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (other == null || this::class != other::class) return false

        other as Schedule

        if (id != other.id) return false
        if (workGroup != other.workGroup) return false
        if (room != other.room) return false
        if (day != other.day) return false
        if (time != other.time) return false
        if (lockRoom != other.lockRoom) return false
        if (lockTime != other.lockTime) return false

        return true
    }

    fun equalsIgnoreId(other: Schedule): Boolean = copy(id = null) == other.copy(id = null)

    override fun hashCode(): Int {
        var result = id?.hashCode() ?: 0
        result = 31 * result + workGroup.hashCode()
        result = 31 * result + room.hashCode()
        result = 31 * result + day
        result = 31 * result + time
        result = 31 * result + lockRoom.hashCode()
        result = 31 * result + lockTime.hashCode()
        return result
    }

    companion object {
        fun timeOfDayToString(time: Int): String {
            var day = time % (24 * 60)
            if (day < 0) day += 24 * 60

            val hour = day / 60
            val minute = day % 60

            return hour.toString().padStart(2, '0') + ":" + minute.toString().padStart(2, '0')
        }

        fun timeDifferenceToString(time: Long): String {
            if (time < 0) {
                return "running"
            }

            var remainder = time

            val seconds = (remainder % 60).toString().padStart(2, '0')
            remainder /= 60

            val minutes = (remainder % 60).toString().padStart(2, '0')
            remainder /= 60

            val hours = (remainder % 24).toString().padStart(2, '0')
            remainder /= 24

            val days = remainder

            if (days > 1) {
                return "$days days"
            }
            if (days > 0) {
                return "1 day"
            }

            if (hours == "00") {
                return "$minutes:$seconds"
            }

            return "$hours:$minutes:$seconds"
        }
    }
}
