<!--

    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.

-->
<section [formGroup]="geoFilterConfigForm" class="tb-form-panel no-border no-padding">
  <section class="tb-form-panel stroked">
    <div class="tb-form-panel-title" translate>rule-node-config.coordinate-field-names</div>
    <section>
      <div class="flex flex-row gap-4">
        <mat-form-field class="mat-block max-w-50% flex-full" appearance="outline">
          <mat-label>{{ 'rule-node-config.latitude-field-name' | translate }}</mat-label>
          <input matInput formControlName="latitudeKeyName" required>
          @if (geoFilterConfigForm.get('latitudeKeyName').hasError('required')) {
            <mat-error>
              {{ 'rule-node-config.latitude-field-name-required' | translate }}
            </mat-error>
          }
        </mat-form-field>
        <mat-form-field class="mat-block max-w-50% flex-full" appearance="outline">
          <mat-label>{{ 'rule-node-config.longitude-field-name' | translate }}</mat-label>
          <input matInput formControlName="longitudeKeyName" required>
          @if (geoFilterConfigForm.get('longitudeKeyName').hasError('required')) {
            <mat-error>
              {{ 'rule-node-config.longitude-field-name-required' | translate }}
            </mat-error>
          }
        </mat-form-field>
      </div>
      <div class="tb-form-hint tb-primary-fill" translate>rule-node-config.coordinate-field-hint</div>
    </section>
  </section>
  <section class="tb-form-panel stroked" [class.no-padding-bottom]="!defaultPaddingEnable">
    <div class="tb-form-panel-title" translate>rule-node-config.geofence-configuration</div>
    <section class="flex flex-col">
      <mat-form-field class="mat-block flex-1" hideRequiredMarker appearance="outline">
        <mat-label>{{ 'rule-node-config.perimeter-type' | translate }}</mat-label>
        <mat-select formControlName="perimeterType">
          @for (type of perimeterTypes; track type) {
            <mat-option [value]="type">
              {{ perimeterTypeTranslationMap.get(type) | translate }}
            </mat-option>
          }
        </mat-select>
      </mat-form-field>
      <div tb-hint-tooltip-icon="{{ geoFilterConfigForm.get('perimeterType').value === perimeterType.CIRCLE
           ? ('rule-node-config.fetch-circle-parameter-info-from-metadata-hint'
            | translate : { perimeterKeyName: geoFilterConfigForm.get('perimeterKeyName').valid
                              ? geoFilterConfigForm.get('perimeterKeyName').value
                              : 'ss_perimeter'})
        : ('rule-node-config.fetch-poligon-parameter-info-from-metadata-hint'
            | translate : { perimeterKeyName: geoFilterConfigForm.get('perimeterKeyName').valid
                              ? geoFilterConfigForm.get('perimeterKeyName').value
                              : 'ss_perimeter'}) }}"
        class="tb-form-row no-border no-padding slide-toggle">
        <mat-slide-toggle class="mat-slide" formControlName="fetchPerimeterInfoFromMessageMetadata">
          {{ 'rule-node-config.fetch-perimeter-info-from-metadata' | translate }}
        </mat-slide-toggle>
      </div>
      @if (geoFilterConfigForm.get('fetchPerimeterInfoFromMessageMetadata').value) {
        <mat-form-field class="mat-block" appearance="outline">
          <mat-label>{{ 'rule-node-config.perimeter-key-name' | translate }}</mat-label>
          <input matInput formControlName="perimeterKeyName" required>
          @if (geoFilterConfigForm.get('perimeterKeyName').hasError('required')) {
            <mat-error>
              {{ 'rule-node-config.perimeter-key-name-required' | translate }}
            </mat-error>
          }
          <mat-hint>{{ 'rule-node-config.perimeter-key-name-hint' | translate }}</mat-hint>
        </mat-form-field>
      }
      @if (geoFilterConfigForm.get('perimeterType').value === perimeterType.CIRCLE &&
        !geoFilterConfigForm.get('fetchPerimeterInfoFromMessageMetadata').value) {
        <div class="flex flex-col">
          <div class="flex flex-row gap-4">
            <mat-form-field class="flex-1" appearance="outline">
              <mat-label>{{ 'rule-node-config.circle-center-latitude' | translate }}</mat-label>
              <input type="number" min="-90" max="90" step="0.1" matInput formControlName="centerLatitude" required>
              @if (geoFilterConfigForm.get('centerLatitude').hasError('required')) {
                <mat-error>
                  {{ 'rule-node-config.circle-center-latitude-required' | translate }}
                </mat-error>
              }
            </mat-form-field>
            <mat-form-field class="flex-1" appearance="outline">
              <mat-label>{{ 'rule-node-config.circle-center-longitude' | translate }}</mat-label>
              <input type="number" min="-180" max="180" step="0.1" matInput formControlName="centerLongitude" required>
              @if (geoFilterConfigForm.get('centerLongitude').hasError('required')) {
                <mat-error>
                  {{ 'rule-node-config.circle-center-longitude-required' | translate }}
                </mat-error>
              }
            </mat-form-field>
          </div>
          <div class="flex flex-row gap-4">
            <mat-form-field class="flex-1" appearance="outline">
              <mat-label>{{ 'rule-node-config.range' | translate }}</mat-label>
              <input type="number" min="0" step="0.1" matInput formControlName="range" required>
              @if (geoFilterConfigForm.get('range').hasError('required')) {
                <mat-error>
                  {{ 'rule-node-config.range-required' | translate }}
                </mat-error>
              }
            </mat-form-field>
            <mat-form-field class="flex-1" appearance="outline">
              <mat-label>{{ 'rule-node-config.range-units' | translate }}</mat-label>
              <mat-select formControlName="rangeUnit" required>
                @for (type of rangeUnits; track type) {
                  <mat-option [value]="type">
                    {{ rangeUnitTranslationMap.get(type) | translate }}
                  </mat-option>
                }
              </mat-select>
              @if (geoFilterConfigForm.get('rangeUnit').hasError('required')) {
                <mat-error>
                  {{ 'rule-node-config.range-units-required' | translate }}
                </mat-error>
              }
            </mat-form-field>
          </div>
        </div>
      }
      @if (geoFilterConfigForm.get('perimeterType').value === perimeterType.POLYGON &&
        !geoFilterConfigForm.get('fetchPerimeterInfoFromMessageMetadata').value) {
        <mat-form-field class="mat-block" subscriptSizing="dynamic" appearance="outline">
          <mat-label>{{ 'rule-node-config.polygon-definition' | translate }}</mat-label>
          <input matInput formControlName="polygonsDefinition" required>
          <mat-hint>{{ 'rule-node-config.polygon-definition-hint' | translate }}</mat-hint>
          @if (geoFilterConfigForm.get('polygonsDefinition').hasError('required')) {
            <mat-error>
              {{ 'rule-node-config.polygon-definition-required' | translate }}
            </mat-error>
          }
        </mat-form-field>
      }
    </section>
  </section>
</section>
