/**
 * /script/tester.js
 * @version 1.3
 * @desc Script file for the tester page (not the actual sQuizEngine yet. :P)
 * @author Fándly Gergő Zoltán (gergo@systemtest.tk, systemtest.tk)
 * @copy 2017 Fándly Gergő Zoltán
 * License:
    sQuiz for creating small jQuery based quizs in an implementable way
    Copyright (C) 2017  Fándly Gergő Zoltán

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
 **/

function showSelector(){
    $("#list").slideUp();
    $("#fromfile").slideUp();
    $("#fromurl").slideUp();
    $("#sQuiz").slideUp();
    $("#menuwrapper").slideDown();
    $("#back").attr("onclick", "window.history.back()");
}

function loadFromList(){
    $("#back").attr("onclick", "showSelector()");
    $("#menuwrapper").slideUp();
    $.getJSON("./tests/main.json", function(json){
        var list;
        
        if(!json.locallist){
            //get list from server
            $.getJSON("./engine/engine.php?list", function(json){
                list=json;
                $("#listcontent").html("");
                $.each(list, function(key, val){
                    var push="<tr>";
                    push+="<td>"+(parseInt(key)+1).toString()+"</td>";
                    push+="<td>"+val.name+"</td>";
                    push+="<td>"+val.description+"</td>";
                    push+="<td><button type=\"button\" onclick=\"startQuiz('"+val.file+"')\">"+lang.solve+"</td>";
                    push+="</tr>";
                    $("#listcontent").append(push);
                });
                $("#list table").footable();
                $("#list").slideDown();
            });
        }
        else{
            //get list from locallist
            list=json.available;
            $("#listcontent").html("");
            $.each(list, function(key, val){
                var push="<tr>";
                push+="<td>"+(parseInt(key)+1).toString()+"</td>";
                push+="<td>"+val.name+"</td>";
                push+="<td>"+val.description+"</td>";
                push+="<td><button type=\"button\" onclick=\"startQuiz('"+val.file+"')\">"+lang.solve+"</td>";
                push+="</tr>";
                $("#listcontent").append(push);
            });
            $("#list table").footable();
            $("#list").slideDown();
        }
    });
}

function loadFromFile(){
    $("#back").attr("onclick", "showSelector()");
    $("#menuwrapper").slideUp();
    $("#fromfile").slideDown();
}

function loadFromURL(){
    $("#back").attr("onclick", "showSelector()");
    $("#menuwrapper").slideUp();
    $("#fromurl").slideDown();
}

function openQuizFromFile(){
    var file=$("#fromfile").children("input[type=file]")[0].files[0];
    var fr=new FileReader();
    fr.onload=function(){
        openQuizFromFileCallback(fr.result);
    };
    fr.readAsText(file, "utf-8");
}
function openQuizFromFileCallback(res){
    $("#fromfile").slideUp();
    startQuizStr(res);
}

function openQuizFromURL(){
    $("#fromurl").slideUp();
    startQuiz($("input[name=testurl]").val());
}

//load quiz
function startQuiz(path){
    $("#menuwrapper").slideUp();
    $("#list").slideUp();
    var sQuiz;
    $.ajax({
        url: path,
        type: "GET",
        success: function(res){
            startQuizStr(res);
        }
    });
}
function startQuizStr(quizstr){
    sQuiz=new $.sQuiz($("#sQuiz"), quizstr);
    sQuiz.setRegionalization(lang);
    sQuiz.setQuizDoneCallback(function(res, quiz){
        console.log(res);
    });
    sQuiz.setQuizDoneOptions({
        download: true,
        upload: true,
        upload_url: "./engine/engine.php"
    });
    sQuiz.init();
}
