<div class="video-edit" [formGroup]="form">
  <div ngbNav #nav="ngbNav" class="nav-tabs">

    <ng-template #pluginSettingTemplate let-pluginSetting>
      <div class="form-group" [hidden]="isPluginFieldHidden(pluginSetting)">
        <my-dynamic-form-field [form]="pluginDataFormGroup" [formErrors]="formErrors['pluginData']" [setting]="pluginSetting.commonOptions"></my-dynamic-form-field>
      </div>
    </ng-template>

    <ng-container ngbNavItem>
      <a ngbNavLink i18n>Basic info</a>

      <ng-template ngbNavContent>
        <div class="form-columns">
          <div class="col-video-edit">
            <div class="form-group">
              <label i18n for="name">Title</label>
              <input type="text" id="name" class="form-control" formControlName="name" />
              <div *ngIf="formErrors.name" class="form-error" role="alert">
                {{ formErrors.name }}
              </div>
            </div>

            <div class="form-group">
              <label for="label-tags" i18n class="label-tags">Tags</label>

              <my-help>
                <ng-template ptTemplate="customHtml">
                  <ng-container i18n>
                    Tags could be used to suggest relevant recommendations. <br />
                    There is a maximum of 5 tags. <br />
                    Press <kbd>Enter</kbd> to add a new tag.
                  </ng-container>
                </ng-template>
              </my-help>

              <my-select-tags labelForId="label-tags" formControlName="tags"></my-select-tags>
              <div *ngIf="formErrors.tags" class="form-error" role="alert">
                {{ formErrors.tags }}
              </div>
            </div>

            <div class="form-group">
              <label i18n for="description">Description</label>

              <my-help helpType="markdownText">
                <ng-template ptTemplate="preHtml">
                  <ng-container i18n>Video descriptions are truncated by default and require manual action to expand them.</ng-container>

                  <br />

                  <ng-container i18n>A timestamp (<i>00:05</i> for example) is automatically converted into a link to a part of the video.</ng-container>
                </ng-template>
              </my-help>

              <my-markdown-textarea
                formControlName="description" [markdownVideo]="publishedVideo"
                [formError]="formErrors.description" [truncateTo3Lines]="true"
              ></my-markdown-textarea>
            </div>
          </div>

          <div class="col-video-edit">
            <div class="form-group">
              <label i18n for="channel">Channel</label>
              <my-select-channel labelForId="channel" [items]="userVideoChannels" formControlName="channelId"></my-select-channel>
            </div>

            <div class="form-group">
              <label i18n for="category">Category</label>
              <my-select-options
                labelForId="category" [items]="videoCategories" formControlName="category" [clearable]="true"
              ></my-select-options>

              <div *ngIf="formErrors.category" class="form-error" role="alert">
                {{ formErrors.category }}
              </div>
            </div>

            <div class="form-group">
              <label i18n for="licence">Licence</label>

              <my-help>
                <ng-template ptTemplate="customHtml">
                  <ng-container i18n>
                    <a href="https://chooser-beta.creativecommons.org/" target="_blank" rel="noopener noreferrer">Choose</a> the appropriate licence for your work.
                  </ng-container>
                </ng-template>
              </my-help>

              <my-select-options
                labelForId="licence" [items]="videoLicences" formControlName="licence" [clearable]="true"
              ></my-select-options>

              <div *ngIf="formErrors.licence" class="form-error" role="alert">
                {{ formErrors.licence }}
              </div>
            </div>

            <div class="form-group">
              <label i18n for="language">Language</label>
              <my-select-options
                labelForId="language" [items]="videoLanguages" formControlName="language"
                [clearable]="true" [searchable]="true" [groupBy]="'group'"
              ></my-select-options>

              <div *ngIf="formErrors.language" class="form-error" role="alert">
                {{ formErrors.language }}
              </div>
            </div>

            <div class="form-group">
              <label i18n for="privacy">Privacy</label>
              <my-select-options
                labelForId="privacy" [items]="videoPrivacies" formControlName="privacy" [clearable]="false"
              ></my-select-options>

              <div *ngIf="formErrors.privacy" class="form-error" role="alert">
                {{ formErrors.privacy }}
              </div>
            </div>

            <div *ngIf="passwordProtectionSelected" class="form-group">
              <label i18n for="videoPassword">Password</label>
              <my-input-text formControlName="videoPassword" inputId="videoPassword" [withCopy]="true" [formError]="formErrors['videoPassword']"></my-input-text>
            </div>

            <div *ngIf="schedulePublicationSelected" class="form-group">
              <label i18n for="schedulePublicationAt">Schedule publication ({{ calendarTimezone }})</label>
              <p-calendar
                id="schedulePublicationAt" formControlName="schedulePublicationAt" [dateFormat]="calendarDateFormat"
                [firstDayOfWeek]="0" [minDate]="minScheduledDate" [showTime]="true" [hideOnDateTimeSelect]="true"
              >
              </p-calendar>

              <div *ngIf="formErrors.schedulePublicationAt" class="form-error" role="alert">
                {{ formErrors.schedulePublicationAt }}
              </div>
            </div>

            <my-peertube-checkbox inputName="nsfw" formControlName="nsfw" helpPlacement="bottom-right">
              <ng-template ptTemplate="label">
                <ng-container i18n>Contains sensitive content</ng-container>
              </ng-template>

              <ng-template ptTemplate="help">
                <ng-container i18n>Some instances hide videos containing mature or explicit content by default.</ng-container>
              </ng-template>
            </my-peertube-checkbox>

            <my-peertube-checkbox *ngIf="!hideWaitTranscoding" inputName="waitTranscoding" formControlName="waitTranscoding" helpPlacement="bottom-right">
              <ng-template ptTemplate="label">
                <ng-container i18n>Publish after transcoding</ng-container>
              </ng-template>

              <ng-template ptTemplate="help">
                <ng-container i18n>The video may be unplayable during the transcoding process. It's the reason why we prefer to publish publicly the video after transcoding.</ng-container>
              </ng-template>
            </my-peertube-checkbox>

            <ng-container
              *ngFor="let pluginSetting of getPluginsFields('main')"
              [ngTemplateOutlet]="pluginSettingTemplate" [ngTemplateOutletContext]="{ $implicit: pluginSetting }"
            >
            </ng-container>
          </div>
        </div>
      </ng-template>
    </ng-container>

    <ng-container ngbNavItem *ngIf="!liveVideo">
      <a ngbNavLink i18n>Captions</a>

      <ng-template ngbNavContent>
        <div class="captions">

          <div class="captions-header">
            <button (click)="openAddCaptionModal()" class="peertube-create-button">
              <my-global-icon iconName="add" aria-hidden="true"></my-global-icon>
              <ng-container i18n>Add another caption</ng-container>
            </button>
          </div>

          <div class="form-group" *ngFor="let videoCaption of videoCaptions">

            <div class="caption-entry">
              <ng-container *ngIf="!videoCaption.action">
                <a
                  i18n-title title="See the subtitle file" class="caption-entry-label" target="_blank" rel="noopener noreferrer"
                  [href]="videoCaption.captionPath"
                >{{ videoCaption.language.label }}</a>

                <div i18n class="caption-entry-state">Already uploaded on {{ videoCaption.updatedAt | date }}	&#10004;</div>

                <button i18n class="caption-entry-edit" (click)="openEditCaptionModal(videoCaption)">Edit</button>
                <button i18n class="caption-entry-delete" (click)="deleteCaption(videoCaption)">Delete</button>
              </ng-container>

              <ng-container *ngIf="videoCaption.action === 'CREATE'">
                <span class="caption-entry-label">{{ videoCaption.language.label }}</span>

                <div i18n class="caption-entry-state caption-entry-state-create">Will be created on update</div>

                <button i18n class="caption-entry-delete" (click)="deleteCaption(videoCaption)">Cancel create</button>
              </ng-container>

              <ng-container *ngIf="videoCaption.action === 'UPDATE'">
                <span class="caption-entry-label">{{ videoCaption.language.label }}</span>

                <div i18n class="caption-entry-state caption-entry-state-create">Will be edited on update</div>

                <button i18n class="caption-entry-delete" (click)="deleteCaption(videoCaption)">Cancel edition</button>
              </ng-container>

              <ng-container *ngIf="videoCaption.action === 'REMOVE'">
                <span class="caption-entry-label">{{ videoCaption.language.label }}</span>

                <div i18n class="caption-entry-state caption-entry-state-delete">Will be deleted on update</div>

                <button i18n class="caption-entry-delete" (click)="deleteCaption(videoCaption)">Cancel deletion</button>
              </ng-container>
            </div>
          </div>

          <div i18n class="no-caption" *ngIf="videoCaptions?.length === 0">
            No captions for now.
          </div>

        </div>
      </ng-template>
    </ng-container>

    <ng-container ngbNavItem *ngIf="!liveVideo">
      <a ngbNavLink i18n>Chapters</a>

      <ng-template ngbNavContent>
        <div class="row mb-5">
          <div class="chapters col-md-12 col-xl-6" formArrayName="chapters">
            <ng-container *ngFor="let chapterControl of getChaptersFormArray().controls; let i = index">
              <div class="chapter" [formGroupName]="i">
                <!-- Row 1 -->
                <div></div>

                <label i18n [ngClass]="{ 'hide-chapter-label': i !== 0 }" [for]="'timecode[' + i + ']'">Timecode</label>

                <label i18n [ngClass]="{ 'hide-chapter-label': i !== 0 }" [for]="'title[' + i + ']'">Chapter name</label>

                <div></div>

                <!-- Row 2 -->
                <div class="position">{{ i + 1 }}</div>

                <my-timestamp-input
                  class="d-block" [disableBorder]="false" [inputName]="'timecode[' + i + ']'"
                  [maxTimestamp]="publishedVideo?.duration" formControlName="timecode"
                ></my-timestamp-input>

                <div>
                  <input
                    [ngClass]="{ 'input-error': formErrors.chapters[i].title }"
                    type="text" [id]="'title[' + i + ']'" [name]="'title[' + i + ']'" formControlName="title"
                  />

                  <div [ngClass]="{ 'opacity-0': !formErrors.chapters[i].title }" class="form-error" role="alert">
                    <span class="opacity-0">t</span> <!-- Ensure we have reserve a correct height -->
                    {{ formErrors.chapters[i].title }}
                  </div>
                </div>

                <my-delete-button *ngIf="!isLastChapterControl(i)" (click)="deleteChapterControl(i)"></my-delete-button>
              </div>
            </ng-container>

            <div *ngIf="getChapterArrayErrors()" class="form-error" role="alert">
              {{ getChapterArrayErrors() }}
            </div>
          </div>

          <my-embed *ngIf="publishedVideo" class="col-md-12 col-xl-6" [video]="publishedVideo"></my-embed>
        </div>
      </ng-template>
    </ng-container>

    <ng-container ngbNavItem *ngIf="liveVideo">
      <a ngbNavLink i18n>Live settings</a>

      <ng-template ngbNavContent>
        <div class="row live-settings">
          <div class="col-md-12">
            <div class="alert pt-alert-primary">
              <my-live-documentation-link></my-live-documentation-link>
            </div>

            <div *ngIf="liveVideo.rtmpUrl" class="form-group">
              <label for="liveVideoRTMPUrl" i18n>Live RTMP Url</label>
              <my-input-text inputId="liveVideoRTMPUrl" [value]="liveVideo.rtmpUrl" [withToggle]="false" [withCopy]="true" [show]="true" [readonly]="true"></my-input-text>
            </div>

            <div *ngIf="liveVideo.rtmpsUrl" class="form-group">
              <label for="liveVideoRTMPSUrl" i18n>Live RTMPS Url</label>
              <my-input-text inputId="liveVideoRTMPSUrl" [value]="liveVideo.rtmpsUrl" [withToggle]="false" [withCopy]="true" [show]="true" [readonly]="true"></my-input-text>
            </div>

            <div class="form-group">
              <label for="liveVideoStreamKey" i18n>Live stream key</label>
              <my-input-text inputId="liveVideoStreamKey" [value]="liveVideo.streamKey" [withCopy]="true" [readonly]="true"></my-input-text>

              <div class="form-group-description" i18n>⚠️ Never share your stream key with anyone.</div>
            </div>

            <div class="form-group">
              <div class="peertube-radio-container">
                <input type="radio" formControlName="permanentLive" id="permanentLiveFalse" [value]="false">
                <label i18n for="permanentLiveFalse" class="radio">This is a normal live</label>

                <span class="form-group-description" i18n>
                  You can stream only once in a normal live. If you enable replay, it will be saved under the same URL as your live
                </span>
              </div>

              <div class="peertube-radio-container">
                <input type="radio" formControlName="permanentLive" id="permanentLiveTrue" [value]="true">
                <label i18n for="permanentLiveTrue" class="radio">This is a permanent/recurring live</label>

                <span class="form-group-description" i18n>
                  You can stream multiple times in a permanent/recurring live. If you enable replays, they will be saved as separate videos
                </span>
              </div>
            </div>

            <div class="form-group" *ngIf="isSaveReplayAllowed()">
              <my-peertube-checkbox inputName="liveVideoSaveReplay" formControlName="saveReplay">
                <ng-template ptTemplate="label">
                  <ng-container i18n>Automatically publish a replay when your live ends</ng-container>
                </ng-template>

                <ng-container ngProjectAs="description">
                  <span i18n>⚠️ If you enable this option, your live will be terminated if you exceed your video quota</span>
                </ng-container>
              </my-peertube-checkbox>
            </div>

            <div class="form-group mx-4" *ngIf="isSaveReplayEnabled()">
              <label i18n for="replayPrivacy">Privacy of the new replay</label>
              <my-select-options
                labelForId="replayPrivacy" [items]="replayPrivacies" [clearable]="false" formControlName="replayPrivacy"
              ></my-select-options>
            </div>

            <div class="form-group" *ngIf="isLatencyModeEnabled()">
              <label i18n for="latencyMode">Latency mode</label>
              <my-select-options
                labelForId="latencyMode" [items]="latencyModes" formControlName="latencyMode" [clearable]="true"
              ></my-select-options>

              <div *ngIf="formErrors.latencyMode" class="form-error" role="alert">
                {{ formErrors.latencyMode }}
              </div>
            </div>
          </div>
        </div>
      </ng-template>

    </ng-container>

    <ng-container ngbNavItem>
      <a ngbNavLink i18n>Advanced settings</a>

      <ng-template ngbNavContent>
        <div class="row advanced-settings">
          <div class="col-md-12 col-xl-8">

            <ng-content></ng-content>

            <div class="form-group">
              <label i18n for="previewfile">Video thumbnail</label>

              <my-thumbnail-manager id="previewfile" formControlName="previewfile" [video]="publishedVideo"></my-thumbnail-manager>
            </div>

            <div class="form-group">
              <label i18n for="support">Support</label>
              <my-help helpType="markdownEnhanced">
                <ng-template ptTemplate="preHtml">
                  <ng-container i18n>
                    Short text to tell people how they can support you (membership platform...).
                  </ng-container>
                </ng-template>
              </my-help>

              <my-markdown-textarea
                id="support" formControlName="support" markdownType="enhanced"
                [formError]="formErrors['support']"
              ></my-markdown-textarea>
            </div>
          </div>

          <div class="col-md-12 col-xl-4">

            <div *ngIf="videoSource" class="form-group">
              <label i18n for="filename">Filename</label>

              <my-help>
                <ng-template ptTemplate="preHtml">
                  <ng-container i18n>
                    Name of the uploaded file
                  </ng-container>
                </ng-template>
              </my-help>

              <input type="text" [disabled]="true" id="filename" class="form-control" [value]="videoSource.filename" />
            </div>

            <div class="form-group originally-published-at">
              <div class="label-container">
                <label i18n for="originallyPublishedAt">Original publication date</label>

                <button i18n class="reset-button reset-button-small" (click)="resetField('originallyPublishedAt')" *ngIf="hasPublicationDate()">
                  Reset
                </button>
              </div>

              <div class="form-group-description" i18n>
                This is the date when the content was originally published (e.g. the release date for a film)
              </div>

              <p-calendar
                id="originallyPublishedAt" formControlName="originallyPublishedAt" [dateFormat]="calendarDateFormat" [firstDayOfWeek]="0"
                [showTime]="true" [hideOnDateTimeSelect]="true" [monthNavigator]="true" [yearNavigator]="true" [yearRange]="myYearRange"
              >
              </p-calendar>

              <div *ngIf="formErrors.originallyPublishedAt" class="form-error" role="alert">
                {{ formErrors.originallyPublishedAt }}
              </div>
            </div>

            <div class="form-group mb-4">
              <label i18n for="commentsPolicy">Comments policy</label>
              <my-select-options labelForId="commentsPolicy" [items]="commentPolicies" formControlName="commentsPolicy" [clearable]="false"></my-select-options>

              <div *ngIf="formErrors.commentsPolicy" class="form-error" role="alert">
                {{ formErrors.commentsPolicy }}
              </div>
            </div>

            <my-peertube-checkbox
              inputName="downloadEnabled" formControlName="downloadEnabled"
              i18n-labelText labelText="Enable download"
            ></my-peertube-checkbox>
          </div>
        </div>
      </ng-template>
    </ng-container>

    <ng-container ngbNavItem *ngIf="getPluginsFields('plugin-settings').length !== 0">
      <a ngbNavLink i18n>Plugin settings</a>

      <ng-template ngbNavContent>
        <div class="row plugin-settings">

          <div class="col-md-12 col-xl-8">
            <ng-container
              *ngFor="let pluginSetting of getPluginsFields('plugin-settings')"
              [ngTemplateOutlet]="pluginSettingTemplate" [ngTemplateOutletContext]="{ $implicit: pluginSetting }"
            >
            </ng-container>
          </div>

        </div>
      </ng-template>
    </ng-container>
  </div>

  <div [ngbNavOutlet]="nav"></div>
</div>

<my-video-caption-add-modal
  #videoCaptionAddModal [existingCaptions]="getExistingCaptions()" [serverConfig]="serverConfig" (captionAdded)="onCaptionEdited($event)"
></my-video-caption-add-modal>
