<ion-header>
    <ion-navbar core-back-button>
        <ion-title>{{ 'addon.mod_lesson.detailedstats' | translate }}</ion-title>
    </ion-navbar>
</ion-header>
<ion-content>
    <ion-refresher [enabled]="loaded" (ionRefresh)="doRefresh($event)">
        <ion-refresher-content pullingText="{{ 'core.pulltorefresh' | translate }}"></ion-refresher-content>
    </ion-refresher>

    <core-loading [hideUntil]="loaded">
        <ion-list *ngIf="student">
            <!-- Student data. -->
            <a ion-item text-wrap core-user-link [userId]="student.id" [courseId]="courseId" [title]="student.fullname">
                <ion-avatar core-user-avatar [user]="student" item-start [userId]="student.id" [courseId]="courseId"></ion-avatar>
                <h2>{{student.fullname}}</h2>
                <core-progress-bar [progress]="student.bestgrade"></core-progress-bar>
            </a>

            <!-- Retake selector if there is more than one retake. -->
            <ion-item text-wrap *ngIf="student.attempts && student.attempts.length > 1">
                <ion-label id="addon-mod_lesson-retakeslabel">{{ 'addon.mod_lesson.attemptheader' | translate }}</ion-label>
                <ion-select [(ngModel)]="selectedRetake" (ionChange)="changeRetake(selectedRetake)" aria-labelledby="addon-mod_lesson-retakeslabel" interface="action-sheet">
                    <ion-option *ngFor="let retake of student.attempts" [value]="retake.try">{{retake.label}}</ion-option>
                </ion-select>
            </ion-item>

            <!-- Retake stats. -->
            <div *ngIf="retake && retake.userstats && retake.userstats.gradeinfo" class="addon-mod_lesson-userstats">
                <ion-item text-wrap>
                    <ion-row>
                        <ion-col>
                            <p class="item-heading">{{ 'addon.mod_lesson.grade' | translate }}</p>
                            <p>{{ 'core.percentagenumber' | translate:{$a: retake.userstats.grade} }}</p>
                        </ion-col>

                        <ion-col>
                            <p class="item-heading">{{ 'addon.mod_lesson.rawgrade' | translate }}</p>
                            <p>{{ retake.userstats.gradeinfo.earned }} / {{ retake.userstats.gradeinfo.total }}</p>
                        </ion-col>
                    </ion-row>
                </ion-item>
                <ion-item text-wrap>
                    <p class="item-heading">{{ 'addon.mod_lesson.timetaken' | translate }}</p>
                    <p>{{ retake.userstats.timetakenReadable }}</p>
                </ion-item>
                <ion-item text-wrap>
                    <p class="item-heading">{{ 'addon.mod_lesson.completed' | translate }}</p>
                    <p>{{ retake.userstats.completed * 1000 | coreFormatDate }}</p>
                </ion-item>
            </div>

            <!-- Not completed, no stats. -->
            <ion-item text-wrap *ngIf="retake && (!retake.userstats || !retake.userstats.gradeinfo)">
                {{ 'addon.mod_lesson.notcompleted' | translate }}
            </ion-item>

            <!-- Pages. -->
            <ng-container *ngIf="retake">
                <!-- The "text-dimmed" class does nothing, but the same goes for the "dimmed" class in Moodle. -->
                <ion-card *ngFor="let page of retake.answerpages" class="addon-mod_lesson-answerpage" [ngClass]="{'text-dimmed': page.grayout}">
                    <ion-card-header text-wrap>
                        <h2>{{page.qtype}}: {{page.title}}</h2>
                    </ion-card-header>
                    <ion-item text-wrap>
                        <p class="item-heading">{{ 'addon.mod_lesson.question' | translate }}</p>
                        <p><core-format-text [component]="component" [componentId]="lesson.coursemodule" [maxHeight]="50" [text]="page.contents"></core-format-text></p>
                    </ion-item>
                    <ion-item text-wrap>
                        <p class="item-heading">{{ 'addon.mod_lesson.answer' | translate }}</p>
                    </ion-item>
                    <ion-item text-wrap *ngIf="!page.answerdata || !page.answerdata.answers || !page.answerdata.answers.length">
                        <p>{{ 'addon.mod_lesson.didnotanswerquestion' | translate }}</p>
                    </ion-item>
                    <div *ngIf="page.answerdata && page.answerdata.answers && page.answerdata.answers.length" class="addon-mod_lesson-answer">
                        <div *ngFor="let answer of page.answerdata.answers">
                            <ion-item text-wrap *ngIf="page.isContent">
                                <!-- Content page, display a button and the content. -->
                                <ion-row>
                                    <ion-col>
                                        <button ion-button block text-wrap color="light" [disabled]="true">{{ answer[0].buttonText }}</button>
                                    </ion-col>
                                    <ion-col>
                                        <p [innerHTML]="answer[0].content"></p>
                                    </ion-col>
                                </ion-row>
                            </ion-item>

                            <div *ngIf="page.isQuestion">
                                <!-- Question page, show the right input for the answer. -->

                                <!-- Truefalse or matching. -->
                                <ion-item text-wrap *ngIf="answer[0].isCheckbox" [ngClass]="{'addon-mod_lesson-highlight': answer[0].highlight}">
                                    <ion-label>
                                        <p><core-format-text [component]="component" [componentId]="lesson.coursemodule" [text]="answer[0].content"></core-format-text></p>
                                        <ion-badge *ngIf="answer[1]" color="dark">
                                            <core-format-text [component]="component" [componentId]="lesson.coursemodule" [text]="answer[1]"></core-format-text>
                                        </ion-badge>
                                    </ion-label>
                                    <ion-checkbox [attr.name]="answer[0].name" [ngModel]="answer[0].checked" [disabled]="true" item-end>
                                    </ion-checkbox>
                                </ion-item>

                                <!-- Short answer or numeric. -->
                                <ion-item text-wrap *ngIf="answer[0].isText">
                                    <p>{{ answer[0].value }}</p>
                                    <ion-badge *ngIf="answer[1]" color="dark">
                                        <core-format-text [component]="component" [componentId]="lesson.coursemodule" [text]="answer[1]"></core-format-text>
                                    </ion-badge>
                                </ion-item>

                                <!-- Matching. -->
                                <ion-item text-wrap *ngIf="answer[0].isSelect">
                                    <ion-row>
                                        <ion-col>
                                            <p><core-format-text [component]="component" [componentId]="lesson.coursemodule" [text]=" answer[0].content"></core-format-text></p>
                                        </ion-col>
                                        <ion-col>
                                            <p>{{answer[0].value}}</p>
                                            <ion-badge *ngIf="answer[1]" color="dark">
                                                <core-format-text [component]="component" [componentId]="lesson.coursemodule" [text]="answer[1]"></core-format-text>
                                            </ion-badge>
                                        </ion-col>
                                    </ion-row>
                                </ion-item>

                                <!-- Essay or couldn't determine. -->
                                <ion-item text-wrap *ngIf="!answer[0].isCheckbox && !answer[0].isText && !answer[0].isSelect">
                                    <p><core-format-text [component]="component" [componentId]="lesson.coursemodule" [text]="answer[0]"></core-format-text></p>
                                    <ion-badge *ngIf="answer[1]" color="dark">
                                        <core-format-text [component]="component" [componentId]="lesson.coursemodule" [text]="answer[1]"></core-format-text>
                                    </ion-badge>
                                </ion-item>
                            </div>

                            <ion-item text-wrap *ngIf="!page.isContent && !page.isQuestion">
                                <!-- Another page (end of branch, ...). -->
                                <p><core-format-text [component]="component" [componentId]="lesson.coursemodule" [text]="answer[0]"></core-format-text></p>
                                <ion-badge *ngIf="answer[1]" color="dark">
                                    <core-format-text [component]="component" [componentId]="lesson.coursemodule" [text]="answer[1]"></core-format-text>
                                </ion-badge>
                            </ion-item>
                        </div>

                        <ion-item text-wrap *ngIf="page.answerdata.response">
                            <p class="item-heading">{{ 'addon.mod_lesson.response' | translate }}</p>
                            <p><core-format-text [component]="component" [componentId]="lesson.coursemodule" [text]="page.answerdata.response"></core-format-text></p>
                        </ion-item>
                        <ion-item text-wrap *ngIf="page.answerdata.score">
                            <p>{{page.answerdata.score}}</p>
                        </ion-item>
                    </div>
                </ion-card>
            </ng-container>
        </ion-list>
    </core-loading>
</ion-content>
