package de.kif.frontend.views.table

import de.kif.common.SearchElement
import de.kif.common.model.Language
import de.kif.common.model.Track
import de.kif.frontend.iterator
import de.kif.frontend.launch
import de.kif.frontend.repository.RepositoryDelegate
import de.kif.frontend.repository.TrackRepository
import de.kif.frontend.repository.WorkGroupRepository
import de.westermann.kwebview.components.TextView
import org.w3c.dom.HTMLElement
import org.w3c.dom.HTMLSpanElement
import org.w3c.dom.get

class WorkGroupTableLine(view: HTMLElement) : TableLine(view) {

    var lineId = dataset["id"]?.toLongOrNull() ?: -1

    private val workGroup =
        RepositoryDelegate(WorkGroupRepository, lineId)

    private val spanWorkGroupName: TextView
    private val spanWorkGroupLength: TextView
    private val spanWorkGroupInterested: TextView
    private val spanWorkGroupTrack: TextView
    private val spanWorkGroupProjector: TextView
    private val spanWorkGroupResolution: TextView
    private val spanWorkGroupLanguage: TextView

    override var searchElement: SearchElement = super.searchElement

    init {
        val spans = view.getElementsByTagName("span").iterator().asSequence().toList()

        spanWorkGroupName =
            TextView.wrap(spans.first { it.dataset["editType"] == "workgroup-name" } as HTMLSpanElement)
        spanWorkGroupLength =
            TextView.wrap(spans.first { it.dataset["editType"] == "workgroup-length" } as HTMLSpanElement)
        spanWorkGroupInterested =
            TextView.wrap(spans.first { it.dataset["editType"] == "workgroup-interested" } as HTMLSpanElement)
        spanWorkGroupTrack =
            TextView.wrap(spans.first { it.dataset["editType"] == "workgroup-track" } as HTMLSpanElement)
        spanWorkGroupProjector =
            TextView.wrap(spans.first { it.dataset["editType"] == "workgroup-projector" } as HTMLSpanElement)
        spanWorkGroupResolution =
            TextView.wrap(spans.first { it.dataset["editType"] == "workgroup-resolution" } as HTMLSpanElement)
        spanWorkGroupLanguage =
            TextView.wrap(spans.first { it.dataset["editType"] == "workgroup-language" } as HTMLSpanElement)

        setupEditable(spanWorkGroupName) {
            launch {
                val wg = workGroup.get()
                if (wg.name != it) {
                    WorkGroupRepository.update(wg.copy(name = it))
                }
            }
        }

        setupEditable(spanWorkGroupLength, "\\d+".toRegex()) {
            val number = it.toIntOrNull() ?: return@setupEditable
            launch {
                val wg = workGroup.get()
                if (wg.length != number) {
                    WorkGroupRepository.update(wg.copy(length = number))
                }
            }
        }

        setupEditable(spanWorkGroupInterested, "\\d+".toRegex()) {
            val number = it.toIntOrNull() ?: return@setupEditable
            launch {
                val wg = workGroup.get()
                if (wg.interested != number) {
                    WorkGroupRepository.update(wg.copy(interested = number))
                }
            }
        }

        setupBoolean(spanWorkGroupProjector) {
            launch {
                val wg = workGroup.get()
                WorkGroupRepository.update(wg.copy(projector = !wg.projector))
            }
        }

        setupBoolean(spanWorkGroupResolution) {
            launch {
                val wg = workGroup.get()
                WorkGroupRepository.update(wg.copy(resolution = !wg.resolution))
            }
        }

        setupList(spanWorkGroupLanguage, Language.values().sortedBy { it.localeName }, { it.localeName }) {
            if (it == null) return@setupList
            launch {
                val wg = workGroup.get()
                if (wg.language == it) return@launch
                WorkGroupRepository.update(wg.copy(language = it))
            }
        }

        launch {
            val tracks = listOf<Track?>(null) + TrackRepository.all()

            setupList(spanWorkGroupTrack, tracks, { it.name }) {
                launch x@{
                    val wg = workGroup.get()
                    if (wg.track == it) return@x
                    WorkGroupRepository.update(wg.copy(track = it))
                }
            }
        }

        WorkGroupRepository.onUpdate {
            if (it != lineId) return@onUpdate

            launch {
                val wg = WorkGroupRepository.get(it) ?: return@launch
                workGroup.set(wg)
                searchElement = wg.createSearch()

                spanWorkGroupName.text = wg.name
                spanWorkGroupLength.text = wg.length.toString()
                spanWorkGroupInterested.text = wg.interested.toString()
                spanWorkGroupTrack.text = wg.track?.name ?: ""
                spanWorkGroupProjector.text = wg.projector.toString()
                spanWorkGroupResolution.text = wg.resolution.toString()
                spanWorkGroupLanguage.text = wg.language.localeName
            }
        }
    }
}