<?php

include("database_queries.php");

function check_login($con) {
    
    if(isset($_SESSION['user_id'])) {
        
        $id = $_SESSION['user_id'];
        if(check_user_id($con, $id)) {
            return get_user_data_id($con, $id);
        } else {
            unset($_SESSION['user_id']);
            header("Location: login.php");
            die;
        }
    } else {
        header("Location: login.php");
        die;
    }
}

function generate_salt() {
    return substr(bin2hex(random_bytes(128)), 0, 128);
}

function generate_user_id($username, $salt) {
    $uname = $username . $salt;
    return hash('sha3-512', $uname);
}

function generate_password_hash($password, $salt) {
    $pword = $password . $salt;
    return hash('sha3-512', $pword);
}

function load_stations_table($con) {
    $stmt = get_stations_all($con);
    foreach($stmt->fetchAll() as $row) {
        echo "<tr id=\"" . $row['s_id'] . "\">";
        echo "<td>" . $row['name'] . "</td>";
        echo "<td>" . $row['standort'] . "</td>";
        echo "</tr>";
    }
}

function load_teams_table($con) {
    $stmt = get_teams($con);
    foreach($stmt->fetchAll() as $row) {
        echo "<tr id=\"" . $row['m_id'] . "\" >";
        echo "<td>" . $row['name'] . "</td>";
        echo "<td>" . $row['feuerwehr'] . "</td>";
        echo "</tr>";
    }
}

function load_total_score($con) {
    echo "<thead> \n
    <th scope=\"col\">Mannschaft</th>\n
    <th scope=\"col\">Feuerwehr</th>\n
    <th scope=\"col\">Gesamtpunkte</th>\n
    </thead>\n
    <tbody>\n";
    $stmt = get_total_score($con);
    foreach($stmt->fetchAll() as $row) {
        echo "<tr>\n";
        echo "<td>" . $row['Mannschaftsname'] . "</td>\n";
        echo "<td>" . $row['Feuerwehr'] . "</td>\n";
        echo "<td>" . $row['Gesamtpunkte'] . "</td>\n";
        echo "</tr>\n";
    }
    echo "</tbody>\n";
}

function load_options_stations($con, $session, $stats) {
    $stmt = get_stations_all($con);
    if ($session == "total-score"){
        $total_score_selected = " selected";
    } else {
        $total_score_selected = "";
    }
    if($stats) {
        echo "<option value=\"total-score\"" . $total_score_selected . ">Gesamtpunkte</option>";
    }
    foreach($stmt->fetchAll() as $option) {
        if($session == $option['s_id']){
            $station_selected = " selected";
        } else {
            $station_selected = "";
        }
        echo "<option value=\"" . $option['s_id'] . "\"" . $station_selected . ">" . $option['name'] . "</option>";
    }
}

function load_station_table($con, $s_id) {
    echo "<thead> \n
    <th scope=\"col\">Mannschaft</th>
    <th scope=\"col\">Feuerwehr</th>
    <th scope=\"col\">Punkte</th>
    <th scope=\"col\">Zeit</th>
    </thead>
    <tbody>\n";
    $stmt = get_station($con, $s_id);
    foreach($stmt->fetchAll() as $row) {
        echo "<tr id=\"" . $row['m_id'] . "\" class=\"row\">\n";
        echo "<td>" . $row['Name'] . "</td>\n";
        echo "<td>" . $row['Feuerwehr'] . "</td>\n";
        echo "<td>" . $row['Punkte'] . "</td>\n";
        if ($row['Zeit'] != NULL) {
            $time = get_time_str($con, $row['m_id'], $s_id);
            echo "<td>" . $time . "</td>\n";
        } else {
            echo "<td>" . $row['Zeit'] . "</td>\n";
        }
        echo "</tr>\n";
    }
    echo "</tbody>\n";
}

function load_teams_no_points($con, $s_id) {
    if (station_exists($con, $s_id)) {
        $stmt = get_teams_no_points($con, $s_id);
        foreach($stmt->fetchAll() as $option) {
            echo "<option value=\"" . $option['m_id'] . "\">" . $option['name'] . " " . $option['feuerwehr'] . "</option>";
        }
    }
}

function load_fire_departments($con) {
    $stmt = get_fire_departments($con);
    foreach($stmt->fetchAll() as $option) {
        echo "<option>".$option['feuerwehr']."</option>\n";
    }
}

function check_admin($con) {
    $user_data = check_login($con);
    if($user_data['user_group'] == "admin") {
        return $user_data;
    } else {
        unset($_SESSION['user_id']);
        unset($_SESSION['user_group']);
        header("Location: login.php");
        die;
    }
}

function load_users($con) {
    $stmt = get_users($con);
    foreach($stmt->fetchAll() as $row) {
        echo "<tr id=\"" . $row['id'] . "\" >\n";
        echo "<td>" . $row['user_name'] . "</td>\n";
        echo "<td>" . $row['user_group'] . "</td>\n";
        echo "<td>";
        if ($row['s_id'] != NULL) { 
            echo get_station_all($con, $row['s_id'])->fetch()['name']; 
        } else { 
            echo $row['s_id']; 
        }
        echo "</td>\n";
        echo "</tr>\n";
    }
}

function check_time($con, $m_id, $s_id) {
    $stmt = get_time($con, $m_id, $s_id);
    if (($stmt->fetch()) == NULL) {
        return false;
    } else {
        return true;
    }
}

function get_time_str($con, $m_id, $s_id) {
    $minutes = get_minutes($con, $m_id, $s_id)->fetch()['minutes'];
    $seconds = get_seconds($con, $m_id, $s_id)->fetch()['seconds'];
    $millis = get_millis($con, $m_id, $s_id)->fetch()['millis'];
    if ($minutes < 10) {
        $time = "0" . $minutes;
    } else {
        $time = $minutes;
    }

    if ($seconds < 10) {
        $time .= ":0" . $seconds;
    } else {
        $time .= ":" . $seconds;
    }

    $millis /= 10000;
    
    if ($millis < 10) {
        $time .= ".0" . $millis;
    } else {
        $time .= "." . $millis;
    }
    return $time;
}