package primefactors;

import javafx.stage.*;
import javafx.scene.*;
import javafx.scene.paint.*;
import javafx.scene.shape.*;
import javafx.ext.swing.*;
import javafx.scene.layout.*;
import javafx.scene.text.*;

import java.lang.System;
import java.lang.Math;

/** 
 * Adapted from code at:
 * http://www.itwriting.com/primetest/countprimes.html
 * On my macbook: 
 *                Safari 3.1.2 = 6.487 sec
 *                     Flash 9 = 1.253 sec
 *           Silverlight 2beta = 0.488 sec
 * JavaFX preview release =      0.415 sec
 * 
 * The findPrimes method uses the Java Math library to scan
 * the values between 1 and n to discover which ones are
 * prime values.  It returns the number of prime values found
 */
function findPrimes(n:Integer):Integer {
    var j:Integer;
    var limit:Number;
    var numprimes:Integer = 1; //2 is prime
    var i:Integer = 3;
    while(i <= n) {
        var isPrime = true;
        limit = Math.ceil(Math.sqrt(i)) + 1;

        j = 3;
        while(j < limit) {
            if(i mod j == 0) {
                isPrime = false;
                break;
            }
            j+=2;
        }
        i+=2;
        if(not isPrime) {
            continue;
        }
        if(isPrime) {
            numprimes++;
        }
    }

    return numprimes;
}
  
  
    Stage {
        scene: Scene {

            var labelFont = Font.font("", FontWeight.BOLD, 12); // define font for the form labels
            var titleFont = Font.font("", FontWeight.BOLD, 14); // define font for the form title
            var maxNumber = SwingTextField { text: "1000000" translateX: 120 translateY: 90 columns:9 horizontalAlignment: SwingHorizontalAlignment.RIGHT background: Color.LIGHTBLUE  borderless: true};
            var result = Text { content: "- - - -" fill: Color.DARKSLATEGRAY x: 120 y: 47};  // initial text that appears in the result number field
            var elapsed = Text { content: "- - - -" fill: Color.DARKSLATEGRAY x: 120 y: 76}; // initial text that appears in the elapsed time field

            content: [
            // create a nice background box for the form
                Rectangle { width: 250 height: 170
                    /*
                    fill: LinearGradient {
                        startX: 0 endX: 0
                        stops: [
                            Stop { color: Color.WHITESMOKE offset: 0 },
                            Stop { color: Color.LIGHTGRAY offset: 1 }
                        ]  }*/
                    fill: Color.web("#cccccc");
                },
                SwingLabel { text: "Counting Primes" font: titleFont foreground: Color.DARKSLATEGRAY translateX: 68 translateY: 6},
                SwingLabel { text: "Primes Found: " font: labelFont  translateX: 20 translateY: 35},
            result,  // show the result value
                SwingLabel { text: "Elapsed Time:" font: labelFont translateX: 20 translateY:63},
            elapsed, // show the elapsed time
                SwingLabel { text: "Enter Maximum: " font: labelFont translateX: 20 translateY: 90},
            maxNumber, // show the field that allows the user to enter the maximum number defining the search
                SwingButton {
                    translateX: 114
                    translateY: 113
                    text: "Start Counting!"
                    action: function() {
                    // we're running in a single thread so we can capture the start time
                        var start =
                        System.currentTimeMillis() as Integer;
                    // do the computation to figure out how many primes there are in the range 1..maxNumber
                        var numprimes = findPrimes(java.lang.Integer.parseInt(maxNumber.text));
                    // now get the time when we return
                        var end = System.currentTimeMillis() as Integer;
                    // show the results on the form
                        result.content = "{numprimes} primes";
                        elapsed.content = "{(end - start) / 1000.0} seconds";
                    }
                }

            ]
        }
    }