<?php
include "classes/DatabaseConnection.php";
// return JSON string of the request and results for list of words
echo '{"request_type":"words_list"';
if ($_GET["from_language"]) {
    $from_language = $_GET["from_language"];
    echo ',"parm_from_language":"' . $from_language . '"';
} else {
    $from_language = 2; // default Greek
    echo ',"parm_from_language":"dft-' . $from_language . '"';
}

if ($_GET["to_language"]) {
    $to_language = $_GET["to_language"];
    echo ',"parm_to_language":"' . $to_language . '"';
} else {
    $to_language = 1; // default English
    echo ',"parm_to_language":"dft-' . $to_language . '"';
}


if ($_GET["randomise"]) {
    ($_GET["randomise"] == "1") ? $randomise = TRUE : $randomise = FALSE;
    echo ',"parm_randomise":"' . $_GET["randomise"] . '"';
} else {
    $randomise = FALSE; // default alphabetical order
    echo ',"parm_randomise":"dft-false"';
}
// read words and linked meanings from database
$db_conn = DatabaseConnection::getConnection();


if ($randomise) {
    $query = "SELECT fw.id as from_word_id, fw.word as from_word, m.to_word_id, m.to_word, m.meaning_id, m.part_of_speech, m.part_of_speech_abbr FROM words fw LEFT JOIN v_meanings m on fw.id = m.from_word_id WHERE fw.is_deleted = '0' and fw.language_id = $from_language and nvl(m.language_id, $to_language) = $to_language ORDER BY rand()";
} else {
    $query = "SELECT fw.id as from_word_id, fw.word as from_word, m.to_word_id, m.to_word, m.meaning_id, m.part_of_speech, m.part_of_speech_abbr FROM words fw LEFT JOIN v_meanings m on fw.id = m.from_word_id WHERE fw.is_deleted = '0' and fw.language_id = $from_language and nvl(m.language_id, $to_language) = $to_language ORDER BY fw.word COLLATE utf8mb4_general_ci";
}

$result = $db_conn->connection->query($query);

// initialise variables
$first_time = TRUE;
$prv_word_id = "";
$word_details = "";
$meaning_cell = "";

// start array of words 
echo ',"words":[';

foreach ($result as $row) {
    // when "from word" changes...
    if ($row['from_word_id'] != $prv_word_id) {
        // close array of meanings and object bracket of previous word if not first time
        if (!$first_time) {
            echo ']},';
        }
        // set up details of "from word"
        echo '{"type":"word","word_id":"' . $row['from_word_id'] . '","word_language":"' . $from_language . '","word":"' . $row['from_word'] . '","to_language":"' . $to_language . '","meanings":[';
        // not all words have meanings set up...
        if ($row['to_word']) {
            echo '{"from_word_id":"' . $row['from_word_id'] . '","to_word_id":"' . $row['to_word_id'] . '","to_word":"' . $row['to_word'] . '","meaning_id":"' . $row['meaning_id'] . '","meaning_language":"' . $to_language . '","part_of_speech":"' . $row['part_of_speech'] . '","part_of_speech_abbr":"' . $row['part_of_speech_abbr'] . '"}';
        }
        $prv_word_id = $row['from_word_id'];
    } else {
        // rows with same word_id should have a meaning (non-null to_word) but check anyway
        if ($row['to_word']) {
            echo ',{"from_word_id":"' . $row['from_word_id'] . '","to_word_id":"' . $row['to_word_id'] . '","to_word":"' . $row['to_word'] . '","meaning_id":"' . $row['meaning_id'] . '","meaning_language":"' . $to_language . '","part_of_speech":"' . $row['part_of_speech'] . '","part_of_speech_abbr":"' . $row['part_of_speech_abbr'] . '"}';        }
    }
    $first_time = FALSE;
}

// output last word's details
if (!$first_time) {
    // close array of meanings and object bracket of previous word if not first time
    if (!$first_time) {
        echo ']}';
    }
}

// close array of words and request object
echo ']}';
?>