<div *ngIf="showTitle" class="main-title" i18n="dashboard.difficulty-adjustment">Difficulty Adjustment</div>
<div class="card-wrapper">
  <div class="card">
    <div class="card-body more-padding">
      <div class="difficulty-adjustment-container" *ngIf="(isLoadingWebSocket$ | async) === false && (difficultyEpoch$ | async) as epochData; else loadingDifficulty">
        <div class="item">
          <h5 class="card-title" i18n="difficulty-box.remaining">Remaining</h5>
          <div class="card-text">
            <ng-container *ngTemplateOutlet="epochData.remainingBlocks === 1 ? blocksSingular : blocksPlural; context: {$implicit: epochData.remainingBlocks }"></ng-container>
            <ng-template #blocksPlural let-i i18n="shared.blocks">{{ i }} <span class="shared-block">blocks</span></ng-template>
            <ng-template #blocksSingular let-i i18n="shared.block">{{ i }} <span class="shared-block">block</span></ng-template>
          </div>
          <div class="symbol"><app-time kind="until" [time]="epochData.estimatedRetargetDate" [fastRender]="true" [precision]="1"></app-time></div>
        </div>
        <div class="item">
          <h5 class="card-title" i18n="difficulty-box.estimate">Estimate</h5>
          <div class="card-text" [ngStyle]="{'color': epochData.colorAdjustments}">
            <span *ngIf="epochData.change > 0; else arrowDownDifficulty" >
              <fa-icon class="retarget-sign" [icon]="['fas', 'caret-up']" [fixedWidth]="true"></fa-icon>
            </span>
            <ng-template #arrowDownDifficulty >
              <fa-icon class="retarget-sign" [icon]="['fas', 'caret-down']" [fixedWidth]="true"></fa-icon>
            </ng-template>
            {{ epochData.change | absolute | number: '1.2-2' }}
            <span class="symbol">%</span>
          </div>
          <div class="symbol">
            <span i18n="difficulty-box.previous">Previous</span>:
            <span [ngStyle]="{'color': epochData.colorPreviousAdjustments}">
              <span *ngIf="epochData.previousRetarget > 0; else arrowDownPreviousDifficulty" >
                <fa-icon class="previous-retarget-sign" [icon]="['fas', 'caret-up']" [fixedWidth]="true"></fa-icon>
              </span>
              <ng-template #arrowDownPreviousDifficulty >
                <fa-icon class="previous-retarget-sign" [icon]="['fas', 'caret-down']" [fixedWidth]="true"></fa-icon>
              </ng-template>
              {{ epochData.previousRetarget | absolute | number: '1.2-2' }} </span> %
          </div>
        </div>
        <div class="item" *ngIf="showProgress">
          <h5 class="card-title" i18n="difficulty-box.current-period">Current Period</h5>
          <div class="card-text">{{ epochData.progress | number: '1.2-2' }} <span class="symbol">%</span></div>
          <div class="progress small-bar">
            <div class="progress-bar" role="progressbar" style="width: 15%; background-color: var(--primary)" [ngStyle]="{'width': epochData.base}">&nbsp;</div>
          </div>
        </div>
        <div class="item" *ngIf="showHalving">
          <h5 class="card-title" i18n="difficulty-box.next-halving">Next Halving</h5>
          <div class="card-text" i18n-ngbTooltip="mining.average-fee" [ngbTooltip]="halvingBlocksLeft" [tooltipContext]="{ epochData: epochData }" placement="bottom">
            <span>{{ timeUntilHalving | date }}</span>
            <div class="symbol" *ngIf="blocksUntilHalving === 1; else approxTime">
              <app-time kind="until" [time]="epochData.adjustedTimeAvg + now" [fastRender]="false" [fixedRender]="true" [precision]="1" minUnit="minute"></app-time>
            </div>
            <ng-template #approxTime>
              <div class="symbol">
                <app-time kind="until" [time]="timeUntilHalving" [fastRender]="false" [fixedRender]="true" [precision]="0" [numUnits]="2" [units]="['year', 'day', 'hour', 'minute']"></app-time>
              </div>
            </ng-template>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

<ng-template #halvingBlocksLeft let-epochData="epochData">
  <ng-container *ngTemplateOutlet="epochData.blocksUntilHalving === 1 ? blocksSingular : blocksPlural; context: {$implicit: epochData.blocksUntilHalving }"></ng-container>
  <ng-template #blocksPlural let-i i18n="shared.blocks">{{ i }} <span class="shared-block">blocks</span></ng-template>
  <ng-template #blocksSingular let-i i18n="shared.block">{{ i }} <span class="shared-block">block</span></ng-template>
</ng-template>

<ng-template #loadingDifficulty>
  <div class="difficulty-skeleton loading-container">
    <div class="item">
      <h5 class="card-title" i18n="difficulty-box.remaining">Remaining</h5>
      <div class="card-text">
        <div class="skeleton-loader"></div>
        <div class="skeleton-loader"></div>
      </div>
    </div>
    <div class="item">
      <h5 class="card-title" i18n="difficulty-box.estimate">Estimate</h5>
      <div class="card-text">
        <div class="skeleton-loader"></div>
        <div class="skeleton-loader"></div>
      </div>
    </div>
    <div class="item">
      <h5 class="card-title" i18n="difficulty-box.next-halving">Next Halving</h5>
      <div class="card-text">
        <div class="skeleton-loader"></div>
        <div class="skeleton-loader"></div>
      </div>
    </div>
  </div>
</ng-template>
