<!--

    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.

-->
<div class="tb-details-buttons xs:flex xs:flex-col">
  <button mat-raised-button color="primary"
          [disabled]="(isLoading$ | async)"
          (click)="onEntityAction($event, 'open')"
          [class.!hidden]="isEdit || isDetailsPage">
    {{'common.open-details-page' | translate }}
  </button>
  <button mat-raised-button color="primary"
          [disabled]="(isLoading$ | async) || !(entity?.hasData && !entity?.url)"
          (click)="onEntityAction($event, 'uploadPackage')"
          [class.!hidden]="isEdit">
    {{ 'ota-update.download' | translate }}
  </button>
  <button mat-raised-button color="primary"
          [disabled]="(isLoading$ | async)"
          (click)="onEntityAction($event, 'delete')"
          [class.!hidden]="hideDelete() || isEdit">
    {{ 'ota-update.delete' | translate }}
  </button>
  <div class="flex flex-row xs:flex-col">
    <button mat-raised-button
            ngxClipboard
            (cbOnSuccess)="onPackageIdCopied()"
            [cbContent]="entity?.id?.id"
            [class.!hidden]="isEdit">
      <mat-icon svgIcon="mdi:clipboard-arrow-left"></mat-icon>
      <span translate>ota-update.copyId</span>
    </button>
    <button mat-raised-button
            ngxClipboard
            (cbOnSuccess)="onPackageChecksumCopied()"
            [cbContent]="entity?.checksum"
            [class.!hidden]="isEdit || !entity?.checksum">
      <mat-icon svgIcon="mdi:clipboard-arrow-left"></mat-icon>
      <span translate>ota-update.copy-checksum</span>
    </button>
    <button mat-raised-button
            ngxClipboard
            (cbOnSuccess)="onPackageDirectUrlCopied()"
            [cbContent]="entity?.url"
            [class.!hidden]="isEdit || !entity?.url">
      <mat-icon svgIcon="mdi:clipboard-arrow-left"></mat-icon>
      <span translate>ota-update.copy-direct-url</span>
    </button>
  </div>
</div>
<div class="mat-padding flex flex-col gap-2">
  <form [formGroup]="entityForm">
    <fieldset [disabled]="(isLoading$ | async) || !isEdit" class="flex flex-col min-w-full">
      <div class="flex flex-row xs:flex-col gt-xs:gap-2">
        <mat-form-field class="mat-block flex-full gt-xs:max-w-45%" appearance="outline">
          <mat-label translate>ota-update.title</mat-label>
          <input matInput formControlName="title" type="text" required [readonly]="!isAdd">
          @if (entityForm.get('title').hasError('required')) {
            <mat-error>
              {{ 'ota-update.title-required' | translate }}
            </mat-error>
          }
          @if (entityForm.get('title').hasError('maxlength')) {
            <mat-error>
              {{ 'ota-update.title-max-length' | translate }}
            </mat-error>
          }
        </mat-form-field>
        <mat-form-field class="mat-block flex-1" appearance="outline">
          <mat-label translate>ota-update.version</mat-label>
          <input matInput formControlName="version" type="text" required [readonly]="!isAdd">
          @if (entityForm.get('version').hasError('required')) {
            <mat-error>
              {{ 'ota-update.version-required' | translate }}
            </mat-error>
          }
          @if (entityForm.get('version').hasError('maxlength')) {
            <mat-error>
              {{ 'ota-update.version-max-length' | translate }}
            </mat-error>
          }
        </mat-form-field>
      </div>
      <mat-form-field class="mat-block flex-1" style="margin-bottom: 8px" appearance="outline">
        <mat-label translate>ota-update.version-tag</mat-label>
        <input matInput formControlName="tag" type="text" [readonly]="!isAdd">
        @if (isAdd) {
          <mat-hint translate>ota-update.version-tag-hint</mat-hint>
        }
      </mat-form-field>
      <tb-device-profile-autocomplete
        formControlName="deviceProfileId"
        appearance="outline"
        required
        [class.pb-4]="isAdd"
        [class.pb-0]="!isAdd"
        [hint]="'ota-update.chose-compatible-device-profile'"
        [editProfileEnabled]="false"
        [addNewProfile]="false"
        [showDetailsPageLink]="true">
      </tb-device-profile-autocomplete>
      <mat-form-field class="mat-block" appearance="outline">
        <mat-label translate>ota-update.package-type</mat-label>
        <mat-select formControlName="type" required>
          @for ( packageType of packageTypes; track packageType) {
            <mat-option [value]="packageType">
              {{ otaUpdateTypeTranslationMap.get(packageType) | translate }}
            </mat-option>
          }
        </mat-select>
      </mat-form-field>
      @if (isAdd) {
        <section>
          <div class="mat-caption" style="margin: -8px 0 8px;" translate>ota-update.warning-after-save-no-edit</div>
          <mat-radio-group formControlName="isURL" class="gap-4">
            <mat-radio-button [value]="false">{{ "ota-update.upload-binary-file" | translate }}</mat-radio-button>
            <mat-radio-button [value]="true">{{ "ota-update.use-external-url" | translate }}</mat-radio-button>
          </mat-radio-group>
        </section>
      }
      @if (!entityForm.get('isURL').value) {
        <section>
          @if (isAdd) {
            <section>
              <tb-file-input
                formControlName="file"
                workFromFileObj="true"
                label="{{ 'ota-update.package-file' | translate }}"
                [required]="!entityForm.get('isURL').value"
                dropLabel="{{'ota-update.drop-package-file-or' | translate}}">
              </tb-file-input>
              <mat-checkbox formControlName="generateChecksum" style="margin-top: 16px">
                {{ 'ota-update.auto-generate-checksum' | translate }}
              </mat-checkbox>
            </section>
          }
          @if (!(isAdd && entityForm.get('generateChecksum').value)) {
            <div class="flex flex-row xs:flex-col sm:gap-2 md:flex-col gt-xs:gap-2">
              <mat-form-field class="mat-block flex-full gt-xs:max-w-33%" appearance="outline">
                <mat-label translate>ota-update.checksum-algorithm</mat-label>
                <mat-select formControlName="checksumAlgorithm">
                  @for ( checksumAlgorithm of checksumAlgorithms; track checksumAlgorithm) {
                    <mat-option [value]="checksumAlgorithm">
                      {{ checksumAlgorithmTranslationMap.get(checksumAlgorithm) }}
                    </mat-option>
                  }
                </mat-select>
              </mat-form-field>
              <mat-form-field class="mat-block flex-1" appearance="outline">
                <mat-label translate>ota-update.checksum</mat-label>
                <input matInput formControlName="checksum" type="text">
                @if (isAdd) {
                  <mat-hint translate>ota-update.checksum-hint</mat-hint>
                }
              </mat-form-field>
            </div>
          }
          @if (!isAdd) {
            <section>
              <div class="flex flex-row xs:flex-col sm:gap-2 md:flex-col gt-md:gap-2">
                <mat-form-field class="mat-block flex-full gt-xs:max-w-33%" appearance="outline">
                  <mat-label translate>ota-update.file-name</mat-label>
                  <input matInput formControlName="fileName" type="text">
                </mat-form-field>
                <mat-form-field class="mat-block flex-1" appearance="outline">
                  <mat-label translate>ota-update.file-size-bytes</mat-label>
                  <input matInput formControlName="dataSize" type="text">
                </mat-form-field>
                <mat-form-field class="mat-block flex-1" appearance="outline">
                  <mat-label translate>ota-update.content-type</mat-label>
                  <input matInput formControlName="contentType" type="text">
                </mat-form-field>
              </div>
            </section>
          }
        </section>
      }
      @if (entityForm.get('isURL').value) {
        <section style="margin-top: 8px">
          <mat-form-field class="mat-block" appearance="outline">
            <mat-label translate>ota-update.direct-url</mat-label>
            <input matInput formControlName="url"
                   type="text"
                   [required]="entityForm.get('isURL').value">
            @if (entityForm.get('url').hasError('required') || entityForm.get('url').hasError('pattern')) {
              <mat-error translate>
                ota-update.direct-url-required
              </mat-error>
            }
          </mat-form-field>
        </section>
      }
      <div formGroupName="additionalInfo">
        <mat-form-field class="mat-block" appearance="outline">
          <mat-label translate>ota-update.description</mat-label>
          <textarea matInput formControlName="description" cdkTextareaAutosize cdkAutosizeMinRows="1" rows="1"></textarea>
        </mat-form-field>
      </div>
    </fieldset>
  </form>
</div>
