package com.riaektiv.javafx.desktop

import javafx.application.Application
import javafx.collections.FXCollections
import javafx.geometry.Insets
import javafx.scene.Group
import javafx.scene.Scene
import javafx.scene.control.*
import javafx.scene.control.cell.PropertyValueFactory
import javafx.scene.layout.HBox
import javafx.scene.layout.VBox
import javafx.scene.text.Font
import javafx.stage.Stage

/**
 * Coding With Passion Since 1991
 * Created: 9/4/2016, 6:12 PM Eastern Time

 * @author Sergey Chuykov
 */
class PortfolioApplication : Application() {

    private val table = TableView<Position>()

    private val data = FXCollections.observableArrayList(
            Position("APA", "Apache Corp.", "3000"),
            Position("CHK", "Chesapeake Energy Corp.", "1000"),
            Position("NBL", "Nobel Energy Inc.", "5000"))

    @SuppressWarnings("unchecked")
    @Throws(Exception::class)
    override fun start(stage: Stage) {
        val scene = Scene(Group())
        stage.title = "Portfolio"
        stage.width = 700.0
        stage.height = 500.0

        val label = Label("Positions")
        label.font = Font("Arial", 20.0)

        table.isEditable = true
        table.maxHeight = 300.0

        val symbolCol = TableColumn<Position, String>("Symbol")
        symbolCol.minWidth = 100.0
        symbolCol.cellValueFactory = PropertyValueFactory<Position, String>("symbol")

        val companyCol = TableColumn<Position, String>("Company")
        companyCol.minWidth = 400.0
        companyCol.cellValueFactory = PropertyValueFactory<Position, String>("company")

        val qtyCol = TableColumn<Position, String>("Qty")
        qtyCol.minWidth = 100.0
        qtyCol.cellValueFactory = PropertyValueFactory<Position, String>("qty")

        table.items = data
        table.columns.addAll(symbolCol, companyCol, qtyCol)

        val symbolText = TextField()
        symbolText.promptText = "Symbol"
        symbolText.maxWidth = symbolCol.prefWidth

        val companyText = TextField()
        companyText.maxWidth = companyCol.prefWidth
        companyText.promptText = "Company"

        val qtyText = TextField()
        qtyText.maxWidth = qtyCol.prefWidth
        qtyText.promptText = "Qty"

        val addButton = Button("Add")
        addButton.setOnAction { e ->
            data.add(Position(
                    symbolText.text,
                    companyText.text,
                    qtyText.text))
            symbolText.clear()
            companyText.clear()
            qtyText.clear()
        }

        val hb = HBox()
        hb.children.addAll(symbolText, companyText, qtyText, addButton)
        hb.spacing = 3.0
        val vb = VBox()
        vb.spacing = 5.0
        vb.padding = Insets(10.0, 0.0, 0.0, 10.0)
        vb.children.addAll(label, table, hb)

        (scene.root as Group).children.addAll(vb)

        stage.scene = scene
        stage.show()
    }

    companion object {
        @JvmStatic fun main(args: Array<String>) {
            launch(PortfolioApplication::class.java, *args)
        }
    }

}
