<cd-modal [modalRef]="activeModal">
  <ng-container i18n="form title"
                class="modal-title">{{ action | titlecase }} {{ resource | upperFirst }}</ng-container>

  <ng-container class="modal-content">
    <form #frm="ngForm"
          [formGroup]="form"
          novalidate>
      <div class="modal-body">
        <div class="form-group row">
          <label class="cd-col-form-label"
                 for="name"
                 i18n>Name</label>
          <div class="cd-col-form-input">
            <input type="text"
                   id="name"
                   name="name"
                   class="form-control"
                   placeholder="Name..."
                   formControlName="name"
                   autofocus>
            <span class="invalid-feedback"
                  *ngIf="form.showError('name', frm, 'required')"
                  i18n>This field is required!</span>
            <span class="invalid-feedback"
                  *ngIf="form.showError('name', frm, 'pattern')"
                  i18n>The name can only consist of alphanumeric characters, dashes and underscores.</span>
            <span class="invalid-feedback"
                  *ngIf="form.showError('name', frm, 'uniqueName')"
                  i18n>The chosen erasure code profile name is already in use.</span>
          </div>
        </div>

        <div class="form-group row">
          <label for="plugin"
                 class="cd-col-form-label">
            <span class="required"
                  i18n>Plugin</span>
            <cd-helper [html]="tooltips.plugins[plugin].description">
            </cd-helper>
          </label>
          <div class="cd-col-form-input">
            <select class="form-select"
                    id="plugin"
                    name="plugin"
                    formControlName="plugin">
              <option *ngIf="!plugins"
                      ngValue=""
                      i18n>Loading...</option>
              <option *ngFor="let plugin of plugins"
                      [ngValue]="plugin">
                {{ plugin }}
              </option>
            </select>
            <span class="invalid-feedback"
                  *ngIf="form.showError('name', frm, 'required')"
                  i18n>This field is required!</span>
          </div>
        </div>

        <div class="form-group row">
          <label for="k"
                 class="cd-col-form-label">
            <span class="required"
                  i18n>Data chunks (k)</span>
            <cd-helper [html]="tooltips.k">
            </cd-helper>
          </label>
          <div class="cd-col-form-input">
            <input type="number"
                   id="k"
                   name="k"
                   class="form-control"
                   ng-model="$ctrl.erasureCodeProfile.k"
                   placeholder="Data chunks..."
                   formControlName="k"
                   min="2">
            <span class="invalid-feedback"
                  *ngIf="form.showError('k', frm, 'required')"
                  i18n>This field is required!</span>
            <span class="invalid-feedback"
                  *ngIf="form.showError('k', frm, 'min')"
                  i18n>Must be equal to or greater than 2.</span>
            <span class="invalid-feedback"
                  *ngIf="form.showError('k', frm, 'max') && form.getValue('crushFailureDomain') === CrushFailureDomains.Osd"
                  i18n>Chunks (k+m) have exceeded the available OSDs of {{deviceCount}}.</span>
            <span class="invalid-feedback"
                  *ngIf="form.showError('k', frm, 'max') && form.getValue('crushFailureDomain') === CrushFailureDomains.Host"
                  i18n>Chunks (k+m+1) have exceeded the available hosts of {{deviceCount}}.</span>
            <span class="invalid-feedback"
                  *ngIf="form.showError('k', frm, 'unequal')"
                  i18n>For an equal distribution k has to be a multiple of (k+m)/l.</span>
            <span class="invalid-feedback"
                  *ngIf="form.showError('k', frm, 'kLowerM')"
                  i18n>K has to be equal to or greater than m in order to recover data correctly through c.</span>
            <span *ngIf="plugin === 'lrc'"
                  class="form-text text-muted"
                  i18n>Distribution factor: {{lrcMultiK}}</span>
          </div>
        </div>

        <div class="form-group row">
          <label for="m"
                 class="cd-col-form-label">
            <span class="required"
                  i18n>Coding chunks (m)</span>
            <cd-helper [html]="tooltips.m">
            </cd-helper>
          </label>
          <div class="cd-col-form-input">
            <input type="number"
                   id="m"
                   name="m"
                   class="form-control"
                   placeholder="Coding chunks..."
                   formControlName="m"
                   min="1">
            <span class="invalid-feedback"
                  *ngIf="form.showError('m', frm, 'required')"
                  i18n>This field is required!</span>
            <span class="invalid-feedback"
                  *ngIf="form.showError('m', frm, 'min')"
                  i18n>Must be equal to or greater than 1.</span>
            <span class="invalid-feedback"
                  *ngIf="form.showError('m', frm, 'max') && form.getValue('crushFailureDomain') ===  CrushFailureDomains.Osd"
                  i18n>Chunks (k+m) have exceeded the available OSDs of {{deviceCount}}.</span>
            <span class="invalid-feedback"
                  *ngIf="form.showError('m', frm, 'max') && form.getValue('crushFailureDomain') ===  CrushFailureDomains.Host"
                  i18n>Chunks (k+m+1) have exceeded the available hosts of {{deviceCount}}.</span>
          </div>
        </div>

        <div class="form-group row"
             *ngIf="plugin === 'shec'">
          <label for="c"
                 class="cd-col-form-label">
            <span class="required"
                  i18n>Durability estimator (c)</span>
            <cd-helper [html]="tooltips.plugins.shec.c">
            </cd-helper>
          </label>
          <div class="cd-col-form-input">
            <input type="number"
                   id="c"
                   name="c"
                   class="form-control"
                   placeholder="Coding chunks..."
                   formControlName="c"
                   min="1">
            <span class="invalid-feedback"
                  *ngIf="form.showError('c', frm, 'min')"
                  i18n>Must be equal to or greater than 1.</span>
            <span class="invalid-feedback"
                  *ngIf="form.showError('c', frm, 'cGreaterM')"
                  i18n>C has to be equal to or lower than m as m defines the amount of chunks that can be used.</span>
          </div>
        </div>

        <div class="form-group row"
             *ngIf="plugin === 'clay'">
          <label for="d"
                 class="cd-col-form-label">
            <span class="required"
                  i18n>Helper chunks (d)</span>
            <cd-helper [html]="tooltips.plugins.clay.d">
            </cd-helper>
          </label>
          <div class="cd-col-form-input">
            <div class="input-group">
              <input type="number"
                     id="d"
                     name="d"
                     class="form-control"
                     placeholder="Helper chunks..."
                     formControlName="d">
              <button class="btn btn-light"
                      id="d-calc-btn"
                      ngbTooltip="Set d manually or use the plugin's default calculation that maximizes d."
                      i18n-ngbTooltip
                      type="button"
                      (click)="toggleDCalc()">
                <svg [cdsIcon]="dCalc ? icons.unlock : icons.lock"
                     [size]="icons.size16"
                     class="cds-info-color"></svg>
              </button>
            </div>
            <span class="form-text text-muted"
                  *ngIf="dCalc"
                  i18n>D is automatically updated on k and m changes</span>
            <ng-container
              *ngIf="!dCalc">
              <span class="form-text text-muted"
                    *ngIf="getDMin() < getDMax()"
                    i18n>D can be set from {{getDMin()}} to {{getDMax()}}</span>
              <span class="form-text text-muted"
                    *ngIf="getDMin() === getDMax()"
                    i18n>D can only be set to {{getDMax()}}</span>
            </ng-container>
            <span class="invalid-feedback"
                  *ngIf="form.showError('d', frm, 'dMin')"
                  i18n>D has to be greater than k ({{getDMin()}}).</span>
            <span class="invalid-feedback"
                  *ngIf="form.showError('d', frm, 'dMax')"
                  i18n>D has to be lower than k + m ({{getDMax()}}).</span>
          </div>
        </div>

        <div class="form-group row"
             *ngIf="plugin === PLUGIN.LRC">
          <label class="cd-col-form-label"
                 for="l">
            <span class="required"
                  i18n>Locality (l)</span>
            <cd-helper [html]="tooltips.plugins.lrc.l">
            </cd-helper>
          </label>
          <div class="cd-col-form-input">
            <input type="number"
                   id="l"
                   name="l"
                   class="form-control"
                   placeholder="Coding chunks..."
                   formControlName="l"
                   min="1">
            <span class="invalid-feedback"
                  *ngIf="form.showError('l', frm, 'required')"
                  i18n>This field is required!</span>
            <span class="invalid-feedback"
                  *ngIf="form.showError('l', frm, 'min')"
                  i18n>Must be equal to or greater than 1.</span>
            <span class="invalid-feedback"
                  *ngIf="form.showError('l', frm, 'unequal')"
                  i18n>Can't split up chunks (k+m) correctly with the current locality.</span>
            <span class="form-text text-muted"
                  i18n>Locality groups: {{lrcGroups}}</span>
          </div>
        </div>

        <div class="form-group row">
          <label for="crushFailureDomain"
                 class="cd-col-form-label">
            <ng-container i18n>Crush failure domain</ng-container>
            <cd-helper [html]="tooltips.crushFailureDomain">
            </cd-helper>
          </label>
          <div class="cd-col-form-input">
            <select class="form-select"
                    id="crushFailureDomain"
                    name="crushFailureDomain"
                    formControlName="crushFailureDomain"
                    (change)="onCrushFailureDomainChane()">
              <option *ngIf="!failureDomains"
                      ngValue=""
                      i18n>Loading...</option>
              <option *ngFor="let domain of failureDomainKeys"
                      [ngValue]="domain">
                {{ domain }} ( {{failureDomains[domain].length}} )
              </option>
            </select>
          </div>
        </div>

        <div class="form-group row">
          <label for="crushNumFailureDomains"
                 class="cd-col-form-label">
            <ng-container i18n>Crush num failure domain</ng-container>
            <cd-helper [html]="tooltips.crushNumFailureDomains">
            </cd-helper>
          </label>
          <div class="cd-col-form-input">
            <input type="number"
                   id="crushNumFailureDomains"
                   name="crushNumFailureDomains"
                   class="form-control"
                   formControlName="crushNumFailureDomains"
                   min="0">
            <span class="invalid-feedback"
                  *ngIf="form.showError('crushNumFailureDomains', frm, 'required')"
                  i18n>This field is required when crush osds per failure domain is set!</span>
          </div>
        </div>

        <div class="form-group row">
          <label for="crushOsdsPerFailureDomain"
                 class="cd-col-form-label">
            <ng-container i18n>Crush osds per failure domain</ng-container>
            <cd-helper [html]="tooltips.crushOsdsPerFailureDomain">
            </cd-helper>
          </label>
          <div class="cd-col-form-input">
            <input type="number"
                   id="crushOsdsPerFailureDomain"
                   name="crushOsdsPerFailureDomain"
                   class="form-control"
                   formControlName="crushOsdsPerFailureDomain"
                   min="0">
            <span class="invalid-feedback"
                  *ngIf="form.showError('crushOsdsPerFailureDomain', frm, 'required')"
                  i18n>This field is required when crush num failure domain is set!</span>
          </div>
        </div>

        <div class="form-group row"
             *ngIf="plugin === PLUGIN.LRC">
          <label for="crushLocality"
                 class="cd-col-form-label">
            <ng-container i18n>Crush Locality</ng-container>
            <cd-helper [html]="tooltips.plugins.lrc.crushLocality">
            </cd-helper>
          </label>
          <div class="cd-col-form-input">
            <select class="form-select"
                    id="crushLocality"
                    name="crushLocality"
                    formControlName="crushLocality">
              <option *ngIf="!failureDomains"
                      ngValue=""
                      i18n>Loading...</option>
              <option *ngIf="failureDomainKeys.length > 0"
                      ngValue=""
                      i18n>None</option>
              <option *ngFor="let domain of failureDomainKeys"
                      [ngValue]="domain">
                {{ domain }} ( {{failureDomains[domain].length}} )
              </option>
            </select>
          </div>
        </div>

        <div class="form-group row"
             *ngIf="PLUGIN.CLAY === plugin">
          <label for="scalar_mds"
                 class="cd-col-form-label">
            <ng-container i18n>Scalar mds</ng-container>
            <cd-helper [html]="tooltips.plugins.clay.scalar_mds">
            </cd-helper>
          </label>
          <div class="cd-col-form-input">
            <select class="form-select"
                    id="scalar_mds"
                    name="scalar_mds"
                    formControlName="scalar_mds">
              <option *ngFor="let plugin of [PLUGIN.JERASURE, PLUGIN.ISA, PLUGIN.SHEC]"
                      [ngValue]="plugin">
                {{ plugin }}
              </option>
            </select>
          </div>
        </div>

        <div class="form-group row"
             *ngIf="[PLUGIN.JERASURE, PLUGIN.ISA, PLUGIN.CLAY].includes(plugin)">
          <label for="technique"
                 class="cd-col-form-label">
            <ng-container i18n>Technique</ng-container>
            <cd-helper [html]="tooltips.plugins[plugin].technique">
            </cd-helper>
          </label>
          <div class="cd-col-form-input">
            <select class="form-select"
                    id="technique"
                    name="technique"
                    formControlName="technique">
              <option *ngFor="let technique of techniques"
                      [ngValue]="technique">
                {{ technique }}
              </option>
            </select>
          </div>
        </div>

        <div class="form-group row"
             *ngIf="plugin === PLUGIN.JERASURE">
          <label for="packetSize"
                 class="cd-col-form-label">
            <ng-container i18n>Packetsize</ng-container>
            <cd-helper [html]="tooltips.plugins.jerasure.packetSize">
            </cd-helper>
          </label>
          <div class="cd-col-form-input">
            <input type="number"
                   id="packetSize"
                   name="packetSize"
                   class="form-control"
                   placeholder="Packetsize..."
                   formControlName="packetSize"
                   min="1">
            <span class="invalid-feedback"
                  *ngIf="form.showError('packetSize', frm, 'min')"
                  i18n>Must be equal to or greater than 1.</span>
          </div>
        </div>

        <div class="form-group row">
          <label for="crushRoot"
                 class="cd-col-form-label">
            <ng-container i18n>Crush root</ng-container>
            <cd-helper [html]="tooltips.crushRoot">
            </cd-helper>
          </label>
          <div class="cd-col-form-input">
            <select class="form-select"
                    id="crushRoot"
                    name="crushRoot"
                    formControlName="crushRoot">
              <option *ngIf="!buckets"
                      ngValue=""
                      i18n>Loading...</option>
              <option *ngFor="let bucket of buckets"
                      [ngValue]="bucket">
                {{ bucket.name }}
              </option>
            </select>
          </div>
        </div>

        <div class="form-group row">
          <label for="crushDeviceClass"
                 class="cd-col-form-label">
            <ng-container i18n>Crush device class</ng-container>
          </label>
          <div class="cd-col-form-input">
            <select class="form-select"
                    id="crushDeviceClass"
                    name="crushDeviceClass"
                    formControlName="crushDeviceClass">
              <option ngValue=""
                      i18n>All devices</option>
              <option *ngFor="let deviceClass of devices"
                      [ngValue]="deviceClass">
                {{ deviceClass }}
              </option>
            </select>
            <cd-help-text>
              <span i18n>{{tooltips.crushDeviceClass}}</span>
            </cd-help-text>
            <span class="form-text text-muted"
                  i18n>Available OSDs: {{deviceCount}}</span>
          </div>
        </div>

        <div class="form-group row">
          <label for="directory"
                 class="cd-col-form-label">
            <ng-container i18n>Directory</ng-container>
            <cd-helper [html]="tooltips.directory">
            </cd-helper>
          </label>
          <div class="cd-col-form-input">
            <input type="text"
                   id="directory"
                   name="directory"
                   class="form-control"
                   placeholder="Path..."
                   formControlName="directory">
          </div>
        </div>
      </div>

      <div class="modal-footer">
        <cd-form-button-panel (submitActionEvent)="onSubmit()"
                              [form]="form"
                              [submitText]="(action | titlecase) + ' ' + (resource | upperFirst)"></cd-form-button-panel>
      </div>
    </form>
  </ng-container>
</cd-modal>
