/* 
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. Use is subject to license terms. 
 * 
 * This file is available and licensed under the following license:
 * 
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions are met:
 *
 *   * Redistributions of source code must retain the above copyright notice, 
 *     this list of conditions and the following disclaimer.
 *
 *   * Redistributions in binary form must reproduce the above copyright notice,
 *     this list of conditions and the following disclaimer in the documentation
 *     and/or other materials provided with the distribution.
 *
 *   * Neither the name of Sun Microsystems nor the names of its contributors 
 *     may be used to endorse or promote products derived from this software 
 *     without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package reversi;

import javafx.stage.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.image.*;
import javafx.scene.paint.*;
import javafx.scene.text.*;
import javafx.util.Math;
import javafx.scene.transform.Scale;

/**
 * @author Pavel Porvatov
 */

def WIDTH = if (Config.IS_MOBILE) 240 else 1100;
def HEIGHT = if (Config.IS_MOBILE) 320 else 700;

def width = bind stage.scene.width on replace {
    invalidate();
}

def height = bind stage.scene.height on replace {
    invalidate();
}

var scale: Number;

function invalidate() {
    if (height <= 0 or width <= 0) {
        return;
    }

    scale = Math.min(width / WIDTH, height / HEIGHT);
}

def boardView: BoardView = BoardView {
    board: Board {}
    onChanged: function () {
        btnStartGame.disable = boardView.isComputerMove();

        darkScores.content = "{boardView.board.getDarkPiecesCount()}";
        lightScores.content = "{boardView.board.getLightPiecesCount()}";
    }
}

def titleImage = ImageView {
    smooth: true
    image: Config.titleImage
}

def darkImage = ImageView {
    smooth: true
    image: Config.darkImage
}

def darkScores: Text = Text {
    translateX: bind darkImage.translateX +
        (Config.darkImage.width - darkScores.boundsInLocal.width) / 2 - 2
    translateY: bind darkImage.translateY +
        (Config.darkImage.height - darkScores.boundsInLocal.height) / 2 - 1
    content: "2"
    textOrigin: TextOrigin.TOP
    fill: Color.WHITE
    font: Font {
        size: if (Config.IS_MOBILE) 10 else 24
        name: "Bitstream Vera Sans Bold"
    }
}

def lightImage = ImageView {
    smooth: true
    image: Config.lightImage
}

def lightScores: Text = Text {
    translateX: bind lightImage.translateX +
        (Config.lightImage.width - lightScores.boundsInLocal.width) / 2 - 2
    translateY: bind lightImage.translateY +
        (Config.lightImage.height - lightScores.boundsInLocal.height) / 2 - 1
    content: "2"
    textOrigin: TextOrigin.TOP
    fill: Color.BLACK
    font: bind darkScores.font
}

def group = ToggleGroup {
}

def humanVsComp = RadioButton {
    selected: true
    text: "Human - Computer"
    toggleGroup: group
    font: Font {
        size: if (Config.IS_MOBILE) 8 else 20
        name: "Bitstream Vera Sans Bold"
    }
}

def compVsHuman = RadioButton {
    text: "Computer - Human"
    toggleGroup: group
    font: bind humanVsComp.font
}

def btnStartGame: Button = Button {
    text: "Start Game"
    action: function () {
        boardView.startGame(compVsHuman.selected);
    }
    font: bind humanVsComp.font
}

if (Config.IS_MOBILE) {
    boardView.translateX = 0;
    boardView.translateY = 80;

    titleImage.translateX = 5;
    titleImage.translateY = 5;
    titleImage.preserveRatio = true;
    titleImage.fitWidth = 120;
    titleImage.fitHeight = 70;

    darkImage.translateX = 20;
    darkImage.translateY = 45;

    lightImage.translateX = 50;
    lightImage.translateY = 45;

    humanVsComp.translateX = 130;
    humanVsComp.translateY = 5;

    compVsHuman.translateX = 130;
    compVsHuman.translateY = 25;

    btnStartGame.translateX = 130;
    btnStartGame.translateY = 45;
} else {
    boardView.translateX = 50;
    boardView.translateY = 50;

    titleImage.translateX = 740;
    titleImage.translateY = 150;

    darkImage.translateX = 810;
    darkImage.translateY = 280;

    lightImage.translateX = 880;
    lightImage.translateY = 280;

    humanVsComp.translateX = 750;
    humanVsComp.translateY = 350;

    compVsHuman.translateX = 750;
    compVsHuman.translateY = 390;

    btnStartGame.translateX = 850;
    btnStartGame.translateY = 430;
}

def stage: Stage = Stage {
    title: "Reversi"
    fullScreen: "{__PROFILE__}" != "browser"

    scene: Scene {
        fill: LinearGradient {
            proportional: true
            startX: 0
            endX: 0
            startY: 0
            endY: 1
            stops: [
                Stop{ offset: 0 color: Color.web("#B6B6B6") },
                Stop{ offset: 1 color: Color.web("#121212") }
            ]
        }

        width: if (Config.IS_MOBILE) WIDTH else 600
        height: if (Config.IS_MOBILE) HEIGHT else 400
        content: [
            Group {
                transforms: [
                    Scale {
                        x: bind scale
                        y: bind scale
                        pivotX: 0
                        pivotY: 0
                    }
                ]

                content: [
                    boardView,

                    titleImage,

                    darkImage,
                    darkScores,

                    lightImage,
                    lightScores,

                    humanVsComp,
                    compVsHuman,
                    btnStartGame
                ]
            }
        ]
    }
}