import { ChangeDetectionStrategy, Component, inject } from '@angular/core';
import { TranslatePipe } from '../../../../i18n/translate.pipe';
import { TranslateService } from '../../../../i18n/translate.service';
import { ProjectEditorFacade } from '../../facade/project-editor.facade';
import { ButtonComponent } from '../../../../shared/ui/button/button.component';

@Component({
  selector: 'app-project-editor-save-bar',
  standalone: true,
  imports: [TranslatePipe, ButtonComponent],
  templateUrl: './project-editor-save-bar.component.html',
  styleUrls: ['./project-editor-save-bar.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class ProjectEditorSaveBarComponent {
  private readonly facade = inject(ProjectEditorFacade);
  private readonly translate = inject(TranslateService);
  readonly dirty = this.facade.dirty;
  readonly status = this.facade.status;
  readonly issues = this.facade.validationIssues;
  readonly hasBlockingIssues = this.facade.hasBlockingIssues;
  readonly canUndo = this.facade.canUndo;
  readonly canRedo = this.facade.canRedo;

  undo(): void {
    this.facade.undo();
  }

  redo(): void {
    this.facade.redo();
  }
  readonly lastSavedAt = this.facade.lastSavedAt;
  readonly draftRestored = this.facade.draftRestored;

  save(): void {
    this.facade.save();
  }

  publish(): void {
    this.facade.publish();
  }

  resetDraft(): void {
    if (confirm(this.translate.t('builder.confirmResetDraft'))) {
      this.facade.resetDraft();
    }
  }

  formatSavedAt(timestamp: number): string {
    return new Date(timestamp).toLocaleTimeString();
  }

  dismissDraftRestoredNotice(): void {
    this.facade.dismissDraftRestoredNotice();
  }
}
