import { AfterViewInit, Component, OnDestroy, OnInit } from "@angular/core";
import { marker } from "@biesbjerg/ngx-translate-extract-marker";
import { styleChangedObserver } from "src/app/components/header/header.component";
import { TitleService } from "../../shared/services/title.service";
import { EnvironmentService } from "src/environments/environment.service";
import { MatDialog } from "@angular/material/dialog";
import { DownloadRecoveryCodeDialogComponent } from "src/app/shared/dialogs/download-recovery-code-dialog/download-recovery-code-dialog.component";
import { AuthService } from "src/app/shared/server-services/auth.service";
import { GuideService } from "src/app/shared/guide/guide.service";

@Component({
  selector: "app-welcome",
  templateUrl: "./welcome.component.html",
  styleUrls: ["./welcome.component.scss"],
})
export class WelcomeComponent implements OnInit {
  public colorTheme;
  public siteBase: string;

  constructor(
    private titleService: TitleService,
    private environmentService: EnvironmentService,
    private authService: AuthService,
    private dialog: MatDialog
  ) {
    this.colorTheme = localStorage.getItem("style_mode");
    styleChangedObserver.subscribe((style) => {
      this.colorTheme = style;
    });

    this.siteBase = this.environmentService.site_base;

    this.titleService.setTabTitle(marker("Welcome"));

    if (!this.authService.isAnonym()) { // show it earlier, than the guides directives registered, or it can cause rendering problems
      this.showRecoveryKeyReminder();
    }
  }
  
  ngOnInit(): void {
    
  }

  private showRecoveryKeyReminder: () => void = () => {
    var nextReminderDateString = localStorage.getItem(
      "recoveryKeyDownloadReminderDate"
    );
    if (!nextReminderDateString) {
      this.dialog.open(DownloadRecoveryCodeDialogComponent);
      return;
    }

    var nextReminderDate = new Date(Date.parse(nextReminderDateString));
    var currentDate = new Date();

    if (nextReminderDate < currentDate) {
      this.dialog.open(DownloadRecoveryCodeDialogComponent);
    }
  };
}
