// Styling of Lemur components
// For documentation, see:
// https://github.com/jMonkeyEngine-Contributions/Lemur/wiki/Styling
import com.simsilica.lemur.*
import com.simsilica.lemur.component.QuadBackgroundComponent
import com.simsilica.lemur.Button
import com.simsilica.lemur.Button.ButtonAction
import com.simsilica.lemur.Command
import com.simsilica.lemur.HAlignment
import com.simsilica.lemur.Insets3f
import com.simsilica.lemur.component.QuadBackgroundComponent
import com.simsilica.lemur.component.TbtQuadBackgroundComponent

def bgColor = color(1, 1, 1, 1)
def buttonEnabledColor = color(0, 0, 0, 1)
def buttonDisabledColor = color(0.8, 0.9, 1, 0.2)
def buttonBgColor = color(1, 1, 1, 1)
def sliderColor = color(0.6, 0.8, 0.8, 1)
def sliderBgColor = color(0.5, 0.75, 0.75, 1)
def gradientColor = color(0.5, 0.75, 0.85, 0.5)
def tabbuttonEnabledColor = color(0.4, 0.45, 0.5, 1)
def solidWhiteBackground = new QuadBackgroundComponent(color(1, 1, 1, 1)) // Solid white



def gradient = TbtQuadBackgroundComponent.create(
        texture(name: "/com/simsilica/lemur/icons/bordered-gradient.png",
                generateMips: false),
        1, 1, 1, 126, 126,
        1f, false)

def doubleGradient = new QuadBackgroundComponent(gradientColor)
doubleGradient.texture = texture(name: "/com/simsilica/lemur/icons/double-gradient-128.png",
        generateMips: false)

def orangeBorder = TbtQuadBackgroundComponent.create(
        texture(name: "/com/simsilica/lemur/icons/bordered-gradient.png", // Replace with an appropriate texture if needed
                generateMips: false),
        1, 1, 1, 126, 126,
        1f, false)
orangeBorder.color = color(1, 0.5, 0, 1) // Orange color

selector("pp") {
    font = font("Interface/Fonts/Metropolis/Metropolis-Regular-32.fnt")
}

selector("label", "pp") {
    insets = new Insets3f(2, 2, 2, 2)
    color = buttonEnabledColor
}

selector("header", "pp") {
    font = font("Interface/Fonts/Metropolis/Metropolis-Bold-42.fnt")
    insets = new Insets3f(2, 2, 2, 2)
    color = color(1, 0.5, 0, 1)
    textHAlignment = HAlignment.Center
}

selector("container", "pp") {
    background = solidWhiteBackground.clone()
    background.setColor(bgColor)
}

selector("toolbar") {
    // Set the grey background
    background = new QuadBackgroundComponent(greyBackground)

    // Add a red border using a TbtQuadBackgroundComponent
    def redBorder = TbtQuadBackgroundComponent.create(
            texture(name: "/com/simsilica/lemur/icons/bordered-gradient.png",
                    generateMips: false),
            1, 1, 1, 1, 1,
            1f, false)
    redBorder.color = redBorderColor
    background = greyBackground

    // Optional: Set padding inside the toolbar
    insets = new Insets3f(10, 10, 10, 10)
}
selector("slider", "pp") {
    background = gradient.clone()
    background.setColor(bgColor)
}

def pressedCommand = new Command<Button>() {
    void execute(Button source) {
        if (source.isPressed())
            source.move(1, -1, 0)
        else
            source.move(-1, 1, 0)
    }
}

def enabledCommand = new Command<Button>() {
    void execute(Button source) {
        if (source.isEnabled())
            source.setColor(buttonEnabledColor)
        else
            source.setColor(buttonDisabledColor)
    }
}

def repeatCommand = new Command<Button>() {
    private long startTime
    private long lastClick

    void execute(Button source) {
        // Only do the repeating click while the mouse is
        // over the button (and pressed of course)
        if (source.isPressed() && source.isHighlightOn()) {
            long elapsedTime = System.currentTimeMillis() - startTime
            // After half a second pause, click 8 times a second
            if (elapsedTime > 500 && elapsedTime > lastClick + 125) {
                source.click()

                // Try to quantize the last click time to prevent drift
                lastClick = ((elapsedTime - 500) / 125) * 125 + 500
            }
        }
        else {
            startTime = System.currentTimeMillis()
            lastClick = 0
        }
    }
}

def stdButtonCommands = [
        (ButtonAction.Down)    : [pressedCommand],
        (ButtonAction.Up)      : [pressedCommand],
        (ButtonAction.Enabled) : [enabledCommand],
        (ButtonAction.Disabled): [enabledCommand]
]

def sliderButtonCommands = [
        (ButtonAction.Hover): [repeatCommand]
]

selector("title", "pp") {
    color = color(0.8, 0.9, 1, 0.85f)
    highlightColor = color(1, 0.8, 1, 0.85f)
    shadowColor = color(0, 0, 0, 0.75f)
    shadowOffset = vec3(2, -2, -1)
    background = new QuadBackgroundComponent(color(0.5, 0.75, 0.85, 1))
    background.texture = texture(name: "/com/simsilica/lemur/icons/double-gradient-128.png",
            generateMips: false)
    insets = new Insets3f(2, 2, 2, 2)

    buttonCommands = stdButtonCommands
}


selector("button", "pp") {
    def outerBackground = new QuadBackgroundComponent(color(1, 0.5, 0, 1)) // Orange border
    def innerBackground = new QuadBackgroundComponent(buttonBgColor) // Inner button background

    // Apply the outer border as the main background
    background = outerBackground

    // Use insets to create a margin/padding effect for the inner background
    insets = new Insets3f(3, 3, 3, 3) // Adjust the border thickness
    buttonCommands = stdButtonCommands
}

selector("slider", "pp") {
    insets = new Insets3f(1, 3, 1, 2)
}

selector("slider", "button", "pp") {
    background = doubleGradient.clone()
    //background.setColor(sliderBgColor)
    insets = new Insets3f(0, 0, 0, 0)
}

selector("slider.thumb.button", "pp") {
    text = "[]"
    color = sliderColor
}

selector("slider.left.button", "pp") {
    text = "-"
    background = doubleGradient.clone()
    //background.setColor(sliderBgColor)
    background.setMargin(5, 0)
    color = sliderColor

    buttonCommands = sliderButtonCommands
}

selector("slider.right.button", "pp") {
    text = "+"
    background = doubleGradient.clone()
    //background.setColor(sliderBgColor)
    background.setMargin(4, 0)
    color = sliderColor

    buttonCommands = sliderButtonCommands
}

selector("slider.up.button", "pp") {
    buttonCommands = sliderButtonCommands
}

selector("slider.down.button", "pp") {
    buttonCommands = sliderButtonCommands
}

selector("checkbox", "pp") {
    color = buttonEnabledColor
    fontSize = 20
}

selector("rollup", "pp") {
    background = gradient.clone()
    background.setColor(bgColor)
}

selector("tabbedPanel", "pp") {
    activationColor = buttonEnabledColor
}

selector("tabbedPanel.container", "pp") {
    background = null
}

selector("tab.button", "pp") {
    background = solidWhiteBackground.clone()
    background.setColor(bgColor)
    color = tabbuttonEnabledColor
    insets = new Insets3f(4, 2, 0, 2)

    buttonCommands = stdButtonCommands
}
selector("settings-title", "pp") {
    fontSize = 48 // Set font size
    background = new QuadBackgroundComponent(color(0.4157f, 0.4235f, 0.4392f, 1.0f)) // Grey background
 }