<!--

    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.

-->
<ng-container  [formGroup]="colorRangeListFormGroup">
  @if (advancedMode) {
    <div class="flex flex-row items-center justify-start">
      <div class="tb-form-panel-title flex-1" translate>widgets.color.settings-type</div>
      <tb-toggle-select formControlName="advancedMode" class="flex-1">
        <tb-toggle-option [value]="false">
          {{ 'widgets.color.basic-mode' | translate }}
        </tb-toggle-option>
        <tb-toggle-option [value]="true">
          {{ 'widgets.color.advanced-mode' | translate}}
        </tb-toggle-option>
      </tb-toggle-select>
    </div>
  } @else {
    <div class="flex flex-row items-center justify-start">
      @if (panelTitle) {
        <div class="tb-form-panel-title flex-1">{{ panelTitle }}</div>
      }
    </div>
  }


  <div class="tb-color-ranges">
    @switch (colorRangeListFormGroup.get('advancedMode').value) {
      @case (false) {
        <div class="tb-control-list tb-drop-list" cdkDropList cdkDropListOrientation="vertical"
             (cdkDropListDropped)="rangeDrop($event, 'range')">
          @for (rangeFormGroup of rangeListFormGroups; track trackByRange($index, rangeFormGroup); let isLast = $last) {
            <div cdkDrag class="tb-draggable tb-advanced-range-drag"
                 [formGroup]="rangeFormGroup"
                 [class.pb-2]="!isLast">
              <div class="flex flex-1 flex-row items-center justify-start">
                <div class="tb-form-row range-container">
                  <div class="tb-value-range-text" translate>widgets.color.from</div>
                  <mat-form-field appearance="outline" class="center number flex-1" subscriptSizing="dynamic">
                    <input matInput type="number" formControlName="from" placeholder="{{ 'widget-config.set' | translate }}">
                  </mat-form-field>
                  <div class="tb-value-range-text tb-value-range-text-to" translate>widgets.color.to</div>
                  <mat-form-field appearance="outline" class="center number flex-1" subscriptSizing="dynamic">
                    <input matInput type="number" formControlName="to" placeholder="{{ 'widget-config.set' | translate }}">
                  </mat-form-field>
                  <tb-color-input asBoxInput
                                  formControlName="color">
                  </tb-color-input>
                </div>
                <button type="button"
                        mat-icon-button
                        class="tb-box-button"
                        (click)="removeRange($index)"
                        matTooltip="{{ 'action.remove' | translate }}"
                        matTooltipPosition="above">
                  <mat-icon>delete</mat-icon>
                </button>
              </div>
              <button class="tb-box-button"
                      mat-icon-button
                      type="button"
                      cdkDragHandle
                      matTooltip="{{ 'action.drag' | translate }}"
                      matTooltipPosition="above">
                <mat-icon>drag_indicator</mat-icon>
              </button>
            </div>
          }
        </div>
      }
      @case (true) {
        <div class="tb-control-list tb-drop-list" cdkDropList cdkDropListOrientation="vertical"
             (cdkDropListDropped)="rangeDrop($event, 'rangeAdvanced')">
          @for (advancedRangeControl of advancedRangeControls; track trackByAdvancedRange($index, advancedRangeControl); let isLast = $last) {
            <div cdkDrag class="tb-draggable tb-advanced-range-drag"
                 [class.pb-2]="!isLast">
              <tb-advanced-range [formControl]="advancedRangeControl"
                                 [aliasController]="aliasController"
                                 [dataKeyCallbacks]="dataKeyCallbacks"
                                 [datasource]="datasource"
                                 (removeAdvancedRange)="removeAdvancedRange($index)">
              </tb-advanced-range>
              <button class="tb-box-button"
                      mat-icon-button
                      type="button"
                      cdkDragHandle
                      matTooltip="{{ 'action.drag' | translate }}"
                      matTooltipPosition="above">
                <mat-icon>drag_indicator</mat-icon>
              </button>
            </div>
          }
        </div>
      }
    }
  </div>
  <div class="tb-add-color-range">
    <button mat-stroked-button
            (click)="addRange()">
      <mat-icon>add</mat-icon>
    </button>
  </div>
</ng-container>
