<?php

class ViewSubmissionUtils {

    /**
     * generates a short summary html
     *
     * @param int assignmentId assignment
     * @param int submissionId submission to create a report for
     * @return string html
     */
    public static function generateSummaryHtml(
        int $assignmentId,
        int $submissionId
    ): string {

        // fetch data
        $summary = DbUtils::getResultSummaryFromDatabase($assignmentId, $submissionId);
        $html = "";

        // calculate success rate, if no unknown result states or compilation errors
        $successRate = "?";
        if ($summary->unknownCount() == 0 && $summary->compilationErrorCount() == 0) {
            $successRate = round(($summary->successfulCount() / $summary->resultCount()) * 100, 2 );
        }

        // generate html
        $html .= $summary->successfulCount() . "/";
        $html .= ($summary->compilationErrorCount() == 0 && $summary->unknownCount() == 0)
            ? $summary->resultCount() . " (" . $successRate . "%)"
                : "?";
        $html .= " tests successful<br>";

        if ($summary->compilationErrorCount() > 0) {
            $html .= $summary->compilationErrorCount() . " compilation error(s)<br>";
        }

        if ($summary->unknownCount() > 0) {
            $html .= $summary->unknownCount() . " test(s) with unknown state<br>";
        }
		
		$html .= $summary->successfulTestCompetencyProfile . " successfully tested competency profile<br>";

        return html_writer::div($html, "dtaSubmissionSummary");
    }

    /**
     * generates detailed view html
     *
     * @param int assignmentId assignment
     * @param int submissionId submission to create a report for
     */
    public static function generateDetailHtml(
        int $assignmentId,
        int $submissionId
    ): string {

        // fetch data
        $summary = DbUtils::getResultSummaryFromDatabase($assignmentId, $submissionId);
        $html = "";

        // define a few css classes and prepare html attribute arrays
        $tableHeaderRowAttributes = array("class" => "dtaTableHeaderRow");
        $tableRowAttributes = array("class" => "dtaTableRow");
        $resultRowAttributes = $tableRowAttributes;
        $unknownAttrib = 'dtaResultUnknown';
        $successAttrib = 'dtaResultSuccess';
        $failureAttrib = 'dtaResultFailure';
        $compErrorAttrib = 'dtaResultCompilationError';

        // summary table
        $tmp = "";
        $tmp .= html_writer::tag("th", "Summary", array("class" => "dtaTableHeader"));
        $tmp .= html_writer::empty_tag("th", array("class" => "dtaTableHeader"));
        $header = html_writer::tag("tr", $tmp, $tableHeaderRowAttributes);
        $header = html_writer::tag("thead", $header);

        $body = "";
        $tmp = "";
        $attributes = array("class" => "dtaTableData");
        $tmp .= html_writer::tag(
            "td",
            "result items in sum",
            $attributes);

        $tmp .= html_writer::tag(
            "td",
            $summary->resultCount(),
            $attributes);

        $resultRowAttributes = $tableRowAttributes;
        $resultRowAttributes['class'] = $resultRowAttributes['class'] . " " . $unknownAttrib;
        
        $body .= html_writer::tag("tr", $tmp, $resultRowAttributes);

        $tmp = "";
        $tmp .= html_writer::tag("td", "successes", $attributes);
        $tmp .= html_writer::tag( "td", $summary->successfulCount(), $attributes);

        $resultRowAttributes = $tableRowAttributes;
        $successRate = "?";

        if ($summary->unknownCount() > 0 || $summary->compilationErrorCount() > 0) {
            $resultRowAttributes['class'] = $resultRowAttributes['class'] . " " . $unknownAttrib;
        } else {
            $successRate = round(($summary->successfulCount() / $summary->resultCount()) * 100, 2 );
            if ($successRate < 50) {
                $resultRowAttributes['class'] = $resultRowAttributes['class'] . " " . $compErrorAttrib;
            } else if ($successRate < 75) {
                $resultRowAttributes['class'] = $resultRowAttributes['class'] . " " . $failureAttrib;
            } else {
                $resultRowAttributes['class'] = $resultRowAttributes['class'] . " " . $successAttrib;
            }
        }
        $body .= html_writer::tag("tr", $tmp, $resultRowAttributes);

        $tmp = "";
        $tmp .= html_writer::tag("td", "failures", $attributes);
        $tmp .= html_writer::tag("td", $summary->failedCount(), $attributes);

        $resultRowAttributes = $tableRowAttributes;
        if ($summary->failedCount() > 0) {
            $resultRowAttributes['class'] = $resultRowAttributes['class'] . " " . $failureAttrib;
        } else {
            $resultRowAttributes['class'] = $resultRowAttributes['class'] . " " . $successAttrib;
        }
        $body .= html_writer::tag("tr", $tmp, $resultRowAttributes);

        $tmp = "";
        $tmp .= html_writer::tag("td", "compilation errors", $attributes);
        $tmp .= html_writer::tag("td", $summary->compilationErrorCount(), $attributes);

        $resultRowAttributes = $tableRowAttributes;
        if ($summary->compilationErrorCount() > 0) {
            $resultRowAttributes['class'] = $resultRowAttributes['class'] . " " . $compErrorAttrib;
        } else {
            $resultRowAttributes['class'] = $resultRowAttributes['class'] . " " . $successAttrib;
        }
        $body .= html_writer::tag("tr", $tmp, $resultRowAttributes);

        $tmp = "";
        $tmp .= html_writer::tag("td", "unknown state", $attributes);
        $tmp .= html_writer::tag("td", $summary->unknownCount(), $attributes);

        $resultRowAttributes = $tableRowAttributes;
        if ($summary->unknownCount() > 0) {
            $resultRowAttributes['class'] = $resultRowAttributes['class'] . " " . $unknownAttrib;
        } else {
            $resultRowAttributes['class'] = $resultRowAttributes['class'] . " " . $successAttrib;
        }
        $body .= html_writer::tag("tr", $tmp, $resultRowAttributes);

        $tmp = "";
        $tmp .= html_writer::tag("td", html_writer::tag("b","success rate"), $attributes);
        $tmp .= html_writer::tag(
            "td",
            html_writer::tag("b", $summary->successfulCount()
                . "/" . (($summary->compilationErrorCount() == 0 && $summary->unknownCount() == 0) ? $summary->resultCount()
                . " (" . $successRate . "%)"
                    : "?")),
            $attributes);

        $resultRowAttributes = $tableRowAttributes;
        if ($summary->unknownCount() > 0 || $summary->compilationErrorCount() > 0) {
            $resultRowAttributes['class'] = $resultRowAttributes['class'] . " " . $unknownAttrib;
        } else {
            if ($successRate < 50) {
                $resultRowAttributes['class'] = $resultRowAttributes['class'] . " " . $compErrorAttrib;
            } else if ($successRate < 75) {
                $resultRowAttributes['class'] = $resultRowAttributes['class'] . " " . $failureAttrib;
            } else {
                $resultRowAttributes['class'] = $resultRowAttributes['class'] . " " . $successAttrib;
            }
        }
        $body .= html_writer::tag("tr", $tmp, $resultRowAttributes);

        $body = html_writer::tag("tbody", $body);
        $table = html_writer::tag("table", $header . $body, array("class" => "dtaTable"));

        $html .= $table;

        // add empty div for spacing between summary and details table
        $html .= html_writer::empty_tag("div", array("class" => "dtaSpacer"));

        // details table
        $tmp = "";
        $tmp .= html_writer::tag("th", "Details", array("class" => "dtaTableHeader"));
        $tmp .= html_writer::empty_tag("th", array("class" => "dtaTableHeader"));
        $header = html_writer::tag("tr", $tmp, $tableHeaderRowAttributes);
        $header = html_writer::tag("thead", $header);

        $body = "";
        $spacerRow = null;
        foreach($summary->results as $r) {
            // add spacer first, if not null
            if (!is_null($spacerRow)) {
                $body .= $spacerRow;
            }

            // new copy of base attributes array
            $resultRowAttributes = $tableRowAttributes;

            // check which css class to add for the colored left-border according to resuls state
            if ($r->state == 0) {
                $resultRowAttributes['class'] = $resultRowAttributes['class'] . ' dtaResultUnknown';
            } else if ($r->state == 1) {
                $resultRowAttributes['class'] = $resultRowAttributes['class'] . ' dtaResultSuccess';
            } else if ($r->state == 2) {
                $resultRowAttributes['class'] = $resultRowAttributes['class'] . ' dtaResultFailure';
            } else if ($r->state == 3) {
                $resultRowAttributes['class'] = $resultRowAttributes['class'] . ' dtaResultCompilationError';
            }

            $tmp = "";
            $tmp .= html_writer::tag(
                "td",
                "name",
                $attributes);
    
            $tmp .= html_writer::tag(
                "td",
                $r->name,
                $attributes);
            $body .= html_writer::tag("tr", $tmp, $resultRowAttributes);

            $tmp = "";
            $tmp .= html_writer::tag(
                "td",
                "state",
                $attributes);

            $tmp .= html_writer::tag(
                "td",
                DtaResult::getStateName($r->state),
                $attributes);
            $body .= html_writer::tag("tr", $tmp, $resultRowAttributes);

            // if state is something different than successful, show additional rows
            if ($r->state != 1) {
                $tmp = "";
                $tmp .= html_writer::tag(
                    "td",
                    "failure type",
                    $attributes);
        
                $tmp .= html_writer::tag(
                    "td",
                    $r->failureType,
                    $attributes);
                $body .= html_writer::tag("tr", $tmp, $resultRowAttributes);
    
                $tmp = "";
                $tmp .= html_writer::tag(
                    "td",
                    "failure reason",
                    $attributes);
        
                $tmp .= html_writer::tag(
                    "td",
                    $r->failureReason,
                    $attributes);
                $body .= html_writer::tag("tr", $tmp, $resultRowAttributes);

                // only show line, column and position if they have useful values
                if (!is_null($r->lineNumber) && $r->lineNumber > 0) {
                    $tmp = "";
                    $tmp .= html_writer::tag(
                        "td",
                        "line number",
                        $attributes);

                    $tmp .= html_writer::tag(
                        "td",
                        $r->lineNumber,
                        $attributes);
                    $body .= html_writer::tag("tr", $tmp, $resultRowAttributes);
                }

                if (!is_null($r->columnNumber) && $r->columnNumber > 0) {
                $tmp = "";
                $tmp .= html_writer::tag(
                    "td",
                    "column number",
                    $attributes);

                $tmp .= html_writer::tag(
                    "td",
                    $r->columnNumber,
                    $attributes);
                $body .= html_writer::tag("tr", $tmp, $resultRowAttributes);
                }

                if (!is_null($r->position) && $r->position > 0) {
                $tmp = "";
                $tmp .= html_writer::tag(
                    "td",
                    "position",
                    $attributes);

                $tmp .= html_writer::tag(
                    "td",
                    $r->position,
                    $attributes);
                $body .= html_writer::tag("tr", $tmp, $resultRowAttributes);
                }

                $tmp = "";
                $tmp .= html_writer::tag(
                    "td",
                    "stacktrace",
                    $attributes);

                $tmp .= html_writer::tag(
                    "td",
                    html_writer::tag("details", $r->stacktrace, array("class" => "dtaStacktraceDetails")),
                    $attributes);
                $body .= html_writer::tag("tr", $tmp, $resultRowAttributes);
            }

            // set spacerRow value if null for next rount separation
            if (is_null($spacerRow)) {
                $spacerRow = html_writer::empty_tag("tr", array("class" => "dtaTableSpacer"));
            }
        }
        $html .= html_writer::tag("table", $header . $body, array("class" => "dtaTable"));

        // wrap generated html into final div
        $html = html_writer::div($html, "dtaSubmissionDetails");

        return $html;
    }

}