import { ChangeDetectionStrategy, Component, inject, signal } from '@angular/core';
import { RouterLink } from '@angular/router';
import { TranslatePipe } from '../../../../i18n/translate.pipe';
import { TranslateService } from '../../../../i18n/translate.service';
import { LangRoutePipe } from '../../../../pipes/lang-route.pipe';
import { ProjectEditorFacade } from '../../facade/project-editor.facade';
import { ButtonComponent } from '../../../../shared/ui/button/button.component';
import { ConfirmDialogComponent } from '../../../../shared/ui/confirm-dialog/confirm-dialog.component';

@Component({
  selector: 'app-project-editor-save-bar',
  standalone: true,
  imports: [TranslatePipe, ButtonComponent, ConfirmDialogComponent, RouterLink, LangRoutePipe],
  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;
  readonly changeSummary = this.facade.changeSummary;
  readonly changesOpen = signal(false);

  toggleChanges(): void {
    this.changesOpen.update(open => !open);
  }

  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();
  }

  readonly publishConfirmOpen = signal(false);
  readonly resetDraftConfirmOpen = signal(false);

  publish(): void {
    this.publishConfirmOpen.set(true);
  }

  confirmPublish(): void {
    this.publishConfirmOpen.set(false);
    this.facade.publish();
  }

  resetDraft(): void {
    this.resetDraftConfirmOpen.set(true);
  }

  confirmResetDraft(): void {
    this.resetDraftConfirmOpen.set(false);
    this.facade.resetDraft();
  }

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

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