<!--
  ~ Licensed to the Apache Software Foundation (ASF) under one or more
  ~ contributor license agreements.  See the NOTICE file distributed with
  ~ this work for additional information regarding copyright ownership.
  ~ The ASF licenses this file to You 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.
  -->

<h2 mat-dialog-title>
    <div class="flex justify-between items-baseline">
        <div>
            {{ readonly ? 'Controller Service Details' : 'Edit Controller Service' }}
        </div>
        <div class="text-base">
            {{ formatType(request.controllerService) }}
        </div>
    </div>
</h2>
<form class="controller-service-edit-form" [formGroup]="editControllerServiceForm">
    <context-error-banner [context]="ErrorContextKey.CONTROLLER_SERVICES"></context-error-banner>
    <mat-tab-group [(selectedIndex)]="selectedIndex" (selectedIndexChange)="tabChanged($event)">
        <mat-tab label="Settings">
            <mat-dialog-content>
                <div class="dialog-tab-content flex gap-x-4">
                    <div class="w-full">
                        <div>
                            <mat-form-field>
                                <mat-label>Name</mat-label>
                                <input matInput formControlName="name" type="text" [readonly]="readonly" />
                            </mat-form-field>
                        </div>
                        <div class="flex flex-col mb-5">
                            <div>Id</div>
                            <div class="tertiary-color font-medium">
                                {{ request.controllerService.id }}
                            </div>
                        </div>
                        <div class="flex flex-col mb-5">
                            <div>Type</div>
                            <div class="tertiary-color font-medium">
                                {{ formatType(request.controllerService) }}
                            </div>
                        </div>
                        <div class="flex flex-col mb-5">
                            <div>Bundle</div>
                            <div class="tertiary-color font-medium">
                                {{ formatBundle(request.controllerService) }}
                            </div>
                        </div>
                        <div class="supports-controller-services flex flex-col mb-5">
                            <div>Supports Controller Service</div>
                            <div class="tertiary-color font-medium">
                                <ul>
                                    @for (
                                        serviceApi of request.controllerService.component.controllerServiceApis;
                                        track serviceApi
                                    ) {
                                        <li>
                                            <controller-service-api
                                                [type]="serviceApi.type"
                                                [bundle]="serviceApi.bundle"></controller-service-api>
                                        </li>
                                    }
                                </ul>
                            </div>
                        </div>
                        <div>
                            <mat-form-field>
                                <mat-label>
                                    Bulletin Level
                                    <i
                                        class="fa fa-info-circle"
                                        nifiTooltip
                                        [tooltipComponentType]="TextTip"
                                        tooltipInputData="The level at which this controller service will generate bulletins."></i>
                                </mat-label>
                                <mat-select formControlName="bulletinLevel">
                                    @for (option of bulletinLevels; track option) {
                                        <mat-option [value]="option.value" [disabled]="readonly">
                                            {{ option.text }}
                                        </mat-option>
                                    }
                                </mat-select>
                            </mat-form-field>
                        </div>
                    </div>
                    <div class="flex flex-col w-full">
                        <div>
                            Referencing Components
                            <i
                                class="fa fa-info-circle primary-color"
                                nifiTooltip
                                [tooltipComponentType]="TextTip"
                                tooltipInputData="Other components referencing this controller service."></i>
                        </div>
                        <div class="relative h-full border">
                            <div class="absolute inset-0 overflow-y-auto p-1">
                                <controller-service-references
                                    [serviceReferences]="request.controllerService.component.referencingComponents"
                                    [goToReferencingComponent]="
                                        goToReferencingComponent
                                    "></controller-service-references>
                            </div>
                        </div>
                    </div>
                </div>
            </mat-dialog-content>
        </mat-tab>
        <mat-tab label="Properties">
            <mat-dialog-content>
                <div class="dialog-tab-content flex gap-x-3">
                    <property-table
                        class="w-2/3"
                        formControlName="properties"
                        [createNewProperty]="createNewProperty"
                        [createNewService]="createNewService"
                        [parameterContext]="parameterContext"
                        [goToParameter]="goToParameter"
                        [convertToParameter]="convertToParameter"
                        [goToService]="goToService"
                        [propertyHistory]="request.history"
                        [supportsParameters]="supportsParameters"
                        [supportsSensitiveDynamicProperties]="
                            request.controllerService.component.supportsSensitiveDynamicProperties
                        "></property-table>
                    <property-verification
                        class="w-1/3"
                        [disabled]="readonly"
                        [isVerifying]="(propertyVerificationStatus$ | async) === 'loading'"
                        [results]="propertyVerificationResults$ | async"
                        (verify)="verifyClicked(request.controllerService)"></property-verification>
                </div>
            </mat-dialog-content>
        </mat-tab>
        <mat-tab label="Comments">
            <mat-dialog-content>
                <div class="dialog-tab-content">
                    <mat-form-field>
                        <mat-label>Comments</mat-label>
                        <textarea
                            matInput
                            formControlName="comments"
                            type="text"
                            rows="10"
                            [readonly]="readonly"></textarea>
                    </mat-form-field>
                </div>
            </mat-dialog-content>
        </mat-tab>
    </mat-tab-group>
    @if ({ value: (saving$ | async)! }; as saving) {
        <mat-dialog-actions align="end">
            @if (readonly) {
                <button mat-flat-button mat-dialog-close>Close</button>
            } @else {
                <button mat-button mat-dialog-close>Cancel</button>
                <button
                    [disabled]="!editControllerServiceForm.dirty || editControllerServiceForm.invalid || saving.value"
                    type="button"
                    (click)="submitForm()"
                    mat-flat-button>
                    <span *nifiSpinner="saving.value">Apply</span>
                </button>
            }
        </mat-dialog-actions>
    }
</form>
