<ion-header>
    <ion-toolbar>
        <ion-buttons slot="start">
            <ion-back-button [text]="'core.back' | translate"></ion-back-button>
        </ion-buttons>
        <ion-title>
            <core-format-text [text]="title" [contextLevel]="contextLevel" [contextInstanceId]="instanceId" [courseId]="courseId">
            </core-format-text>
        </ion-title>
        <ion-buttons slot="end">
            <ion-button *ngIf="canDeleteComments" slot="end" fill="clear" (click)="toggleDelete()"
                [attr.aria-label]="'core.toggledelete' | translate">
                <ion-icon name="fas-pen" slot="icon-only" aria-hidden="true"></ion-icon>
            </ion-button>
            <core-context-menu>
                <core-context-menu-item [hidden]="!(commentsLoaded && !hasOffline)" [priority]="100"
                    [content]="'core.refresh' | translate" (action)="refreshComments(false)" [iconAction]="refreshIcon"
                    [closeOnClick]="true">
                </core-context-menu-item>
                <core-context-menu-item [hidden]="!(commentsLoaded && hasOffline)" [priority]="100"
                    [content]="'core.settings.synchronizenow' | translate" (action)="refreshComments(true)" [iconAction]="syncIcon"
                    [closeOnClick]="false">
                </core-context-menu-item>
            </core-context-menu>
        </ion-buttons>
    </ion-toolbar>
</ion-header>
<ion-content>
    <ion-refresher slot="fixed" [disabled]="!commentsLoaded" (ionRefresh)="refreshComments(false, $event.target)">
        <ion-refresher-content pullingText="{{ 'core.pulltorefresh' | translate }}"></ion-refresher-content>
    </ion-refresher>
    <core-loading [hideUntil]="commentsLoaded">
        <core-empty-box *ngIf="!comments || !comments.length" icon="fas-comments"
            [message]="'core.comments.nocomments' | translate">
        </core-empty-box>

        <ion-card class="core-warning-card" *ngIf="hasOffline">
            <ion-item>
                <ion-icon name="fas-exclamation-triangle" slot="start" aria-hidden="true"></ion-icon>
                <ion-label>
                    {{ 'core.thereisdatatosync' | translate:{$a: 'core.comments.comments' | translate | lowercase } }}
                </ion-label>
            </ion-item>
        </ion-card>

        <ion-card *ngIf="offlineComment" (click)="addComment($event)">
            <ion-item class="ion-text-wrap">
                <core-user-avatar [user]="offlineComment" slot="start"></core-user-avatar>
                <ion-label>
                    <h2>{{ offlineComment.fullname }}</h2>
                    <p>
                        <ion-icon name="fas-clock" aria-hidden="true"></ion-icon> {{ 'core.notsent' | translate }}
                    </p>
                </ion-label>
                <ion-button *ngIf="showDelete" slot="end" fill="clear" [@coreSlideInOut]="'fromRight'" color="danger"
                    (click)="deleteComment($event, offlineComment)" [attr.aria-label]="'core.delete' | translate">
                    <ion-icon name="fas-trash" slot="icon-only" aria-hidden="true"></ion-icon>
                </ion-button>
            </ion-item>
            <ion-item class="ion-text-wrap">
                <ion-label>
                    <core-format-text clean="true" [text]="offlineComment.content" [contextLevel]="contextLevel"
                        [contextInstanceId]="instanceId" [courseId]="courseId">
                    </core-format-text>
                </ion-label>
            </ion-item>
        </ion-card>

        <ion-card *ngFor="let comment of comments">
            <ion-item class="ion-text-wrap">
                <core-user-avatar [user]="comment" slot="start"></core-user-avatar>
                <ion-label>
                    <h2>{{ comment.fullname }}</h2>
                    <p *ngIf="!comment.deleted">{{ comment.timecreated * 1000 | coreFormatDate: 'strftimerecentfull' }}</p>
                    <p *ngIf="comment.deleted">
                        <ion-icon name="fas-trash" aria-hidden="true"></ion-icon> <span class="ion-text-wrap">
                            {{ 'core.deletedoffline' | translate }}
                        </span>
                    </p>
                </ion-label>
                <ion-button *ngIf="showDelete && !comment.deleted && comment.delete" slot="end" fill="clear"
                    [@coreSlideInOut]="'fromRight'" color="danger" (click)="deleteComment($event, comment)"
                    [attr.aria-label]="'core.delete' | translate">
                    <ion-icon name="fas-trash" slot="icon-only" aria-hidden="true"></ion-icon>
                </ion-button>
                <ion-button *ngIf="showDelete && comment.deleted" slot="end" fill="clear" color="danger"
                    (click)="undoDeleteComment($event, comment)" [attr.aria-label]="'core.restore' | translate">
                    <ion-icon name="fas-undo-alt" slot="icon-only" aria-hidden="true"></ion-icon>
                </ion-button>
            </ion-item>
            <ion-item class="ion-text-wrap">
                <ion-label>
                    <core-format-text [text]="comment.content" [contextLevel]="contextLevel" [contextInstanceId]="instanceId"
                        [courseId]="courseId">
                    </core-format-text>
                </ion-label>
            </ion-item>
        </ion-card>

        <core-infinite-loading [enabled]="canLoadMore" (action)="loadMore($event)" [error]="loadMoreError"></core-infinite-loading>
    </core-loading>

    <ion-fab slot="fixed" core-fab vertical="bottom" horizontal="end" *ngIf="canAddComments">
        <ion-fab-button (click)="addComment($event)" [attr.aria-label]="'core.comments.addcomment' | translate">
            <ion-icon name="fas-plus" aria-hidden="true"></ion-icon>
        </ion-fab-button>
    </ion-fab>
</ion-content>
