/**
 * Lesson: Application
 *
 * 1. The Application class serves as the entry point for JavaFX applications.
 * 2. The main method initiates the JavaFX runtime via Application.launch(String... args).
 * 3. When launch() is called without arguments, the application lifecycle follows:
 *    - init(): Initialization phase
 *    - start(): Primary stage setup
 *    - stop(): Cleanup upon application exit
 */

package com.msksbr.jfxLearn.lesson2

import javafx.application.Application
import javafx.scene.Scene
import javafx.stage.Stage
import javafx.scene.control.Button
import javafx.scene.layout.BorderPane

class MainApplication : Application() {
    override fun start(stage: Stage) {
        println("we are in start method")
        // Extension Exercises: create a button, when it clicked, open a website via browser
        val button = Button("open website")
        button.setOnAction {
            hostServices.showDocument("https://msksbr.com")
        }
        val borderPane = BorderPane(button)
        val scene = Scene(borderPane, 300.0, 300.0)

        stage.scene = scene
        stage.title = "JavaFX Application"
        stage.show()
    }

    override fun init() {
        // what can we do in init method?
        // 1. connect database
        // 2, create a new thread
        // ...
        println("we are in init method")
    }

    override fun stop() {
        // what can we do in stop method?
        // 1. close database connection
        // 2. close a thread
        // 3. clean resources
        // ...
        println("we are in stop method")
    }
}

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