<div class="content">
  <div class="default-header"><h1>Controllers</h1></div>
  <div class="default-content">
    <app-controller-discovery></app-controller-discovery>

    <div class="mat-elevation-z8">
      <mat-table #table [dataSource]="dataSource">
        <ng-container matColumnDef="id">
          <mat-header-cell *matHeaderCellDef> ID </mat-header-cell>
          <mat-cell *matCellDef="let row"> {{ row.id }} </mat-cell>
        </ng-container>

        <ng-container matColumnDef="name">
          <mat-header-cell *matHeaderCellDef> Name </mat-header-cell>
          <mat-cell *matCellDef="let row">
            <a
              *ngIf="getControllerStatus(row) === 'running' || row.location === 'remote' || row.location === 'bundled'"
              [routerLink]="['/controller', row.id, 'login']"
              class="table-link"
              >{{ row.name }}</a
            >
            <span
              *ngIf="getControllerStatus(row) != 'running' && row.location !== 'remote' && row.location !== 'bundled'"
              >{{ row.name }}</span
            >
          </mat-cell>
        </ng-container>

        <ng-container matColumnDef="location">
          <mat-header-cell *matHeaderCellDef> Location </mat-header-cell>
          <mat-cell *matCellDef="let row"> {{ row.location }} </mat-cell>
        </ng-container>

        <ng-container matColumnDef="ip">
          <mat-header-cell *matHeaderCellDef> Host </mat-header-cell>
          <mat-cell *matCellDef="let row"> {{ row.host }} </mat-cell>
        </ng-container>

        <ng-container matColumnDef="port">
          <mat-header-cell *matHeaderCellDef> Port </mat-header-cell>
          <mat-cell *matCellDef="let row"> {{ row.port }} </mat-cell>
        </ng-container>

        <ng-container matColumnDef="actions">
          <mat-header-cell *matHeaderCellDef> Actions </mat-header-cell>
          <mat-cell *matCellDef="let row" style="text-align: right">
            <button
              mat-icon-button
              matTooltip="Go to projects"
              matTooltipClass="custom-tooltip"
              (click)="openProjects(row)"
              *ngIf="getControllerStatus(row) === 'running' || row.location === 'remote' || row.location === 'bundled'"
            >
              <mat-icon aria-label="Go to projects">arrow_forward</mat-icon>
            </button>

            <button
              mat-icon-button
              matTooltip="Start controller"
              matTooltipClass="custom-tooltip"
              (click)="startController(row)"
              *ngIf="row.location === 'local' && getControllerStatus(row) === 'stopped'"
            >
              <mat-icon aria-label="Start controller">play_arrow</mat-icon>
            </button>

            <button
              mat-icon-button
              matTooltip="Stop controller"
              matTooltipClass="custom-tooltip"
              (click)="stopController(row)"
              *ngIf="row.location === 'local' && getControllerStatus(row) === 'running'"
            >
              <mat-icon aria-label="Stop controller">stop</mat-icon>
            </button>

            <mat-spinner
              [diameter]="24"
              *ngIf="row.location === 'local' && getControllerStatus(row) === 'starting'"
            ></mat-spinner>

            <button
              mat-icon-button
              matTooltip="Remove controller"
              matTooltipClass="custom-tooltip"
              (click)="deleteController(row)"
            >
              <mat-icon aria-label="Remove controller">delete</mat-icon>
            </button>
          </mat-cell>
        </ng-container>

        <mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
        <mat-row *matRowDef="let row; columns: displayedColumns"></mat-row>
      </mat-table>
    </div>

    <div class="buttons-bar">
      <button *ngIf="isElectronApp" mat-raised-button class="button" (click)="startLocalController()">
        Start local controller
      </button>
      <button mat-raised-button class="button" color="primary" (click)="createModal()">Add Controller</button>
    </div>
  </div>
</div>
