<!--

    Copyright © 2016-2026 The Thingsboard Authors

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<mat-toolbar color="primary">
  <h2 translate>{{ dialogTitle }}</h2>
  <span class="flex-1"></span>
  <!--  <div [tb-help]="'check-connectivity'"></div>-->
  <button mat-icon-button
          (click)="close()"
          type="button">
    <mat-icon class="material-icons">close</mat-icon>
  </button>
</mat-toolbar>
<div mat-dialog-content>
  @if (loadedCommand) {
    <section class="tb-form-panel no-padding no-border">
      <tb-toggle-header #transportProtocol
                        value="{{ selectTransportType }}"
                        name="TransportProtocol"
                        useSelectOnMdLg="false"
                        appearance="fill"
                        [class.!hidden]="allowTransportType.size < 2">
        @if (allowTransportType.has(BasicTransportType.HTTP)) {
          <tb-toggle-option
            [value]="BasicTransportType.HTTP">
            {{ deviceTransportTypeTranslationMap.get(BasicTransportType.HTTP) | translate }}
          </tb-toggle-option>
        }
        @if (allowTransportType.has(DeviceTransportType.MQTT)) {
          <tb-toggle-option
            [value]="DeviceTransportType.MQTT">
            {{ deviceTransportTypeTranslationMap.get(DeviceTransportType.MQTT) | translate }}
          </tb-toggle-option>
        }
        @if (allowTransportType.has(DeviceTransportType.COAP)) {
          <tb-toggle-option
            [value]="DeviceTransportType.COAP">
            {{ deviceTransportTypeTranslationMap.get(DeviceTransportType.COAP) | translate }}
          </tb-toggle-option>
        }
        @if (allowTransportType.has(DeviceTransportType.SNMP)) {
          <tb-toggle-option
            [value]="DeviceTransportType.SNMP">
            {{ deviceTransportTypeTranslationMap.get(DeviceTransportType.SNMP) | translate }}
          </tb-toggle-option>
        }
        @if (allowTransportType.has(DeviceTransportType.LWM2M)) {
          <tb-toggle-option
            [value]="DeviceTransportType.LWM2M">
            {{ deviceTransportTypeTranslationMap.get(DeviceTransportType.LWM2M) | translate }}
          </tb-toggle-option>
        }
      </tb-toggle-header>
      <div class="tb-form-panel no-padding no-border" [class.!hidden]="!allowTransportType.size">
        @switch (transportProtocol.value) {
          @case (BasicTransportType.HTTP) {
            <div class="tb-no-data-text" translate>device.connectivity.use-following-instructions</div>
            <mat-tab-group [(selectedIndex)]="httpTabIndex">
              <mat-tab>
                <ng-template mat-tab-label>
                  <mat-icon class="tabs-icon" svgIcon="windows"></mat-icon>
                  Windows
                </ng-template>
                <ng-template matTabContent>
                  <div class="tb-form-panel no-padding no-border tb-tab-body">
                    <div class="tb-form-panel stroked">
                      <div class="tb-form-panel-title" translate>device.connectivity.install-necessary-client-tools</div>
                      <div class="tb-install-instruction-text" translate>device.connectivity.install-curl-windows</div>
                    </div>
                    <ng-container
                    *ngTemplateOutlet="executeCommand; context: {cmd: {
                              noSecLabel: 'HTTP', noSec: commands.http.http,
                              secLabel: 'HTTPs', sec: commands.http.https,
                              doc: {text: '', href: ''}}}">
                    </ng-container>
                  </div>
                </ng-template>
              </mat-tab>
              <mat-tab>
                <ng-template mat-tab-label>
                  <mat-icon class="tabs-icon" svgIcon="macos"></mat-icon>
                  MacOS
                </ng-template>
                <ng-template matTabContent>
                  <div class="tb-form-panel no-padding no-border tb-tab-body">
                    <div class="tb-form-panel stroked">
                      <div class="tb-form-panel-title" translate>device.connectivity.install-necessary-client-tools</div>
                      <div class="tb-install-instruction-text" translate>device.connectivity.install-curl-macos</div>
                    </div>
                    <ng-container
                    *ngTemplateOutlet="executeCommand; context: {cmd: {
                              noSecLabel: 'HTTP', noSec: commands.http.http,
                              secLabel: 'HTTPs', sec: commands.http.https,
                              doc: {text: '', href: ''}}}">
                    </ng-container>
                  </div>
                </ng-template>
              </mat-tab>
              <mat-tab>
                <ng-template mat-tab-label>
                  <mat-icon class="tabs-icon" svgIcon="linux"></mat-icon>
                  Linux
                </ng-template>
                <ng-template matTabContent>
                  <div class="tb-form-panel no-padding no-border tb-tab-body">
                    <div class="tb-form-panel stroked">
                      <div class="tb-form-panel-title" translate>device.connectivity.install-necessary-client-tools</div>
                      <tb-markdown usePlainMarkdown containerClass="tb-command-code"
                      [data]='createMarkDownCommand("sudo apt-get install curl")'></tb-markdown>
                    </div>
                    <ng-container
                    *ngTemplateOutlet="executeCommand; context: {cmd: {
                              noSecLabel: 'HTTP', noSec: commands.http.http,
                              secLabel: 'HTTPs', sec: commands.http.https,
                              doc: {text: '', href: ''}}}">
                    </ng-container>
                  </div>
                </ng-template>
              </mat-tab>
            </mat-tab-group>
          }
          @case (DeviceTransportType.MQTT) {
            @if (commands.mqtt.sparkplug) {
              <div class="tb-form-panel stroked">
                <ng-container
              *ngTemplateOutlet="seeDocumentation; context:
              {doc: {text: 'device.connectivity.sparkplug-command', href: 'https://thingsboard.io/docs/reference/mqtt-sparkplug-api/'}}">
                </ng-container>
              </div>
            } @else {
              <div class="tb-no-data-text" translate>device.connectivity.use-following-instructions</div>
              <mat-tab-group [(selectedIndex)]="mqttTabIndex">
                <mat-tab>
                  <ng-template mat-tab-label>
                    <mat-icon class="tabs-icon" svgIcon="windows"></mat-icon>
                    Windows
                  </ng-template>
                  <ng-template matTabContent>
                    <div class="tb-form-panel no-padding no-border tb-tab-body">
                      <div class="tb-form-panel stroked">
                        <div class="tb-form-panel-title" translate>device.connectivity.install-necessary-client-tools</div>
                        <div class="tb-install-instruction-text">
                        <ng-container *ngTemplateOutlet="seeDocumentation; context:
                        {doc: {text: 'device.connectivity.install-mqtt-windows',
                        href: 'https://thingsboard.io/docs/reference/mqtt-api/?connectdevice=mqtt-windows#mqtt-connect'}}">
                          </ng-container>
                        </div>
                      </div>
                      <ng-container
                      *ngTemplateOutlet="executeCommand; context: {cmd: {
                              noSecLabel: 'MQTT', noSec: commands.mqtt.mqtt,
                              secLabel: 'MQTTs', sec: commands.mqtt.mqtts,
                              doc: {text: 'device.connectivity.mqtts-x509-command', href: 'https://thingsboard.io/docs/user-guide/mqtt-over-ssl/'}}}">
                      </ng-container>
                    </div>
                  </ng-template>
                </mat-tab>
                <mat-tab>
                  <ng-template mat-tab-label>
                    <mat-icon class="tabs-icon" svgIcon="macos"></mat-icon>
                    MacOS
                  </ng-template>
                  <ng-template matTabContent>
                    <div class="tb-form-panel no-padding no-border tb-tab-body">
                      <div class="tb-form-panel stroked">
                        <div class="tb-form-panel-title" translate>device.connectivity.install-necessary-client-tools</div>
                        <tb-markdown usePlainMarkdown containerClass="tb-command-code"
                        [data]='createMarkDownCommand("brew install mosquitto")'></tb-markdown>
                      </div>
                      <ng-container
                      *ngTemplateOutlet="executeCommand; context: {cmd: {
                              noSecLabel: 'MQTT', noSec: commands.mqtt.mqtt,
                              secLabel: 'MQTTs', sec: commands.mqtt.mqtts,
                              doc: {text: 'device.connectivity.mqtts-x509-command', href: 'https://thingsboard.io/docs/user-guide/mqtt-over-ssl/'}}}">
                      </ng-container>
                    </div>
                  </ng-template>
                </mat-tab>
                <mat-tab>
                  <ng-template mat-tab-label>
                    <mat-icon class="tabs-icon" svgIcon="linux"></mat-icon>
                    Linux
                  </ng-template>
                  <ng-template matTabContent>
                    <div class="tb-form-panel no-padding no-border tb-tab-body">
                      <div class="tb-form-panel stroked">
                        <div class="tb-form-panel-title" translate>device.connectivity.install-necessary-client-tools</div>
                        <tb-markdown usePlainMarkdown containerClass="tb-command-code"
                        [data]='createMarkDownCommand("sudo apt-get install curl mosquitto-clients")'></tb-markdown>
                      </div>
                      <ng-container
                      *ngTemplateOutlet="executeCommand; context: {cmd: {
                              noSecLabel: 'MQTT', noSec: commands.mqtt.mqtt,
                              secLabel: 'MQTTs', sec: commands.mqtt.mqtts,
                              doc: {text: 'device.connectivity.mqtts-x509-command', href: 'https://thingsboard.io/docs/user-guide/mqtt-over-ssl/'}}}">
                      </ng-container>
                    </div>
                  </ng-template>
                </mat-tab>
                @if (!!commands.mqtt.docker) {
                  <mat-tab>
                    <ng-template mat-tab-label>
                      <mat-icon class="tabs-icon" svgIcon="docker"></mat-icon>
                      Docker
                    </ng-template>
                    <ng-template matTabContent>
                      <div class="tb-form-panel no-padding no-border tb-tab-body">
                        <ng-container
                      *ngTemplateOutlet="executeCommand; context: {cmd: {
                              noSecLabel: 'MQTT', noSec: commands.mqtt.docker.mqtt,
                              secLabel: 'MQTTs', sec: commands.mqtt.docker.mqtts,
                              doc: {text: 'device.connectivity.mqtts-x509-command', href: 'https://thingsboard.io/docs/user-guide/mqtt-over-ssl/'}}}">
                        </ng-container>
                      </div>
                    </ng-template>
                  </mat-tab>
                }
              </mat-tab-group>
            }
          }
          @case (DeviceTransportType.COAP) {
            <div class="tb-no-data-text" translate>device.connectivity.use-following-instructions</div>
            <mat-tab-group [(selectedIndex)]="coapTabIndex">
              <mat-tab>
                <ng-template mat-tab-label>
                  <mat-icon class="tabs-icon" svgIcon="linux"></mat-icon>
                  Linux
                </ng-template>
                <ng-template matTabContent>
                  <div class="tb-form-panel no-padding no-border tb-tab-body">
                    @if (commands.coap.coaps !== 'Check documentation') {
                      <div class="tb-form-panel stroked">
                        <div class="tb-form-panel-title" translate>device.connectivity.install-necessary-client-tools</div>
                        <div class="tb-install-instruction-text">
                      <ng-container *ngTemplateOutlet="seeDocumentation; context:
                                    {doc: {text: 'device.connectivity.install-coap-client',
                                    href: 'https://thingsboard.io/docs/user-guide/ssl/coap-access-token/'}}">
                          </ng-container>
                        </div>
                      </div>
                    }
                    <ng-container
                    *ngTemplateOutlet="executeCommand; context: {cmd: {
                              noSecLabel: 'CoAP', noSec: commands.coap.coap,
                              secLabel: 'CoAPs', sec: commands.coap.coaps,
                              doc: {text: 'device.connectivity.coaps-x509-command', href: 'https://thingsboard.io/docs/user-guide/ssl/coap-x509-certificates/'}}}">
                    </ng-container>
                  </div>
                </ng-template>
              </mat-tab>
              @if (!!commands.coap.docker) {
                <mat-tab>
                  <ng-template mat-tab-label>
                    <mat-icon class="tabs-icon" svgIcon="docker"></mat-icon>
                    Docker
                  </ng-template>
                  <ng-template matTabContent>
                    <div class="tb-form-panel no-padding no-border tb-tab-body">
                      <ng-container
                    *ngTemplateOutlet="executeCommand; context: {cmd: {
                              noSecLabel: 'CoAP', noSec: commands.coap.docker.coap,
                              secLabel: 'CoAPs', sec: commands.coap.docker.coaps,
                              doc: {text: 'device.connectivity.coaps-x509-command', href: 'https://thingsboard.io/docs/user-guide/ssl/coap-x509-certificates/'}}}">
                      </ng-container>
                    </div>
                  </ng-template>
                </mat-tab>
              }
            </mat-tab-group>
          }
          @case (DeviceTransportType.SNMP) {
            <div class="tb-form-panel stroked">
              <ng-container
              *ngTemplateOutlet="seeDocumentation; context:
                {doc: {text: 'device.connectivity.snmp-command', href: 'https://thingsboard.io/docs/reference/snmp-api/'}}">
              </ng-container>
            </div>
          }
          @case (DeviceTransportType.LWM2M) {
            <div class="tb-form-panel stroked">
              <ng-container
              *ngTemplateOutlet="seeDocumentation; context:
                {doc: {text: 'device.connectivity.lwm2m-command', href: 'https://thingsboard.io/docs/reference/lwm2m-api/'}}">
              </ng-container>
            </div>
          }
        }
      </div>
      <div class="tb-form-panel stroked">
        <div class="flex flex-1 flex-row">
          <div class="tb-form-panel-title" translate>device.state</div>
          <div class="status" [class.inactive]="!status" [class.hide]="status === undefined">
            {{ (status ? 'device.active' : 'device.inactive') | translate }}
          </div>
        </div>
        <div class="tb-form-panel-hint tb-font-14" translate>attribute.latest-telemetry</div>
        <div class="tb-form-table">
          <div class="tb-form-table-header">
            <div class="tb-form-table-header-cell flex-1" translate>device.time</div>
            <div class="tb-form-table-header-cell flex-1" translate>attribute.key</div>
            <div class="tb-form-table-header-cell flex-1" translate>attribute.value</div>
          </div>
          @if (latestTelemetry.length) {
            <div class="tb-form-table-body">
              @for (telemetry of latestTelemetry; track telemetry) {
                <div class="tb-form-table-row">
                  <div class="tb-form-table-row-cell flex-1">{{ telemetry.lastUpdateTs | date: 'yyyy-MM-dd HH:mm:ss' }}</div>
                  <div class="tb-form-table-row-cell flex-1">{{ telemetry.key }}</div>
                  <div class="tb-form-table-row-cell flex-1">{{ telemetry.value }}</div>
                </div>
              }
            </div>
          } @else {
            <div class="tb-no-data-available">
              <div class="tb-no-data-bg"></div>
              <div class="tb-no-data-text" translate>attribute.no-latest-telemetry</div>
            </div>
          }
        </div>
      </div>
    </section>
  } @else {
    <div class="tb-loader">
      <mat-spinner color="accent" diameter="65" strokeWidth="4"></mat-spinner>
      <span class="mat-subtitle-1 label">
        {{ 'device.connectivity.loading-check-connectivity-command' | translate }}
      </span>
    </div>
  }
</div>
<div mat-dialog-actions class="tb-dialog-actions">
  <mat-slide-toggle [class.!hidden]="!showDontShowAgain" [(ngModel)]="notShowAgain">{{ 'action.dont-show-again' | translate}}</mat-slide-toggle>
  <span class="flex-1"></span>
  <button mat-button
          [disabled]="(isLoading$ | async)"
  (click)="close()">{{ 'action.close' | translate }}</button>
</div>

<ng-template #executeCommand let-cmd="cmd">
  <div class="tb-form-panel stroked">
    <div class="flex flex-row items-center justify-between" [class.!hidden]="!(cmd.noSec || cmd.sec !== 'Check documentation')">
      <div class="tb-form-panel-title" translate>device.connectivity.execute-following-command</div>
      <tb-toggle-header #protocolType value="{{ cmd.noSec ? 'noSec' : 'sec'}}" name="protocolType" useSelectOnMdLg="false"
                        [class.!hidden]="!cmd.noSec || !cmd.sec">
        <tb-toggle-option value="noSec">{{ cmd.noSecLabel }}</tb-toggle-option>
        <tb-toggle-option value="sec">{{ cmd.secLabel }}</tb-toggle-option>
      </tb-toggle-header>
    </div>
    @switch (protocolType.value) {
      @case ('noSec') {
        <tb-markdown usePlainMarkdown containerClass="tb-command-code"
        [data]=createMarkDownCommand(cmd.noSec)></tb-markdown>
      }
      @case ('sec') {
        @if (cmd.sec !== 'Check documentation') {
          <div>
            <tb-markdown usePlainMarkdown containerClass="tb-command-code"
            [data]=createMarkDownCommand(cmd.sec)></tb-markdown>
          </div>
        } @else {
          <ng-container
            *ngTemplateOutlet="seeDocumentation; context:
                {doc: cmd.doc}">
          </ng-container>
        }
      }
    }
  </div>
</ng-template>

<ng-template #seeDocumentation let-doc="doc">
  <div class="tb-form-row no-border no-padding space-between">
    <div class="tb-font-14 tb-flex-1">{{ doc.text | translate }}</div>
    <a mat-stroked-button color="primary" href="{{doc.href }}" target="_blank">
      <mat-icon>description</mat-icon>
      {{ 'common.documentation' | translate }}
    </a>
  </div>
</ng-template>
