<!-- Buttons to add to the header. -->
<core-navbar-buttons slot="end">
    <ion-button fill="clear" (click)="openModuleSummary()" aria-haspopup="true" [attr.aria-label]="'core.info' | translate">
        <ion-icon name="fas-circle-info" slot="icon-only" aria-hidden="true"></ion-icon>
    </ion-button>
</core-navbar-buttons>

<!-- Content. -->
<core-loading [hideUntil]="!showLoading">

    <!-- Activity info. -->
    <core-course-module-info [module]="module" [description]="description" [component]="component" [componentId]="componentId"
        [courseId]="courseId" [hasDataToSync]="buttonText && hasOffline" (completionChanged)="onCompletionChange()">
    </core-course-module-info>

    <!-- Access rules description messages. -->
    <ion-card *ngIf="gradeMethodReadable || accessRules.length || syncTime">
        <ion-list>
            <ion-item class="ion-text-wrap" *ngFor="let rule of accessRules">
                <ion-label>
                    <p>{{ rule }}</p>
                </ion-label>
            </ion-item>
            <ion-item class="ion-text-wrap" *ngIf="gradeMethodReadable">
                <ion-label>
                    <p class="item-heading">{{ 'addon.mod_quiz.grademethod' | translate }}</p>
                    <p>{{ gradeMethodReadable }}</p>
                </ion-label>
            </ion-item>
            <ion-item class="ion-text-wrap" *ngIf="bestGrade && bestGrade.gradetopass && quiz && quiz.gradeFormatted">
                <ion-label>
                    <p class="item-heading">{{ 'core.grades.gradepass' | translate }}</p>
                    <p>{{ 'addon.mod_quiz.outof' | translate: { $a: {
                        grade: bestGrade.gradetopass,
                        maxgrade: quiz.gradeFormatted
                        } } }}</p>
                </ion-label>
            </ion-item>
            <ion-item class="ion-text-wrap" *ngIf="syncTime">
                <ion-label>
                    <p class="item-heading">{{ 'core.lastsync' | translate }}</p>
                    <p>{{ syncTime }}</p>
                </ion-label>
            </ion-item>
        </ion-list>
    </ion-card>

    <!-- List of user attempts. -->
    <ion-card class="addon-mod_quiz-table" *ngIf="quiz && attempts.length">
        <ion-card-header class="ion-text-wrap">
            <ion-card-title>{{ 'addon.mod_quiz.summaryofattempts' | translate }}</ion-card-title>
        </ion-card-header>
        <ion-card-content role="table">
            <!-- "Header" of the table -->
            <ion-item class="ion-text-wrap addon-mod_quiz-table-header hide-detail" [detail]="true">
                <ion-label role="rowgroup">
                    <ion-row class="ion-align-items-center" role="row">
                        <ion-col class="ion-text-center" *ngIf="quiz.showAttemptColumn" role="columnheader">
                            <strong class="ion-hide-md-up" aria-hidden="true">#</strong>
                            <span class="sr-only ion-hide-md-up">{{ 'addon.mod_quiz.attemptnumber' | translate }}</span>
                            <strong class="ion-hide-md-down">{{ 'addon.mod_quiz.attemptnumber' | translate }}</strong>
                        </ion-col>
                        <ion-col size="7" role="columnheader">
                            <strong>{{ 'addon.mod_quiz.attemptstate' | translate }}</strong>
                        </ion-col>
                        <ion-col class="ion-text-center ion-hide-md-down" *ngIf="quiz.showMarkColumn" role="columnheader">
                            <strong>{{ 'addon.mod_quiz.marks' | translate }} / {{ quiz.sumGradesFormatted }}</strong>
                        </ion-col>
                        <ion-col class="ion-text-center" *ngIf="quiz.showGradeColumn" role="columnheader">
                            <strong>{{ 'addon.mod_quiz.grade' | translate }} / {{ quiz.gradeFormatted }}</strong>
                        </ion-col>
                    </ion-row>
                </ion-label>
            </ion-item>
            <div role="rowgroup">
                <!-- List of attempts. -->
                <ion-item button detail="true" *ngFor="let attempt of attempts" class="ion-text-wrap"
                    [ngClass]='{"addon-mod_quiz-highlighted": attempt.highlightGrade}' [attr.aria-label]="'core.seemoredetail' | translate"
                    (click)="viewAttempt(attempt.id)">
                    <ion-label>
                        <ion-row class="ion-align-items-center" role="row">
                            <ion-col class="ion-text-center" *ngIf="quiz.showAttemptColumn && attempt.preview" role="cell">
                                {{ 'addon.mod_quiz.preview' | translate }}
                            </ion-col>
                            <ion-col class="ion-text-center" *ngIf="quiz.showAttemptColumn && !attempt.preview" role="cell">
                                {{ attempt.attempt }}
                            </ion-col>
                            <ion-col size="7" role="cell">
                                <p *ngFor="let sentence of attempt.readableState">{{ sentence }}</p>
                            </ion-col>
                            <ion-col class="ion-text-center ion-hide-md-down" *ngIf="quiz.showMarkColumn" role="cell">
                                <p>{{ attempt.readableMark }}</p>
                            </ion-col>
                            <ion-col class="ion-text-center" *ngIf="quiz.showGradeColumn" role="cell">
                                <p>{{ attempt.readableGrade }}</p>
                            </ion-col>
                        </ion-row>
                    </ion-label>
                </ion-item>
            </div>
        </ion-card-content>
    </ion-card>

    <!-- Result info. -->
    <ion-card *ngIf="quiz && showResults &&
        (gradeResult || gradeOverridden || gradebookFeedback || (quiz.showFeedbackColumn && overallFeedback))">
        <ion-list>
            <ion-item class="ion-text-wrap" *ngIf="gradeResult">
                <ion-label>{{ gradeResult }}</ion-label>
            </ion-item>
            <ion-item class="ion-text-wrap" *ngIf="gradeOverridden">
                <ion-label>{{ 'core.course.overriddennotice' | translate }}</ion-label>
            </ion-item>
            <ion-item class="ion-text-wrap" *ngIf="gradebookFeedback">
                <ion-label>
                    <p class="item-heading">{{ 'addon.mod_quiz.comment' | translate }}</p>
                    <p>
                        <core-format-text [component]="component" [componentId]="componentId" [text]="gradebookFeedback"
                            contextLevel="module" [contextInstanceId]="module.id" [courseId]="courseId">
                        </core-format-text>
                    </p>
                </ion-label>
            </ion-item>
            <ion-item class="ion-text-wrap" *ngIf="quiz.showFeedbackColumn && overallFeedback">
                <ion-label>
                    <p class="item-heading">{{ 'addon.mod_quiz.overallfeedback' | translate }}</p>
                    <p>
                        <core-format-text [component]="component" [componentId]="componentId" [text]="overallFeedback" contextLevel="module"
                            [contextInstanceId]="module.id" [courseId]="courseId">
                        </core-format-text>
                    </p>
                </ion-label>
            </ion-item>
        </ion-list>
    </ion-card>

    <!-- More data. -->
    <ng-container *ngIf="quiz">
        <!-- Error messages. -->
        <ion-card class="core-danger-card addon-mod_quiz-prevent-messages">
            <ion-item class="ion-text-wrap" *ngFor="let message of preventMessages">
                <ion-label>
                    <p>{{ message }}</p>
                </ion-label>
            </ion-item>
        </ion-card>
        <ion-card class="core-danger-card addon-mod_quiz-no-questions" *ngIf="quiz.hasquestions === 0">
            <ion-item class="ion-text-wrap">
                <ion-label>
                    <p>{{ 'addon.mod_quiz.noquestions' | translate }}</p>
                </ion-label>
            </ion-item>
        </ion-card>
        <ion-card class="core-danger-card addon-mod_quiz-unsupported-questions"
            *ngIf="!hasSupportedQuestions && unsupportedQuestions.length">
            <ion-item class="ion-text-wrap">
                <ion-label>
                    <p class="item-heading">{{ 'addon.mod_quiz.errorquestionsnotsupported' | translate }}</p>
                    <p *ngFor="let type of unsupportedQuestions"
                        class="addon-mod_quiz-unsupported-question addon-mod_quiz-unsupported-question-{{type}}">
                        {{ type }}
                    </p>
                </ion-label>
            </ion-item>
        </ion-card>
        <ion-card class="core-danger-card addon-mod_quiz-unsupported-rules" *ngIf="unsupportedRules.length">
            <ion-item class="ion-text-wrap">
                <ion-label>
                    <p class="item-heading">{{ 'addon.mod_quiz.errorrulesnotsupported' | translate }}</p>
                    <p *ngFor="let name of unsupportedRules"
                        class="addon-mod_quiz-unsupported-rule addon-mod_quiz-unsupported-rule-{{name}}">
                        {{ name }}
                    </p>
                </ion-label>
            </ion-item>
        </ion-card>
        <ion-card class="core-danger-card addon-mod_quiz-unsupported-behaviours" *ngIf="behaviourSupported === false">
            <ion-item class="ion-text-wrap">
                <ion-label>
                    <p class="item-heading">{{ 'addon.mod_quiz.errorbehaviournotsupported' | translate }}</p>
                    <p class="addon-mod_quiz-unsupported-behaviour">{{ quiz.preferredbehaviour }}</p>
                </ion-label>
            </ion-item>
        </ion-card>

        <!-- Other warnings. -->
        <ion-card class="core-warning-card" *ngIf="hasSupportedQuestions && unsupportedQuestions.length">
            <ion-item class="ion-text-wrap">
                <ion-label>
                    <p class="item-heading">{{ 'addon.mod_quiz.canattemptbutnotsubmit' | translate }}</p>
                    <p>{{ 'addon.mod_quiz.warningquestionsnotsupported' | translate }}</p>
                    <p *ngFor="let type of unsupportedQuestions">{{ type }}</p>
                </ion-label>
            </ion-item>
        </ion-card>
    </ng-container>

    <div collapsible-footer *ngIf="!showLoading" slot="fixed">
        <div class="list-item-limited-width" *ngIf="quiz">
            <!-- Button to start/continue. -->
            <ion-button *ngIf="buttonText" expand="block" (click)="attemptQuiz()" class="ion-margin ion-text-wrap"
                [disabled]="showStatusSpinner">
                <ion-spinner *ngIf="showStatusSpinner" slot="start" aria-hidden="true"></ion-spinner>
                {{ buttonText | translate }}
            </ion-button>

            <!-- Button to open in browser if it cannot be attempted in the app. -->
            <ion-button class="ion-text-wrap ion-margin" *ngIf="!buttonText && ((!hasSupportedQuestions && unsupportedQuestions.length) ||
                unsupportedRules.length || behaviourSupported === false)" expand="block" [href]="module.url" core-link
                [showBrowserWarning]="false">
                {{ 'core.openinbrowser' | translate }}
                <ion-icon name="fas-up-right-from-square" slot="end" aria-hidden="true"></ion-icon>
            </ion-button>
        </div>

        <core-course-module-navigation [courseId]="courseId" [currentModuleId]="module.id">
        </core-course-module-navigation>

    </div>
</core-loading>
