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)
        lockRoomButton.classList["disabled"] = !schedule.lockRoom
        lockTimeButton.classList["disabled"] = !schedule.lockTime
    }

    private lateinit var nameView: TextView
    private lateinit var lockRoomButton: IconView
    private lateinit var lockTimeButton: IconView

    init {
        boxView {
            nameView = textView { }

            lockRoomButton = iconView(MaterialIcon.GPS_FIXED) {
                title = "Lock room"
                onClick {
                    entry.pending = true
                    launch {
                        val s = entry.schedule.get()
                        ScheduleRepository.update(s.copy(lockRoom = "disabled" in this.classList))
                    }
                }
            }
            lockTimeButton = iconView(MaterialIcon.ALARM) {
                title = "Lock time slot"
                onClick {
                    entry.pending = true
                    launch {
                        val s = entry.schedule.get()
                        ScheduleRepository.update(s.copy(lockTime = "disabled" in this.classList))
                    }
                }
            }
        }
        boxView {
            textView("-10") {
                title = "Schedule 10 minutes earlier"
                onClick {
                    if (schedule.lockTime) 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.lockTime) 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.lockTime) 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.lockTime) 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)
                    }
                }
            }
        }
    }
}