package org.hso.texturesyncclient.view.startupView

import com.jfoenix.animation.alert.CenterTransition
import com.jfoenix.controls.JFXButton
import com.jfoenix.controls.JFXSpinner
import com.jfoenix.controls.JFXTextField
import javafx.geometry.Insets
import javafx.geometry.Pos
import javafx.scene.control.Label
import javafx.scene.input.KeyCode
import javafx.scene.layout.Background
import javafx.scene.layout.BackgroundFill
import javafx.scene.layout.CornerRadii
import javafx.scene.paint.Paint
import javafx.scene.text.Font
import javafx.scene.text.FontWeight
import tornadofx.*

class StartupView : View() {

    val labelStatus = Label("Verbindung zum Server einrichten")
    val spinnerStatus = JFXSpinner()
    val labelServerIP = Label("Server-Adresse")
    val tfServerIP = JFXTextField()
    val btnConnect = JFXButton("Manuell Verbinden")

    private val svc: StartupViewController by inject()

    override val root = borderpane {
        minWidth = 1000.0
        minHeight = 500.0
        background = Background(BackgroundFill(Paint.valueOf("#2b2b2b"), CornerRadii.EMPTY, Insets.EMPTY))

        center = vbox(50) {
            maxWidth = 350.0
            alignment = Pos.CENTER

            add(labelStatus)
            add(spinnerStatus)

            vbox(10) {
                alignment = Pos.CENTER

                add(labelServerIP)
                add(tfServerIP)
                add(btnConnect)
            }

        }

        style {
            spinnerStatus.isVisible = false

            labelStatus.font = Font.font("Verdana", FontWeight.MEDIUM, 20.0)
            labelStatus.textFill = Paint.valueOf("#2b7bbb")

            labelServerIP.textFill = Paint.valueOf("#2b7bbb")
            labelServerIP.font = Font.font("Verdana", FontWeight.MEDIUM, 15.0)
            btnConnect.textFill = Paint.valueOf("#2b7bbb")
            btnConnect.background =
                Background(BackgroundFill(Paint.valueOf("#3c3f41"), CornerRadii.EMPTY, Insets.EMPTY))

            tfServerIP.style { //TODO without .style
                textFill = Paint.valueOf("#b15b2e")
                alignment = Pos.BASELINE_CENTER
            }

        }

        btnConnect.setOnAction {
            spinnerStatus.isVisible = true
            svc.btnConnectAction(tfServerIP.text)
            //spinnerStatus.isVisible = false

        }

        tfServerIP.setOnKeyPressed {
            if (it.code == KeyCode.ENTER) {
                spinnerStatus.isVisible = true
                svc.btnConnectAction(tfServerIP.text)
                spinnerStatus.isVisible = false
            }
        }

    }
}