package de.leopoldluley.exacalc.view

import javafx.scene.input.MouseEvent
import tornadofx.*

class MainView : View("ExaCalc") {

    val navigation: NavigationView by inject()

    val calculator: CalculatorView by inject()
    val settings: SettingsView by inject()
    val variables: VariablesView by inject()
    val functions: FunctionsView by inject()

    var current: View = calculator

    override val root = stackpane {
        prefHeight = 400.0
        prefWidth = 600.0

        setOnMouseDragged { doDrag(it) }
        setOnMousePressed { startDrag(it) }

        this += current
        this += navigation
    }

    fun show(view: View) {
        if (current !== view) {
            current.replaceWith(view, ViewTransition.Reveal(0.3.seconds))
            current = view
        }
    }

    var lastXPos = 0.0
    var lastYPos = 0.0
    fun startDrag(event: MouseEvent) {
        lastXPos = event.screenX - FX.primaryStage.x
        lastYPos = event.screenY - FX.primaryStage.y
    }

    fun doDrag(event: MouseEvent) {
        println(event.screenX)
        FX.primaryStage.x = event.screenX - lastXPos
        FX.primaryStage.y = event.screenY - lastYPos
    }

}