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

<ion-content class="height-100 container-wrapper">
  <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 border-t-radius">
      <!-- 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 *ngIf="ThemeService.currentTheme == 'default' " slot="end" src="assets/images/icons-chat-new-conversation.svg"></ion-icon>
                <ion-icon *ngIf="ThemeService.currentTheme == 'gov' " slot="end" src="assets/images/theme/gov/icons-chat-new-conversation.svg"></ion-icon>
              </button>
              <button class="btn-no-color" (click)="openNewGroupPage()">
                <ion-icon *ngIf="ThemeService.currentTheme == 'default' " slot="end" src="assets/images/icons-chat-new-group.svg" ></ion-icon>
                <ion-icon *ngIf="ThemeService.currentTheme == 'gov' " slot="end" src="assets/images/theme/gov/icons-chat-new-group.svg" ></ion-icon>
              </button>
              <button *ngIf="hideRefreshBtn" class="btn-no-color" (click)="refreshing()">
                <ion-icon class="title-icon font-awesome" 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 item-hover 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 *ngIf="ThemeService.currentTheme == 'default' " class="icon" slot="start" src="assets/images/icons-chat-chat-40.svg"></ion-icon>
                      <ion-icon *ngIf="ThemeService.currentTheme == 'gov' && dm._id != idSelected " class="icon" slot="start" src="assets/images/theme/gov/icons-chat-chat-40.svg"></ion-icon>
                      <ion-icon *ngIf="ThemeService.currentTheme == 'gov' && dm._id == idSelected " class="icon" slot="start" src="assets/images/theme/gov/icons-chat-chat-40-hover.svg"></ion-icon>
                    </div>
                    <div [class.highlight]="dm._id =='cjFv5XfreKz5j3fWW'"
                     (click)="openMessagesPage(dm._id)"
                     class="item-content flex-grow-1 cursor-pointer"><!-- (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 *ngIf="dm.lastMessage" class="item-description" [class.item-description-active]="dm._id == idSelected">
                        <ion-label *ngIf="dm.lastMessage">{{dm.lastMessage.msg}}</ion-label>
                        <ion-label *ngIf="dm.lastMessage.file">
                          <fa-icon *ngIf="dm.lastMessage.file.type != 'application/meeting'" icon="file-alt" class="file-icon" [class.set-active-item-font-to-white]="dm._id == idSelected"></fa-icon>
                          <fa-icon *ngIf="dm.lastMessage.file.type == 'application/meeting'" icon="calendar-alt" class="file-icon" [class.set-active-item-font-to-white]="dm._id == idSelected"></fa-icon>
                          <span> {{dm.lastMessage.file.name || dm.lastMessage.file.subject }}</span>
                        </ion-label>
                        <ion-label *ngIf="dm.lastMessage.attachments">
                          <div *ngIf="dm.lastMessage.attachments[0].image_url">
                            <fa-icon icon="image" class="file-icon" [class.set-active-item-font-to-white]="dm._id == idSelected"></fa-icon>
                            <span> Fotografia</span>
                          </div>
                        </ion-label>
                      </div>
                    </div>
                  </div>
              </ion-item-sliding>
              <ion-item-sliding *ngIf="showLoader || userDirectMessages.length < 1">
                  <div *ngFor="let n of numSequence(8); let i = index;" class="item item-hover width-100 d-flex ion-no-padding ion-no-margin">
                    <div class="item-icon"><ion-icon class="icon" slot="start" src="assets/icon/icons-chat-grey.svg"></ion-icon></div>
                    <div class="item-content flex-grow-1 cursor-pointer">
                      <div class="item-title-time">
                        <div class="item-title"><ion-skeleton-text animated style="width: 50%"></ion-skeleton-text></div>
                        <div class="item-date"><ion-skeleton-text animated></ion-skeleton-text></div>
                      </div>
                      <div class="item-description d-flex align-items-center">
                        <fa-icon icon="file-alt" class="file-icon"></fa-icon>
                        <ion-skeleton-text animated style="width: 95%; margin-left:5%"></ion-skeleton-text>
                      </div>
                    </div>
                  </div>
              </ion-item-sliding>
          </ion-list>
          <ion-list *ngSwitchCase="'Grupos'">
              <ion-item-sliding *ngIf="!showLoader">
                  <div *ngFor="let group of allGroups"
                  [class.item-active]="group._id ==idSelected"
                  class="item item-hover d-flex">
                    <div class="item-icon">
                      <!-- <ion-icon class="icon" slot="start" src="assets/images/icons-chat-group-chat-40.svg"></ion-icon> -->

                      <ion-icon *ngIf="ThemeService.currentTheme == 'default' " class="icon" slot="start" src="assets/images/icons-chat-group-chat-40.svg"></ion-icon>
                      <ion-icon *ngIf="ThemeService.currentTheme == 'gov' && group._id != idSelected " class="icon" slot="start" src="assets/images/theme/gov/icons-chat-group-chat-40.svg"></ion-icon>
                      <ion-icon *ngIf="ThemeService.currentTheme == 'gov' && group._id == idSelected " class="icon" slot="start" src="assets/images/theme/gov/icons-chat-group-chat-40-hover.svg"></ion-icon>
                    </div>
                    <div
                     (click)="openGroupMessagesPage(group._id)" class="item-content flex-grow-1 cursor-pointer">
                      <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.customFields.countDownDate">{{showDateDuration(group._updatedAt)}}</div>
                        <div class="item-date" [class.item-date-active]="group._id ==idSelected" *ngIf="group.customFields.countDownDate">{{countDownDate(group.customFields.countDownDate, group._id)}}</div>
                      </div>
                      <div *ngIf="group.lastMessage" class="item-description d-flex align-items-center" [class.item-description-active]="group._id ==idSelected">
                        <div class="item-message">{{group.lastMessage.u.name}}: {{group.lastMessage.msg}} </div>
                        <div class="item-files add-ellipsis" *ngIf="group.lastMessage.file">
                          <fa-icon *ngIf="group.lastMessage.file.type != 'application/meeting'" icon="file-alt" class="file-icon" [class.set-active-item-font-to-white]="group._id == idSelected"></fa-icon>
                          <fa-icon *ngIf="group.lastMessage.file.type == 'application/meeting'" icon="calendar-alt" class="file-icon" [class.set-active-item-font-to-white]="group._id == idSelected"></fa-icon>
                          <span class="item-files-title"> {{group.lastMessage.file.name || group.lastMessage.file.subject}}</span>
                        </div>
                        <div  class="item-files" *ngIf="group.lastMessage.attachments">
                          <div *ngIf="group.lastMessage.attachments[0].image_url">
                            <fa-icon icon="image" class="file-icon" [class.set-active-item-font-to-white]="group._id == idSelected"></fa-icon>
                            <span> Fotografia</span>
                          </div>
                        </div>
                      </div>
                    </div>
                  </div>
              </ion-item-sliding>
              <ion-item-sliding *ngIf="showLoader || allGroups.length < 1">
                <div *ngFor="let n of numSequence(8); let i = index;" class="item item-hover width-100 d-flex ion-no-padding ion-no-margin">
                  <div class="item-icon"><ion-icon class="icon" slot="start" src="assets/icon/icons-group-chat-grey.svg"></ion-icon></div>
                  <div class="item-content flex-grow-1 cursor-pointer">
                    <div class="item-title-time">
                      <div class="item-title"><ion-skeleton-text animated style="width: 50%"></ion-skeleton-text></div>
                      <div class="item-date"><ion-skeleton-text animated></ion-skeleton-text></div>
                    </div>
                    <div class="item-description d-flex align-items-center">
                      <fa-icon icon="file-alt" class="file-icon"></fa-icon>
                      <ion-skeleton-text animated style="width: 95%; margin-left:5%"></ion-skeleton-text>
                    </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)"
          (getDirectMessages)="getDirectMessages($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)"
          (getGroups)="getGroups($event)"
          [style.display]="showGroupMessages ? 'flex' : 'none'"
          class=" height-100 flex-column"
          [roomId]="roomId" #messagecontainer>
        </app-group-messages>

        <app-new-event
          [profile]=""
          [roomId]="groupRoomId"
          [selectedSegment]=segment
          [taskParticipants]="taskParticipants"
          [taskParticipantsCc]="taskParticipantsCc"
          [selectedDate]="eventSelectedDate"
          [eventAttendees]="contacts"
          (onAddEvent)="closeNewEventComponent()"
          (openAttendeesComponent)="openAttendeesComponent($event)"
          (cloneAllmobileComponent)="closeNewEventComponent()"
          [style.display]="showNewEvent ? 'flex' : 'none'"
          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>


