/* ** This files handles the course's data function(s) ** */
// Animate

// Update stage will render next frame
createjs.Ticker.setFPS(30);
createjs.Ticker.addEventListener("tick", tickHandler);

function tickHandler(e) {
    stage.update();
}

// createjs.Ticker.addEventListener("tick", change_angle);
// createjs.Ticker.addEventListener("tick", change_length);

//Function Call
// Keep sliders and numbers in sync
$("#power_f").change(function() {
    $("#num_power_f").val($(this).val());
    calc();
});

$("#num_power_f").change(function() {
    $("#power_f").val($(this).val());
    calc();
});

$("#length").change(function() {
    $("#num_length").val($(this).val());
    calc();
});

$("#num_length").change(function() {
    $("#length").val($(this).val());
    calc();
});

$("#angle").change(function() {
    $("#num_angle").val($(this).val());
    calc();
});

$("#num_angle").change(function() {
    $("#angle").val($(this).val());
    calc();
});
//
function power_f_changed() {
    calc();
}
//
function length_l_changed() {
    calc();
}
//
function angle_changed() {
    calc();
}

// Calculate M = F x d
function calc() {

    // Get Controls Values
    var power_f = $("#num_power_f").val();
    var length_l = $("#num_length").val();
    var angle = $("#num_angle").val();
    var results = $("#calc");
    //var results_icon = $("#calc_icon");
    var moxlobraxionas;
    var distance_d;
    //
    var moxlobraxionas = $("#moxlobraxionas");
    var ropi = $("#ropi");

    //Check that the boxes (sliders) have value greater than zero
    if (power_f < 5000 && power_f != 0 || power_f > 30000 && power_f != 0) {
        alert("Η Δύναμη μπορεί να πάρει τιμές από 5000 Ν έως 30.000 Ν");
    } else if (length_l < 0.1 && length_l != 0 || length_l > 1 && length_l != 0) {
        alert("To Μήκος μπορεί να πάρει τιμές από 0,1 m έως 1 m");
    } else if (angle < 5 && angle != 0 || angle > 15 && angle != 0) {
        alert("Η Γωνία μπορεί να πάρει τιμές από 5 μοίρες έως 15 μοίρες");
    } else if (angle >= 5 || length_l >= 0.1) {
        //remove power check from if and put it in in the calculation as an if statement since it is being used there.
        moveShapes(angle, length_l); //length_l

        console.log(length_l);
        // Converts from degrees to radians.
        Math.radians = function(degrees) {
            return degrees * Math.PI / 180;
        };
        //Calculate d = length
        angle = Math.radians(angle);
        //console.log("Degrees: " + angle);
        angle = Math.sin(angle);

        //console.log("Angle: " + angle);
        distance_d = length_l * angle;
        //Show Results
        mek_force.text = "F = " + power_f + "N";
        //Calculate M = ROPI
        ropi_m = power_f * distance_d;
        //Get 2 decimicals
        ropi_m = ropi_m.toFixed(2);
        distance_d = distance_d.toFixed(5);
        //Make it String
        ropi_m = ropi_m.toString();
        distance_d = distance_d.toString();
        //Replace dot character with comma
        ropi_m = ropi_m.replace(".", ",");
        distance_d = distance_d.replace(".", ",");
        //Results
        moxlobraxionas.html('d = ' + distance_d + ' m ');
        ropi.html('M = ' + ropi_m + " N ∙ m");
        //results.html('<strong>Μοχλοβραχίονας</strong><br/> d = ' + distance_d + ' m <br/> ----------------<br/><strong>Ροπή</strong> <br/> M = ' + ropi_m + " N ∙ m");
        results.css('color', 'green');
        //mek_results.text = ("Μοχλοβραχίονας \n\n d = " + distance_d + ' m \n----------------\nΡοπή\n\n M = ' + ropi_m + " N ∙ m");

    } else {
        results.text('Παρακαλώ επιλέξτε τιμές!');
        results.css('color', 'red');
    }
    return true;
}