<!--

    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-form-panel no-padding no-border" [formGroup]="mobileActionFormGroup">
  <div class="tb-form-row">
    <div class="fixed-title-width">{{ 'widget-action.mobile.action-type' | translate }}*</div>
    <mat-form-field class="flex-1" appearance="outline" subscriptSizing="dynamic">
      <mat-select required formControlName="type" placeholder="{{ 'widget-action.mobile.select-action-type' | translate }}">
        @for (actionType of mobileActionTypes; track actionType) {
          <mat-option [value]="actionType">
            {{ mobileActionTypeTranslations.get(mobileActionType[actionType]) | translate }}
          </mat-option>
        }
      </mat-select>
      @if (mobileActionFormGroup.get('type').hasError('required')
        && mobileActionFormGroup.get('type').touched) {
        <mat-icon matSuffix
                  matTooltipPosition="above"
                  matTooltipClass="tb-error-tooltip"
                  [matTooltip]="'widget-action.mobile.action-type-required' | translate"
                  class="tb-error">
          warning
        </mat-icon>
      }
    </mat-form-field>
  </div>
  <ng-container [formGroup]="mobileActionTypeFormGroup">
    @if (mobileActionFormGroup.get('type').value === mobileActionType.takePhoto ||
      mobileActionFormGroup.get('type').value === mobileActionType.takePictureFromGallery ||
      mobileActionFormGroup.get('type').value === mobileActionType.takeScreenshot) {
      <div class="tb-form-row">
        <mat-slide-toggle class="mat-slide" formControlName="saveToGallery">
          {{ 'widget-action.mobile.save-to-gallery' | translate }}
        </mat-slide-toggle>
      </div>
    }
    @if (mobileActionFormGroup.get('type').value === mobileActionType.deviceProvision) {
      <div class="tb-form-row">
        <div class="fixed-title-width">{{ 'widget-action.mobile.provision-type' | translate }}*</div>
        <mat-form-field class="flex-1" appearance="outline" subscriptSizing="dynamic">
          <mat-select formControlName="provisionType">
            @for (type of provisionTypes; track type) {
              <mat-option [value]="type">
                {{  provisionTypeTranslationMap.get(type) | translate }}
              </mat-option>
            }
          </mat-select>
        </mat-form-field>
      </div>
    }

    @for (config of actionConfig; track config.formControlName) {
      <div class="tb-form-panel stroked">
        <mat-expansion-panel class="tb-settings">
          <mat-expansion-panel-header>
            <mat-panel-description class="flex items-stretch justify-start">
              {{ config.title | translate }}
            </mat-panel-description>
          </mat-expansion-panel-header>
          <tb-js-func
            [formControlName]="config.formControlName"
            [functionName]="config.functionName"
            withModules
            [functionArgs]="config.functionArgs"
            [globalVariables]="functionScopeVariables"
            [editorCompleter]="customActionEditorCompleter"
            hideBrackets
            [helpId]="config.helpId"
          ></tb-js-func>
        </mat-expansion-panel>
      </div>
    }
  </ng-container>

  @if(mobileActionFormGroup.get('type').value) {
    @for (config of commonActionConfig; track config.formControlName) {
      <div class="tb-form-panel stroked">
        <mat-expansion-panel class="tb-settings">
          <mat-expansion-panel-header>
            <mat-panel-description class="flex items-stretch justify-start">
              {{ config.title | translate }}
            </mat-panel-description>
          </mat-expansion-panel-header>
          <tb-js-func
            [formControlName]="config.formControlName"
            [functionName]="config.functionName"
            withModules
            [functionArgs]="config.functionArgs"
            [globalVariables]="functionScopeVariables"
            [editorCompleter]="customActionEditorCompleter"
            hideBrackets
            [helpId]="config.helpId"
          ></tb-js-func>
        </mat-expansion-panel>
      </div>
    }
  }
</div>
