<ion-header>
  <ion-toolbar color="primary">
    <ion-buttons slot="start">
      <ion-back-button defaultHref="/session"></ion-back-button>
    </ion-buttons>
    <ion-title>{{ contact?.get('name') || contact?.get('to')?.get("nickname") ||contact?.get('to')?.get('username') }}</ion-title>
  </ion-toolbar>
</ion-header>

<ion-content class="chat-content">
  <ion-list lines="none">
    <ion-item *ngFor="let message of messageList" class="message-item">
      <ion-avatar slot="end" *ngIf="message?.get('sendUser')?.id === user?.id" class="message-avatar sent-avatar">
        <img [src]="message?.get('sendUser')?.get('avatarUrl')">
      </ion-avatar>
      <ion-label class="message-container"
                 [class.sent-message-container]="message?.get('sendUser')?.id === user?.id"
                 [class.received-message-container]="message?.get('receiveUser')?.id === user?.id">
        <div class="message-details"
             [ngClass]="{'sent-message-details': message?.get('sendUser')?.id === user?.id, 'received-message-details': message?.get('receiveUser')?.id === user?.id}">
          <p class="message-text">{{ message?.get("contentJson")?.content }}</p>
          <div class="message-time">
            <p>{{ message.createdAt|date:'HH:mm:ss' }}</p>
          </div>
        </div>
      </ion-label>
      <ion-avatar slot="start" *ngIf="message?.get('receiveUser')?.id === user?.id" class="message-avatar received-avatar">
        <img [src]="message?.get('receiveUser')?.get('avatarUrl')">
      </ion-avatar>
    </ion-item>
  </ion-list>
</ion-content>

<ion-footer>
  <ion-toolbar>
    <ion-input placeholder="Type your message..." [(ngModel)]="newMessage" clearInput></ion-input>
    <ion-buttons slot="end">
      <ion-button (click)="sendMessage()" color="primary" [disabled]="!newMessage">Send</ion-button>
    </ion-buttons>
  </ion-toolbar>
</ion-footer>