/* 
 * 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 tweeter.view;

import javafx.scene.control.Button;
import javafx.scene.control.TextBox;
import javafx.scene.Group;
import javafx.scene.image.ImageView;
import javafx.scene.Node;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.scene.text.TextOrigin;
import tweeter.view.AbstractView;
import tweeter.view.TweeterView;

/**
 * @author Rakesh Menon
 */

public class LoginView extends AbstractView {

    override var show = true on replace {
        if(visible) {
            userTextBox.requestFocus();
        }
    };

    var logo = ImageView {
        translateX: 20
        translateY: 35
        image: TweeterView.theme.logoImage
    };
    
    var line = Line {
        startX: 5
        startY: 100
        endX: TweeterView.width - 5
        endY: 100
        stroke: bind Color.web("#{TweeterView.theme.linkColor}")
    };

    var userText = Text {
        x: 20
        y: 150
        font: Font { name: "Bitstream Vera Sans Bold" size: 11 }
        fill: bind Color.web("#{TweeterView.theme.linkColor}")
        content: "User Name:"
        textOrigin: TextOrigin.TOP
    };

    var userTextBox = TextBox {
        translateX: 100
        translateY: 143
        width: 115
        blocksMouse: false
    }

    var passwdText = Text {
        x: 20
        y: 180
        font: Font { name: "Bitstream Vera Sans Bold" size: 11 }
        fill: bind Color.web("#{TweeterView.theme.linkColor}")
        content: "Password:"
        textOrigin: TextOrigin.TOP
    };

    var passwdTextBox : TextBox = TextBox {
        translateX: 100
        translateY: 173
        width: 115
        blocksMouse: false
        action: function() {
            login();
        }
    };

    var starText : Text = Text {
        x: bind passwdTextBox.translateX + 5
        y: bind passwdTextBox.translateY + 9
        font: Font { name: "Bitstream Vera Sans Bold" size: 11 }
        fill: bind Color.web("#{TweeterView.theme.linkColor}")
        textOrigin: TextOrigin.TOP
        clip: Rectangle {
            x: bind starText.x
            y: bind starText.y
            width: passwdTextBox.layoutBounds.width - 15
            height: bind passwdTextBox.boundsInLocal.height
        }
    };

    var starBGRect = Rectangle {
        x: bind passwdTextBox.translateX + 2
        y: bind passwdTextBox.translateY + 2
        width: bind passwdTextBox.width - 4
        height: bind passwdTextBox.height - 4
        fill: Color.WHITE
    };

    public var onLogin: function(user : String, passwd : String) = null;
    
    var loginButton = Button {
        translateX: 45
        translateY: 220
        text: "Login"
        width: 70
        blocksMouse: false
        action: function() {
            login();
        }
    };
    
    var cancelButton = Button {
        translateX: 125
        translateY: 220
        text: "Cancel"
        width: 70
        blocksMouse: false
    };

    var stars = "************************************";
    var star = bind passwdTextBox.rawText on replace {
        if(passwdTextBox.rawText.length() < stars.length()) {
            starText.content = "{stars.substring(0, passwdTextBox.rawText.length())}";
        }
    };

    function login() : Void {

        if(onLogin == null) {
            return;
        }

        userTextBox.commit();
        passwdTextBox.commit();
        onLogin(userTextBox.text, passwdTextBox.text);
        passwdTextBox.text = "";
    }
    
    override function create() : Node {

        Group {
            content: [
                logo, line, userText, userTextBox,
                passwdText, passwdTextBox, starBGRect, starText,
                loginButton, cancelButton
            ]
        }
    }
}