<form role="form" [formGroup]="form">

  <ngb-tabset class="root-tabset bootstrap">

    <ngb-tab i18n-title title="Basic configuration">
      <ng-template ngbTabContent>

        <div i18n class="inner-form-title">Instance</div>

        <div class="form-group">
          <label i18n for="instanceName">Name</label>
          <input
            type="text" id="instanceName"
            formControlName="instanceName" [ngClass]="{ 'input-error': formErrors['instanceName'] }"
          >
          <div *ngIf="formErrors.instanceName" class="form-error">
            {{ formErrors.instanceName }}
          </div>
        </div>

        <div class="form-group">
          <label i18n for="instanceShortDescription">Short description</label>
          <textarea
            id="instanceShortDescription" formControlName="instanceShortDescription"
            [ngClass]="{ 'input-error': formErrors['instanceShortDescription'] }"
          ></textarea>
          <div *ngIf="formErrors.instanceShortDescription" class="form-error">
            {{ formErrors.instanceShortDescription }}
          </div>
        </div>

        <div class="form-group">
          <label i18n for="instanceDescription">Description</label><my-help helpType="markdownText"></my-help>
          <my-markdown-textarea
            id="instanceDescription" formControlName="instanceDescription" textareaWidth="500px" [previewColumn]="true"
            [classes]="{ 'input-error': formErrors['instanceDescription'] }"
          ></my-markdown-textarea>
          <div *ngIf="formErrors.instanceDescription" class="form-error">
            {{ formErrors.instanceDescription }}
          </div>
        </div>

        <div class="form-group">
          <label i18n for="instanceTerms">Terms</label><my-help helpType="markdownText"></my-help>
          <my-markdown-textarea
            id="instanceTerms" formControlName="instanceTerms" textareaWidth="500px" [previewColumn]="true"
            [ngClass]="{ 'input-error': formErrors['instanceTerms'] }"
          ></my-markdown-textarea>
          <div *ngIf="formErrors.instanceTerms" class="form-error">
            {{ formErrors.instanceTerms }}
          </div>
        </div>

        <div class="form-group">
          <label i18n for="instanceDefaultClientRoute">Default client route</label>
          <div class="peertube-select-container">
            <select id="instanceDefaultClientRoute" formControlName="instanceDefaultClientRoute">
              <option i18n value="/videos/trending">Videos Trending</option>
              <option i18n value="/videos/recently-added">Videos Recently Added</option>
              <option i18n value="/videos/local">Local videos</option>
            </select>
          </div>
          <div *ngIf="formErrors.instanceDefaultClientRoute" class="form-error">
            {{ formErrors.instanceDefaultClientRoute }}
          </div>
        </div>

        <div class="form-group">
          <label i18n for="instanceDefaultNSFWPolicy">Policy on videos containing sensitive content</label>
          <my-help
            helpType="custom" i18n-customHtml
            customHtml="With <strong>Do not list</strong> or <strong>Blur thumbnails</strong>, a confirmation will be requested to watch the video."
          ></my-help>

          <div class="peertube-select-container">
            <select id="instanceDefaultNSFWPolicy" formControlName="instanceDefaultNSFWPolicy">
              <option i18n value="do_not_list">Do not list</option>
              <option i18n value="blur">Blur thumbnails</option>
              <option i18n value="display">Display</option>
            </select>
          </div>
          <div *ngIf="formErrors.instanceDefaultNSFWPolicy" class="form-error">
            {{ formErrors.instanceDefaultNSFWPolicy }}
          </div>
        </div>

        <div i18n class="inner-form-title">Signup</div>

        <my-peertube-checkbox
          inputName="signupEnabled" formControlName="signupEnabled"
          i18n-labelText labelText="Signup enabled"
        ></my-peertube-checkbox>

        <div *ngIf="isSignupEnabled()" class="form-group">
          <label i18n for="signupLimit">Signup limit</label>
          <input
            type="text" id="signupLimit"
            formControlName="signupLimit" [ngClass]="{ 'input-error': formErrors['signupLimit'] }"
          >
          <div *ngIf="formErrors.signupLimit" class="form-error">
            {{ formErrors.signupLimit }}
          </div>
        </div>

        <div i18n class="inner-form-title">Import</div>

        <my-peertube-checkbox
          inputName="importVideosHttpEnabled" formControlName="importVideosHttpEnabled"
          i18n-labelText labelText="Video import with HTTP enabled"
        ></my-peertube-checkbox>

        <my-peertube-checkbox
          inputName="importVideosTorrentEnabled" formControlName="importVideosTorrentEnabled"
          i18n-labelText labelText="Video import with a torrent file or a magnet URI enabled"
        ></my-peertube-checkbox>

        <div i18n class="inner-form-title">Administrator</div>

        <div class="form-group">
          <label i18n for="adminEmail">Admin email</label>
          <input
            type="text" id="adminEmail"
            formControlName="adminEmail" [ngClass]="{ 'input-error': formErrors['adminEmail'] }"
          >
          <div *ngIf="formErrors.adminEmail" class="form-error">
            {{ formErrors.adminEmail }}
          </div>
        </div>

        <div i18n class="inner-form-title">Users</div>

        <div class="form-group">
          <label i18n for="userVideoQuota">User default video quota</label>
          <div class="peertube-select-container">
            <select id="userVideoQuota" formControlName="userVideoQuota">
              <option *ngFor="let videoQuotaOption of videoQuotaOptions" [value]="videoQuotaOption.value">
                {{ videoQuotaOption.label }}
              </option>
            </select>
          </div>
          <div *ngIf="formErrors.userVideoQuota" class="form-error">
            {{ formErrors.userVideoQuota }}
          </div>
        </div>
      </ng-template>
    </ngb-tab>

    <ngb-tab i18n-title title="Services">
      <ng-template ngbTabContent>
        <div i18n class="inner-form-title">Twitter</div>

        <div class="form-group">
          <label i18n for="signupLimit">Your Twitter username</label>
          <my-help
            helpType="custom" i18n-customHtml
            customHtml="Indicates the Twitter account for the website or platform on which the content was published."
          ></my-help>
          <input
            type="text" id="servicesTwitterUsername"
            formControlName="servicesTwitterUsername" [ngClass]="{ 'input-error': formErrors['servicesTwitterUsername'] }"
          >
          <div *ngIf="formErrors.servicesTwitterUsername" class="form-error">
            {{ formErrors.servicesTwitterUsername }}
          </div>
        </div>

        <my-peertube-checkbox
          inputName="servicesTwitterWhitelisted" formControlName="servicesTwitterWhitelisted"
          i18n-labelText labelText="Instance whitelisted by Twitter"
          i18n-helpHtml helpHtml="If your instance is whitelisted by Twitter, a video player will be embedded in the Twitter feed on PeerTube video share.<br />
  If the instance is not whitelisted, we use an image link card that will redirect on your PeerTube instance.<br /><br />
  Check this checkbox, save the configuration and test with a video URL of your instance (https://example.com/videos/watch/blabla) on <a target='_blank' rel='noopener noreferrer' href='https://cards-dev.twitter.com/validator'>https://cards-dev.twitter.com/validator</a> to see if you instance is whitelisted."
        ></my-peertube-checkbox>
    </ng-template>
    </ngb-tab>

    <ngb-tab i18n-title title="Advanced configuration">
      <ng-template ngbTabContent>

        <div i18n class="inner-form-title">Transcoding</div>

        <my-peertube-checkbox
          inputName="transcodingEnabled" formControlName="transcodingEnabled"
          i18n-labelText labelText="Transcoding enabled"
          i18n-helpHtml helpHtml="If you disable transcoding, many videos from your users will not work!"
        ></my-peertube-checkbox>

        <ng-template [ngIf]="isTranscodingEnabled()">

          <div class="form-group">
            <label i18n for="transcodingThreads">Transcoding threads</label>
            <div class="peertube-select-container">
              <select id="transcodingThreads" formControlName="transcodingThreads">
                <option *ngFor="let transcodingThreadOption of transcodingThreadOptions" [value]="transcodingThreadOption.value">
                  {{ transcodingThreadOption.label }}
                </option>
              </select>
            </div>
            <div *ngIf="formErrors.transcodingThreads" class="form-error">
              {{ formErrors.transcodingThreads }}
            </div>
          </div>

          <div class="form-group" *ngFor="let resolution of resolutions">
            <my-peertube-checkbox
              [inputName]="getResolutionKey(resolution)" [formControlName]="getResolutionKey(resolution)"
              i18n-labelText labelText="Resolution {{resolution}} enabled"
            ></my-peertube-checkbox>

          </div>
        </ng-template>

        <div i18n class="inner-form-title">
          Cache

          <my-help
            helpType="custom" i18n-customHtml
            customHtml="Some files are not federated (previews, captions). We fetch them directly from the origin instance and cache them."
          ></my-help>
        </div>

        <div class="form-group">
          <label i18n for="cachePreviewsSize">Previews cache size</label>
          <input
            type="text" id="cachePreviewsSize"
            formControlName="cachePreviewsSize" [ngClass]="{ 'input-error': formErrors['cachePreviewsSize'] }"
          >
          <div *ngIf="formErrors.cachePreviewsSize" class="form-error">
            {{ formErrors.cachePreviewsSize }}
          </div>
        </div>

        <div class="form-group">
          <label i18n for="cachePreviewsSize">Video captions cache size</label>
          <input
            type="text" id="cacheCaptionsSize"
            formControlName="cacheCaptionsSize" [ngClass]="{ 'input-error': formErrors['cacheCaptionsSize'] }"
          >
          <div *ngIf="formErrors.cacheCaptionsSize" class="form-error">
            {{ formErrors.cacheCaptionsSize }}
          </div>
        </div>

        <div i18n class="inner-form-title">Customizations</div>

        <div class="form-group">
          <label i18n for="customizationJavascript">JavaScript</label>
          <my-help
            helpType="custom" i18n-customHtml
            customHtml="Write directly JavaScript code.<br />Example: <pre>console.log('my instance is amazing');</pre>"
          ></my-help>
          <textarea
            id="customizationJavascript" formControlName="customizationJavascript"
            [ngClass]="{ 'input-error': formErrors['customizationJavascript'] }"
          ></textarea>
          <div *ngIf="formErrors.customizationJavascript" class="form-error">
            {{ formErrors.customizationJavascript }}
          </div>
        </div>

        <div class="form-group">
          <label for="customizationCSS">CSS</label>
          <my-help
              helpType="custom"
              i18n-customHtml
              customHtml="
                Write directly CSS code. Example:<br />
                <pre>
      body {{ '{' }}
        background-color: red;
      {{ '}' }}
                </pre>

                Prepend with <em>#custom-css</em> to override styles. Example:
                <pre>
      #custom-css .logged-in-email {{ '{' }}
        color: red;
      {{ '}' }}
                </pre>
              "
          ></my-help>
          <textarea
            id="customizationCSS" formControlName="customizationCSS"
            [ngClass]="{ 'input-error': formErrors['customizationCSS'] }"
          ></textarea>
          <div *ngIf="formErrors.customizationCSS" class="form-error">
            {{ formErrors.customizationCSS }}
          </div>
        </div>
      </ng-template>
    </ngb-tab>
  </ngb-tabset>

  <input (click)="formValidated()" type="submit" i18n-value value="Update configuration" [disabled]="!form.valid">
  <span class="form-error" i18n *ngIf="!form.valid">It seems the configuration is invalid. Please search potential errors in the different tabs.</span>
</form>
