<!--
  ~ 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>Save Flow Version</h2>
<form class="save-version-form" [formGroup]="saveVersionForm">
    <error-banner></error-banner>
    <mat-dialog-content>
        @if (versionControlInformation) {
            <div class="flex flex-col gap-y-4 mb-6">
                <div>
                    <div>Registry</div>
                    <div class="accent-color font-medium">{{ versionControlInformation.registryName }}</div>
                </div>
                @if (versionControlInformation.branch) {
                    <div>
                        <div>Branch</div>
                        <div class="accent-color font-medium">{{ versionControlInformation.branch }}</div>
                    </div>
                }
                <div>
                    <div>Bucket</div>
                    <div class="accent-color font-medium">{{ versionControlInformation.bucketName }}</div>
                </div>
                <div>
                    <div>
                        <div>Flow Name</div>
                        <div class="accent-color font-medium">{{ versionControlInformation.flowName }}</div>
                    </div>
                </div>
                <div>
                    <div>Flow Description</div>
                    @if (!versionControlInformation.flowDescription) {
                        <div class="unset surface-color">No value set</div>
                    } @else {
                        <div class="accent-color font-medium">{{ versionControlInformation.flowDescription }}</div>
                    }
                </div>
            </div>
        } @else {
            <mat-form-field>
                <mat-label>Registry</mat-label>
                <mat-select formControlName="registry" (selectionChange)="registryChanged($event.value)">
                    @for (option of registryClientOptions; track option) {
                        @if (option.description) {
                            <mat-option
                                [value]="option.value"
                                nifiTooltip
                                [tooltipComponentType]="TextTip"
                                [tooltipInputData]="option.description"
                                [delayClose]="false"
                                >{{ option.text }}
                            </mat-option>
                        } @else {
                            <mat-option [value]="option.value">{{ option.text }}</mat-option>
                        }
                    }
                </mat-select>
            </mat-form-field>

            @if (supportsBranching) {
                <mat-form-field>
                    <mat-label>Branch</mat-label>
                    <mat-select formControlName="branch" (selectionChange)="branchChanged($event.value)">
                        <ng-container *ngFor="let option of branchOptions">
                            <mat-option [value]="option.value">{{ option.text }}</mat-option>
                        </ng-container>
                    </mat-select>
                    @if (saveVersionForm.controls['branch'].hasError('required')) {
                        <mat-error>No branches available</mat-error>
                    }
                </mat-form-field>
            }

            <mat-form-field>
                <mat-label>Bucket</mat-label>
                <mat-select formControlName="bucket">
                    <ng-container *ngFor="let option of bucketOptions">
                        <ng-container *ngIf="option.description; else noDescription">
                            <mat-option
                                [value]="option.value"
                                nifiTooltip
                                [tooltipComponentType]="TextTip"
                                [tooltipInputData]="option.description"
                                [delayClose]="false"
                                >{{ option.text }}
                            </mat-option>
                        </ng-container>
                        <ng-template #noDescription>
                            <mat-option [value]="option.value">{{ option.text }}</mat-option>
                        </ng-template>
                    </ng-container>
                </mat-select>
                @if (saveVersionForm.controls['bucket'].hasError('required')) {
                    <mat-error>No buckets available</mat-error>
                }
            </mat-form-field>

            <div class="w-full">
                <mat-form-field class="flex-1">
                    <mat-label>Flow Name</mat-label>
                    <input matInput formControlName="flowName" type="text" />
                </mat-form-field>
            </div>

            <mat-form-field>
                <mat-label>Flow Description</mat-label>
                <textarea matInput formControlName="flowDescription" type="text"></textarea>
            </mat-form-field>
        }

        <mat-form-field>
            <mat-label>Version Comments</mat-label>
            <textarea matInput formControlName="comments" type="text"></textarea>
        </mat-form-field>
    </mat-dialog-content>

    <mat-dialog-actions align="end">
        <button mat-button mat-dialog-close>Cancel</button>
        <button
            [disabled]="saveVersionForm.invalid || saving()"
            type="button"
            color="primary"
            (click)="submitForm()"
            mat-button>
            <span *nifiSpinner="saving()">Save</span>
        </button>
    </mat-dialog-actions>
</form>
