package de.kif.frontend.views.calendar

import de.kif.common.model.Room
import de.kif.common.model.Schedule
import de.kif.frontend.launch
import de.kif.frontend.repository.ScheduleRepository
import de.westermann.kwebview.View
import de.westermann.kwebview.ViewCollection
import de.westermann.kwebview.components.*

class CalendarTools(entry: CalendarEntry) : ViewCollection<View>() {

    fun setName(room: Room, time: Int) {
        nameView.text = room.name + " - " + Schedule.timeOfDayToString(time)
    }

    lateinit var schedule: Schedule

    fun update(schedule: Schedule) {
        this.schedule = schedule
        setName(schedule.room, schedule.time)
        lookRoomButton.classList["disabled"] = !schedule.lookRoom
        lookTimeButton.classList["disabled"] = !schedule.lookTime
    }

    private lateinit var nameView: TextView
    private lateinit var lookRoomButton: IconView
    private lateinit var lookTimeButton: IconView

    init {
        boxView {
            nameView = textView { }

            lookRoomButton = iconView(MaterialIcon.GPS_FIXED) {
                title = "Look room"
                onClick {
                    entry.pending = true
                    launch {
                        val s = entry.schedule.get()
                        ScheduleRepository.update(s.copy(lookRoom = "disabled" in this.classList))
                    }
                }
            }
            lookTimeButton = iconView(MaterialIcon.ALARM) {
                title = "Look time slot"
                onClick {
                    entry.pending = true
                    launch {
                        val s = entry.schedule.get()
                        ScheduleRepository.update(s.copy(lookTime = "disabled" in this.classList))
                    }
                }
            }
        }
        boxView {
            textView("-10") {
                title = "Schedule 10 minutes earlier"
                onClick {
                    if (schedule.lookTime) return@onClick

                    entry.pending = true
                    launch {
                        val s = entry.schedule.get()
                        ScheduleRepository.update(s.copy(time = s.time - 10))
                    }
                }
            }
            textView("-5") {
                title = "Schedule 5 minutes earlier"
                onClick {
                    if (schedule.lookTime) return@onClick

                    entry.pending = true
                    launch {
                        val s = entry.schedule.get()
                        ScheduleRepository.update(s.copy(time = s.time - 5))
                    }
                }
            }
            textView("+5") {
                title = "Schedule 5 minutes later"
                onClick {
                    if (schedule.lookTime) return@onClick

                    entry.pending = true
                    launch {
                        val s = entry.schedule.get()
                        ScheduleRepository.update(s.copy(time = s.time + 5))
                    }
                }
            }
            textView("+10") {
                title = "Schedule 10 minutes later"
                onClick {
                    if (schedule.lookTime) return@onClick

                    entry.pending = true
                    launch {
                        val s = entry.schedule.get()
                        ScheduleRepository.update(s.copy(time = s.time + 10))
                    }
                }
            }

            iconView(MaterialIcon.DELETE) {
                title = "Delete"
                onClick {
                    entry.pending = true
                    launch {
                        ScheduleRepository.delete(entry.scheduleId)
                    }
                }
            }
        }
    }
}