<ion-header>
    <ion-navbar core-back-button>
        <ion-title>{{ 'addon.messages.messagepreferences' | translate }}</ion-title>
    </ion-navbar>
</ion-header>
<ion-content>
    <ion-refresher [enabled]="preferencesLoaded" (ionRefresh)="refreshPreferences($event)">
        <ion-refresher-content pullingText="{{ 'core.pulltorefresh' | translate }}"></ion-refresher-content>
    </ion-refresher>
    <core-loading [hideUntil]="preferencesLoaded">
        <!-- Contactable privacy. -->
        <ion-card>
            <ion-item *ngIf="!advancedContactable">
              <ion-label>{{ 'addon.messages.blocknoncontacts' | translate }}</ion-label>
              <ion-toggle [(ngModel)]="contactablePrivacy" (ionChange)="saveContactablePrivacy(contactablePrivacy)"></ion-toggle>
            </ion-item>

            <ion-list *ngIf="advancedContactable" text-wrap radio-group [(ngModel)]="contactablePrivacy" (ionChange)="saveContactablePrivacy(contactablePrivacy)">
                <ion-item-divider>{{ 'addon.messages.contactableprivacy' | translate }}</ion-item-divider>
                <ion-item>
                    <ion-label>{{ 'addon.messages.contactableprivacy_onlycontacts' | translate }}</ion-label>
                    <ion-radio [value]="onlyContactsValue"></ion-radio>
                </ion-item>
                <ion-item>
                    <ion-label>{{ 'addon.messages.contactableprivacy_coursemember' | translate }}</ion-label>
                    <ion-radio [value]="courseMemberValue"></ion-radio>
                </ion-item>
                <ion-item *ngIf="allowSiteMessaging">
                    <ion-label>{{ 'addon.messages.contactableprivacy_site' | translate }}</ion-label>
                    <ion-radio [value]="siteValue"></ion-radio>
                </ion-item>
            </ion-list>
        </ion-card>

        <!-- Notifications. -->
        <ng-container *ngIf="preferences">
            <div *ngFor="let component of preferences.components">
                <ion-card list *ngFor="let notification of component.notifications">
                    <ion-item-divider text-wrap>
                        <ion-row no-padding>
                            <ng-container *ngIf="!groupMessagingEnabled">
                                <ion-col no-padding>{{ notification.displayname }}</ion-col>
                                <ion-col col-2 text-center no-padding class="hidden-phone">{{ 'core.settings.loggedin' | translate }}</ion-col>
                                <ion-col *ngIf="!groupMessagingEnabled" col-2 text-center no-padding class="hidden-phone">{{ 'core.settings.loggedoff' | translate }}</ion-col>
                            </ng-container>
                            <ng-container *ngIf="groupMessagingEnabled">
                                <ion-col no-padding>{{ 'addon.notifications.notificationpreferences' | translate }}</ion-col>
                            </ng-container>
                        </ion-row>
                    </ion-item-divider>
                    <ng-container *ngFor="let processor of notification.processors">
                        <!-- If group messaging is enabled, display a simplified view. -->
                        <ng-container *ngIf="groupMessagingEnabled">
                            <ion-item text-wrap>
                                <ion-label>{{ processor.displayname }}</ion-label>
                                <ion-spinner item-end *ngIf="!preferences.disableall && notification.updating"></ion-spinner>
                                <ion-toggle item-end *ngIf="!preferences.disableall && !processor.locked" [(ngModel)]="processor.checked" (ionChange)="changePreference(notification, '', processor)" [disabled]="notification.updating">
                                </ion-toggle>
                                <ion-note item-end *ngIf="!preferences.disableall && processor.locked">{{ processor.lockedmessage }}</ion-note>
                                <ion-note item-end *ngIf="preferences.disableall">{{ 'core.settings.disabled' | translate }}</ion-note>
                            </ion-item>
                        </ng-container>

                        <ng-container *ngIf="!groupMessagingEnabled">
                            <!-- Tablet view -->
                            <ion-row text-wrap class="hidden-phone" align-items-center>
                                <ion-col margin-horizontal>{{ processor.displayname }}</ion-col>
                                <ion-col col-2 text-center *ngFor="let state of ['loggedin', 'loggedoff']">
                                    <!-- If notifications not disabled, show toggle. -->
                                    <ion-spinner [hidden]="preferences.disableall || !(notification.updating && notification.updating[state])"></ion-spinner>
                                    <ion-toggle *ngIf="!preferences.disableall && !processor.locked" [(ngModel)]="processor[state].checked" (ionChange)="changePreference(notification, state, processor)" [disabled]="notification.updating && notification.updating[state]">
                                    </ion-toggle>
                                    <div padding class="text-gray" *ngIf="!preferences.disableall && processor.locked">{{'core.settings.locked' | translate }}</div>
                                    <!-- If notifications are disabled, show "Disabled" instead of toggle. -->
                                    <span *ngIf="preferences.disableall">{{ 'core.settings.disabled' | translate }}</span>
                                </ion-col>
                            </ion-row>
                            <!-- Phone view -->
                            <ion-list-header text-wrap class="hidden-tablet">{{ processor.displayname }}</ion-list-header>
                            <!-- If notifications not disabled, show toggles. If notifications are disabled, show "Disabled" instead of toggle. -->
                            <ion-item *ngFor="let state of ['loggedin', 'loggedoff']" text-wrap class="hidden-tablet">
                                <ion-label>{{ 'core.settings.' + state | translate }}</ion-label>
                                <ion-spinner item-end *ngIf="!preferences.disableall && (notification.updating && notification.updating[state])"></ion-spinner>
                                <ion-toggle item-end *ngIf="!preferences.disableall && !processor.locked" [(ngModel)]="processor[state].checked" (ionChange)="changePreference(notification, state, processor)" [disabled]="notification.updating && notification.updating[state]">
                                </ion-toggle>
                                <ion-note item-end *ngIf="!preferences.disableall && processor.locked">{{'core.settings.locked' | translate }}</ion-note>
                                <ion-note item-end *ngIf="preferences.disableall">{{ 'core.settings.disabled' | translate }}</ion-note>
                            </ion-item>
                        </ng-container>
                    </ng-container>
                </ion-card>
            </div>
        </ng-container>

        <!-- General settings. -->
        <ion-card>
            <ion-list text-wrap>
                <ion-item-divider>{{ 'core.settings.general' | translate }}</ion-item-divider>
                <ion-item text-wrap>
                    <ion-label>
                        <h2>{{ 'addon.messages.useentertosend' | translate }}</h2>
                        <p *ngIf="isDesktop && !isMac">{{ 'addon.messages.useentertosenddescdesktop' | translate }}</p>
                        <p *ngIf="isDesktop && isMac">{{ 'addon.messages.useentertosenddescmac' | translate }}</p>
                    </ion-label>
                    <ion-toggle [(ngModel)]="sendOnEnter" (ngModelChange)="sendOnEnterChanged()"></ion-toggle>
                </ion-item>
            </ion-list>
        </ion-card>
    </core-loading>
</ion-content>
