package de.kif.frontend.views

import de.kif.common.formatDate
import de.kif.frontend.timezoneOffset
import de.westermann.kwebview.View
import de.westermann.kwebview.components.InputType
import de.westermann.kwebview.components.InputView
import de.westermann.kwebview.components.TextView
import de.westermann.kwebview.createHtmlView
import de.westermann.kwebview.iterator
import org.w3c.dom.*
import kotlin.browser.document

fun initRoomConstraints() {
    var index = 10000

    val constraints =
        document.getElementsByClassName("room-constraints")[0] as HTMLElement
    val addButton =
        View.wrap(document.getElementsByClassName("room-constraints-add")[0] as HTMLElement)

    val referenceDate = constraints.dataset["reference"]?.toLongOrNull() ?: 0L

    fun updateDateView(inputGroup: HTMLElement, day: Int) {
        val date = referenceDate + (day * 1000 * 60 * 60 * 24)
        val dateName = formatDate(date, timezoneOffset)
        inputGroup.dataset["hint"] = dateName
    }

    addButton.onClick {
        constraints.appendChild(View.wrap(createHtmlView<HTMLDivElement>()) {
            classList += "input-group"
            html.appendChild(TextView("Gesperrt").apply {
                classList += "form-btn"
                onClick { this@wrap.html.remove() }
            }.html)
            html.appendChild(InputView(InputType.NUMBER).apply {
                classList += "form-control"
                html.name = "constraint-room-day-${index}"
                placeholder = "Tag"

                min = -1337.0
                max = 1337.0

                updateDateView(this@wrap.html, value.toIntOrNull() ?: 0)
                valueProperty.onChange {
                    updateDateView(this@wrap.html, value.toIntOrNull() ?: 0)
                }
            }.html)
            html.appendChild(InputView(InputType.TEXT).apply {
                classList += "form-control"
                html.name = "constraint-room-start-${index}"
                placeholder = "Start"
            }.html)
            html.appendChild(InputView(InputType.TEXT).apply {
                classList += "form-control"
                html.name = "constraint-room-end-${index++}"
                placeholder = "Ende"
            }.html)
        }.html)
    }

    for (child in constraints.children.iterator()) {
        if (child.classList.contains("input-group")) {
            val span = child.firstElementChild as HTMLElement

            span.addEventListener("click", org.w3c.dom.events.EventListener {
                println("click")
                child.remove()
            })

            for (e in child.children.iterator()) {
                if (e is HTMLInputElement && e.name.contains("-day-")) {
                    val input = InputView.wrap(InputType.NUMBER, e)

                    updateDateView(child, input.value.toIntOrNull() ?: 0)
                    input.valueProperty.onChange {
                        updateDateView(child, input.value.toIntOrNull() ?: 0)
                    }
                }
            }
        }
    }
}
