<ion-header class="ion-no-border">
  <app-header></app-header>
</ion-header>

<ion-content class="height-100">
  <ion-refresher  name="refresher" slot="fixed" (ionRefresh)="doRefresh($event)">
    <ion-progress-bar type="indeterminate" *ngIf="showLoader"></ion-progress-bar>
    <ion-refresher-content>
    </ion-refresher-content>
  </ion-refresher>
  <div class="main-content d-flex height-100">
      <!-- Aside left -->
      <div class="aside-wrapper d-flex flex-column flex-grow-1">
       <!--  <p class="text-center mt-0 aside-title px-20">Chat</p> -->
          <div class="title-content">
            <div class="div-title">
              <ion-label class="title">Chat</ion-label>
            </div>
            <div class="div-icon">
              <button class="btn-no-color" (click)="openContactsPage()">
                <ion-icon slot="end" src="assets/images/icons-chat-new-conversation.svg"></ion-icon>
              </button>
              <button class="btn-no-color" (click)="openNewGroupPage()">
                <ion-icon slot="end" src="assets/images/icons-chat-new-group.svg" ></ion-icon>
              </button>
              <button *ngIf="hideRefreshBtn" class="btn-no-color" (click)="refreshing()">
                <ion-icon class="title-icon" name="reload-circle"></ion-icon>
              </button>
            </div>
          </div>
        <ion-toolbar>
          <ion-segment [(ngModel)]="segment" (ionChange)="onSegmentChange()">
            <ion-segment-button value="Contactos">
              Conversas
            </ion-segment-button>
            <ion-segment-button value="Grupos">
              Grupos
            </ion-segment-button>
          </ion-segment>
        </ion-toolbar>
        <div class=" aside overflow-y-auto d-flex flex-wrap flex-grow-1">
        <div class="width-100" [ngSwitch]="segment">
          <ion-list *ngSwitchCase="'Contactos'">
              <ion-item-sliding>
                  <div class="item width-100 d-flex ion-no-padding ion-no-margin"
                  *ngFor="let dm of userDirectMessages"
                  [class.item-active]="dm._id ==idSelected">
                    <div class="item-icon">
                      <ion-icon class="icon" slot="start" src="assets/images/icons-chat-chat-40.svg"></ion-icon>
                    </div>
                    <div [class.highlight]="dm._id =='cjFv5XfreKz5j3fWW'"
                     (click)="openMessagesPage(dm._id)"
                     class="item-content flex-grow-1"><!-- (click)="openMessages(dm)" -->
                      <div class="item-title-time">
                        <div class="item-title" [class.item-title-active]="dm._id == idSelected">
                          <ion-label *ngFor="let user of dm.uids">
                            <span *ngIf="user !=loggedUserChat.me.username">
                              <div *ngFor="let t of dmUsers">
                                <div *ngIf="user == t._id">
                                  {{t.name}}
                                </div>
                              </div>
                          </span>
                          </ion-label>
                        </div>
                        <div class="item-date" [class.item-date-active]="dm._id == idSelected">{{showDateDuration(dm._updatedAt)}}</div>
                      </div>
                      <div class="item-description" [class.item-description-active]="dm._id == idSelected">
                        <ion-label *ngIf="dm.lastMessage">{{dm.lastMessage.msg}}</ion-label>
                      </div>
                    </div>
                  </div>
              </ion-item-sliding>
          </ion-list>
          <ion-list *ngSwitchCase="'Grupos'" >
              <ion-item-sliding>
                  <div *ngFor="let group of allGroups"
                  [class.item-active]="group._id ==idSelected"
                  class="item d-flex">
                    <div class="item-icon">
                      <ion-icon class="icon" slot="start" src="assets/images/icons-chat-group-chat-40.svg"></ion-icon>
                    </div>
                    <div
                     (click)="openGroupMessagesPage(group._id)" class="item-content flex-grow-1">
                      <div class="item-title-time">
                        <div class="item-title" [class.item-title-active]="group._id ==idSelected">
                          <ion-label>{{group.name.split('-').join(' ')}}</ion-label>
                        </div>
                        <div class="item-date" [class.item-date-active]="group._id ==idSelected" *ngIf="group.lastMessage">{{group.lastMessage._updatedAt | date: 'HH:mm'}}</div>
                      </div>
                      <div class="item-description" [class.item-description-active]="group._id ==idSelected" *ngIf="group.lastMessage">
                        <ion-label>{{group.lastMessage.u.name}}: {{group.lastMessage.msg}}</ion-label>
                      </div>
                    </div>
                  </div>
              </ion-item-sliding>
          </ion-list>
        </div>
       <!--  <button (click)="sendMsg()" style="height: 41px;">Send message</button> -->
        </div>
      </div>
      <!-- Aside right -->
      <div class="aside-content d-none flex-column height-100">
        <app-empty-chat [texto]="emptyTextDescription"
          class="height-100 flex-column"
          [style.display]="showEmptyComponent ? 'flex' : 'none'"
          #messagecontainer>
        </app-empty-chat>
        <app-messages class=" height-100 flex-column"
          *ngIf="showMessages"
          [style.display]="showMessages ? 'flex' : 'none'"
          (closeAllDesktopComponents)="closeAllDesktopComponents()"
          (showEmptyContainer)="showEmptyContainer()"
          (showEmptyContainer)="showEmptyContainer()"
          (openNewEventPage)="openNewEventPage($event)"
          [style.display]="showMessages ? 'flex' : 'none'"
          [roomId]="roomId"
          [showMessages]="showMessages"  #messagecontainer>

        </app-messages>

        <app-contacts
          (openMessage)="openMessagesPage($event)"
          *ngIf="showContacts"
          [style.display]="showContacts ? 'flex' : 'none'"
           class=" height-100 flex-column">
        </app-contacts>

        <app-new-group
          (addGroupMessage)="openGroupContactsPage($event)"
          [style.display]="showNewGroup ? 'flex' : 'none'"
           class=" height-100 flex-column">
        </app-new-group>

        <app-edit-group [roomId]="roomId"
          (closeAllDesktopComponents)="closeAllDesktopComponents()"
          (openGroupMessage)="openGroupMessagesPage($event)"
          *ngIf="showEditGroup"
          [style.display]="showEditGroup ? 'flex' : 'none'"
           class="height-100 flex-column">
        </app-edit-group>

        <app-group-contacts
          (openGroupMessage)="openGroupMessagesPage($event)"
          *ngIf="showGroupContacts"
          [style.display]="showGroupContacts ? 'flex' : 'none'"
          [roomId]="groupRoomId" class=" height-100 flex-column"
          >
        </app-group-contacts>

        <app-group-messages
          *ngIf="showGroupMessages"
        [style.display]="showEmptyComponent ? 'flex' : 'none'"
          (closeAllDesktopComponents)="closeAllDesktopComponents()"
          (showEmptyContainer)="showEmptyContainer()"
          (openGroupContacts)="openGroupContactsPage($event)"
          (openEditGroupPage)="openEditGroupPage($event)"
          (openNewEventPage)="openNewEventPage($event)"
          [style.display]="showGroupMessages ? 'flex' : 'none'"
          class=" height-100 flex-column"
          [roomId]="roomId" #messagecontainer>
        </app-group-messages>

        <app-new-event
          [profile]=""
          [selectedSegment]=segment
          [taskParticipants]="taskParticipants"
          [taskParticipantsCc]="taskParticipantsCc"
          [selectedDate]="eventSelectedDate"
          [eventAttendees]="contacts"
          (onAddEvent)="closeNewEventComponent()"
          (openAttendeesComponent)="openAttendeesComponent($event)"
          (cloneAllmobileComponent)="closeNewEventComponent()"
          [style.display]="showNewEvent ? 'flex' : 'none'"
          [roomId]="groupRoomId"
          class=" height-100 flex-column">
        </app-new-event>

        <app-attendee-modal
          [adding]="adding"
          [taskParticipants]="taskParticipants"
          [taskParticipantsCc]="taskParticipantsCc"
          (closeComponent)="closeAttendeesComponent()"
          (setIntervenient)="setIntervenient($event)"
          (setIntervenientCC)="setIntervenientCC($event)"
          (setContact)="setContact($event)"
          [style.display]="showAttendees ? 'flex' : 'none'"
          [footer]="true"
          class=" height-100 flex-column">
        </app-attendee-modal>

      </div>
  </div>
</ion-content>


