import { GradingSchema } from "./gradingschema";
import { Results, gradeFromPoints } from "./results";
import { Table } from "../table";

function firstUpper(s: string) {
    if (!s) {
        return "";
    }
    if (s.length == 1) {
        return s.toUpperCase;
    }
    return s.charAt(0).toUpperCase() + s.slice(1);
}

export function toTable(results: Results, gradingSchema: GradingSchema) {

    const table = new Table([[
        "term", "course", "exam",
        "submissionID", "name",
        "submitted",
        "grade", "points", "rawPoints", "coveragePoints",
        "coverage", 
        "studentTests",
        "graded",
        "correction"
    ]]);

    let row = 1;

    table.addTextToRow(row, "skippedMsg");


    results.studentResults.forEach(studentResult => {
        row++;

        table.setAt("term", row, gradingSchema.term);
        table.setAt("course", row, gradingSchema.course);
        table.setAt("exam", row, gradingSchema.exam);

        table.setAt("submissionID", row, studentResult.submissionId);
        table.setAt("name", row, studentResult.userName);

        if (studentResult.noSubmission) {
            table.setAt("submitted", row, "0");
            return;
        }
        table.setAt("submitted", row, "1");

        table.setAt("grade", row, studentResult.grade);
        table.setAt("points", row, studentResult.points);
        table.setAt("rawPoints", row, studentResult.pointsRaw);
        table.setAt("coveragePoints", row, studentResult.pointsCoverage)

        if (studentResult.coverageResult.skipped()) {
            table.setAt("coverage", row, -1);
            table.concatAt("skippedMsg", row, studentResult.coverageResult.skipMessage);
        } else {
            table.setAt("coverage", row, studentResult.coverageResult.stmtCoverage);
        }

        if (studentResult.userTests) {
            table.setAt("studentTests", row, studentResult.userTestsCount);
            // table.setAt("eigeneTestsSuccess", row, ergStudent.userTestsSuccess);
            // table.setAt("eigeneTestsFailure", row, ergStudent.userTestsFailureOrError);
            // table.setAt("eigeneTestsCount", row, ergStudent.userTestsCount);
        } else {
            table.setAt("studentTests", row, 0);
        }

        if (studentResult.comment.length > 0) {
            table.concatAt("skippedMsg", row, studentResult.comment);
        }

        if (studentResult.skipped()) {
            table.setAt("graded", row, -1)
            table.concatAt("skippedMsg", row, studentResult.skipMessage);
        } else {
            table.setAt("graded", row, 1)
            
            studentResult.manualCorrection.forEach(correction => {
                table.concatAt("correction", row, `${correction.reason} (${correction.points})` );
            });


            studentResult.taskResults.forEach(taskResult => {
                const name = taskResult.name;
                // table.setAt(name + "Precondition", row, ergAufgabe.precondition);
                // table.setAt(name + "points", row, ergAufgabe.points);
                if (taskResult.skipped()) {
                    // table.setAt(name + "Bewertet", row, 0);
                    table.concatAt("skippedMsg", row, taskResult.skipMessage);
                } else {

                    // table.setAt(name + "Bewertet", row, 1);

                    // let bewSuccess = 0;
                    // let bewFailure = 0;
                    // let bewNotFound = 0;

                    taskResult.gradingResults.forEach(gradingResult => {
                        if (gradingResult.skipped()) {
                            // table.setAt(name + "Bewertet", row, 0);
                            table.concatAt("skippedMsg", row, gradingResult.skipMessage);
                        } else {
                            // ergBew.testResults.forEach(test => {
                            //     switch (test.testStatus) {
                            //         case "error":
                            //         case "failure": bewFailure++; break;
                            //         case "success": bewSuccess++; break;
                            //         case "notFound": bewNotFound++; break;
                            //     }
                            // })
                        }
                    });

                    // table.setAt(name + "Success", row, bewSuccess);
                    // table.setAt(name + "Failure", row, bewFailure);
                    // table.setAt(name + "NotFound", row, bewNotFound);

                }
                taskResult.manualCorrections.forEach(correction => {
                    table.concatAt("correction", row, `${name}: ${correction.reason} (${correction.points})` );
                });
            });

        }


    });

    return table;
}