<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.io.BufferedReader, java.io.InputStreamReader, java.io.IOException, java.io.StringWriter, java.io.PrintWriter" %>
<html>
<head>
    <title>Ladejobs</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0; /* Ensure no extra margin */
            padding: 0; /* Ensure no extra padding */
            overflow: visible; /* Ensure content is fully visible */
        }

        table {
            width: 100%; /* Ensure table takes full width */
            border-collapse: collapse;
            font-size: small;
        }
        table, th, td {
            border: 1px solid black;
        }
        th, td {
            padding: 7px;
            text-align: left;
            white-space: normal;
        }
        th {
            background-color: #f2f2f2;
        }
        td {
            background-color: white;
        }

        .container {
            display: flex;
            flex-direction: row; /* Align items vertically */
            align-items: flex-start;
            width: 100%; /* Ensure container takes full width */
            height: 100%; /* Ensure container takes full height */
            overflow: visible; /* Ensure content is fully visible */
        }

        .traffic-light-container {
            margin-top: 10px;
            margin-left: 20px; /* Adjust the margin as needed */
        }

        .traffic-light {
            width: 40px; /* Reduced width */
            background-color: #333;
            padding: 10px; /* Reduced padding */
            border-radius: 10px;
            display: flex;
            flex-direction: column;
            align-items: center;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
        }

        .light {
            width: 30px; /* Reduced width */
            height: 30px; /* Reduced height */
            margin: 5px 0; /* Reduced margin */
            border-radius: 50%;
            background-color: #555;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
        }

        .light.red {
            background-color: rgb(70, 0, 0);
        }

        .light.red.on {
            background-color: red;
            box-shadow: 0 0 10px rgba(255, 0, 0, 0.5);
        }

        .light.yellow {
            background-color: rgb(70, 70, 0);
        }

        .light.yellow.on {
            background-color: yellow;
            box-shadow: 0 0 10px rgba(255, 255, 0, 0.5);
        }

        .light.green {
            background-color: rgb(0, 70, 0);
        }

        .light.green.on {
            background-color: rgb(0, 255, 0);
            box-shadow: 0 0 10px rgba(0, 255, 0, 0.5);
        }
    </style>
</head>
<body>
    <div class="container">
        <div>
            <%
                boolean allSuccess = true;
                boolean anyRunning = false;
                try {
                    Runtime runtime = Runtime.getRuntime();

                    // Execute the system command
                    String[] commands = {"/bin/sh", "-c", "cd ~/alex_skripte/laderoutine && laderoutine.sh"};
                    Process process = runtime.exec(commands);

                    // Read the output of the command
                    BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
                    String line;

                    // Start HTML table
                    out.println("<table>");
                    out.println("<tr><th>Modul</th><th>Start</th><th>Ende</th><th>Status</th></tr>");

                    // Process each line and add to the table
                    while ((line = reader.readLine()) != null) {
                        // Split the line into columns
                        String[] columns = line.split("\\t");
                        out.println("<tr>");
                        for (String column : columns) {
                            out.println("<td>" + column + "</td>");
                        }
                        out.println("</tr>");

                        // Check the status column (assuming it's the fourth column)
                        String status = columns[3].trim();
                        if (status.equals("Fehler")) {
                            allSuccess = false;
                        } else if (status.equals("läuft gerade...")) {
                            anyRunning = true;
                        }
                    }

                    // End HTML table
                    out.println("</table>");

                    // Read the error stream of the command
                    BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
                    while ((line = errorReader.readLine()) != null) {
                        out.println("ERROR: " + line);
                    }

                    // Wait for the command to complete
                    int exitCode = process.waitFor();
                    // out.println("<p>Exited with code: " + exitCode + "</p>");
                } catch (IOException | InterruptedException e) {
                    StringWriter sw = new StringWriter();
                    e.printStackTrace(new PrintWriter(sw));
                    out.println("Error: " + sw.toString());
                }
            %>
        </div>
        <div class="traffic-light-container">
            <div class="traffic-light">
                <div class="light red <% if (!allSuccess) { %>on<% } %>"></div>
                <div class="light yellow <% if (anyRunning && allSuccess) { %>on<% } %>"></div>
                <div class="light green <% if (allSuccess && !anyRunning) { %>on<% } %>"></div>
            </div>
        </div>
    </div>
</body>
</html>