package whiteout;

import javafx.scene.paint.Color;
import javafx.scene.paint.LinearGradient;
import javafx.scene.paint.Stop;
import javafx.scene.text.Font;
import javafx.scene.text.Text;

public class Env {

    public-init var screenWidth: Number;
    public-init var screenHeight: Number;

    public def smallFont = 
        if (screenWidth < 320) 
            Font {size: 15}
        else
            Font {size: 20}

    public def mediumFont = 
        if (screenHeight >= 240)
            Font {size: 40}
        else
            Font {size: 20}

    public def bigFont = 
        if (screenWidth < 320)
            Font {size: 30}
        else
            Font {size: 60}

    // Compute height / width of the blue buttons based on the size of "Reset"
    def resetSize = Text {
        content: "Reset"
        font: smallFont
    };

    // Leave a bit of space around "Reset"
    public def blueButtonHeight = resetSize.layoutBounds.height + 8;
    public def blueButtonWidth = resetSize.layoutBounds.width + 10;

    // If this is set much greater than 5, the game gets a bit odd since
    // the randomize function just toggles cells in the 0..4 range.
    // It has to do overlapping toggles or else the game is uninteresting.
    public def nButtons = 5;

    // This is the gap we will leave between game buttons, and we want at least this
    // much margin at the top / bottom of the screen.  That makes 5 buttons and 6 gaps.
    def gameButtonGapPercent = .1;

    def nButtonsPlusGaps = nButtons * (1 + gameButtonGapPercent);

    def countSize = Text {
        content: "222"
        font: mediumFont
    }

    // Find X size of the control buttons area on the right
    def controlSize = if (blueButtonWidth > countSize.layoutBounds.width)
                         blueButtonWidth
                     else
                         countSize.layoutBounds.width;

    // This is the X where the game buttons will end.
    def gameButtonMaxX = if (screenWidth - 1.3 * controlSize < screenHeight) 
                             // narrow screen
                             screenWidth - 1.3 * controlSize
                         else
                             // short screen
                             screenHeight;

    // 0 .... gameButtonMaxX will be occupied by n game buttons and n + 1 gaps.
    public def gameButtonSize = gameButtonMaxX / (nButtonsPlusGaps + gameButtonGapPercent);
    public def gameButtonGap = gameButtonSize * gameButtonGapPercent;
    public def buttonX = gameButtonMaxX + (screenWidth - gameButtonMaxX - blueButtonWidth) / 2;
    public def topMargin = (screenHeight - ((gameButtonSize * nButtonsPlusGaps) - gameButtonGap)) / 2;
    public def slateGrad = LinearGradient{
        startX: 0
        startY: 0
        endX: 0
        endY: 1
        stops: [
            Stop {offset: 0.0, color: Color.rgb(0x34, 0x34, 0x34)}
            Stop {offset: 0.5, color: Color.rgb(0x55, 0x55, 0x55)}
            Stop {offset: 1.0, color: Color.rgb(0x43, 0x43, 0x43)}
        ]
    }
    public def blueGrad = LinearGradient{
        startX: 0
        startY: 0
        endX: 0
        endY: 1
        stops: [
            Stop {offset: 0.0, color:Color.rgb(0, 0x33, 0xff)}
            Stop {offset: 0.5, color:Color.rgb(0, 0x55, 0xff)}
            Stop {offset: 1.0, color:Color.BLUE}
        ]
    }
}