/*
 * Lesson: Stage
 */

package com.msksbr.jfxLearn.lesson3

import javafx.application.Application
import javafx.scene.Scene
import javafx.scene.control.Alert
import javafx.scene.control.Button
import javafx.scene.control.Label
import javafx.scene.image.Image
import javafx.scene.input.KeyCode
import javafx.scene.input.KeyEvent
import javafx.scene.layout.AnchorPane
import javafx.scene.layout.BorderPane
import javafx.stage.Modality
import javafx.stage.Stage
import javafx.stage.StageStyle

class MainApplication : Application() {
    override fun start(stage: Stage?) {
        val label = Label("Hello Kotlin\nPress G to view event test dialog")
        val button0 = Button("APPLICATION MODAL")
        button0.layoutX = 100.0
        button0.layoutY = 100.0
        val button1 = Button("WINDOW MODAL")
        button1.layoutX = 100.0
        button1.layoutY = 130.0
        val button2 = Button("NONE MODAL")
        button2.layoutX = 100.0
        button2.layoutY = 160.0
        val anchorPane = AnchorPane(label, button0, button1, button2)
        val scene = Scene(anchorPane)

        // modality test window
        val label0 = Label("stage 0, APPLICATION_MODAL")
        val borderPane0 = BorderPane(label0)
        val scene0 = Scene(borderPane0, 300.0, 300.0)
        val stage0 = Stage()
        stage0.scene = scene0

        val label1 = Label("stage 1, WINDOW_MODAL")
        val borderPane1 = BorderPane(label1)
        val scene1 = Scene(borderPane1, 300.0, 300.0)
        val stage1 = Stage()
        stage1.scene = scene1

        val label2 = Label("stage 2, NONE_MODAL")
        val borderPane2 = BorderPane(label2)
        val scene2 = Scene(borderPane2, 300.0, 300.0)
        val stage2 = Stage()
        stage2.scene = scene2

        // 1, title: set window title
        stage?.title = "lesson3"
        // 2, icons: set window icon
        stage?.icons?.add(Image("mskbot.png"))
        // 3, isResizable: set window size adjustable
        stage?.isResizable = false
        // 4. x, y, width and height
        // x,y is omitted
        stage?.width = 300.0
        stage?.height = 300.0
        // 5. style:
        //  DECORATED:   default on current operating system
        //  UNDECORATED: non-border window
        //  TRANSPARENT: transparent background and non-border, complete transparent, but ONLY THIS STAGE IS TRANSPARENT
        //  UTILITY:     only close button and title, just like a dialog
        stage?.initStyle(StageStyle.DECORATED)
        // 6. modality
        // when application modal selected, other stage of this application can't use
        stage0.initModality(Modality.APPLICATION_MODAL)
        stage1.initModality(Modality.WINDOW_MODAL)
        // when window modal selected, it's own stage can't use
        stage1.initOwner(stage)
        // default and normal modal option
        stage2.initModality(Modality.NONE)
        button0.setOnAction { stage0.show() }
        button1.setOnAction { stage1.show() }
        button2.setOnAction { stage2.show() }
        // 7. event
        stage?.addEventHandler(KeyEvent.KEY_PRESSED) { event ->
            if (event.code == KeyCode.G) {
                Alert(Alert.AlertType.CONFIRMATION).apply {
                    title = "event test"
                    headerText = null
                    contentText = "event test"
                }.show()
            }
        }

        stage?.scene = scene
        stage?.show()
    }
}

fun main(args: Array<String>) {
    Application.launch(MainApplication::class.java, *args)
}