import { Component } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { FormsModule } from '@angular/forms';
import { Router, ActivatedRoute, RouterModule } from '@angular/router';
import { Title } from '@angular/platform-browser';
import { CommonModule } from '@angular/common';
import { Resume } from 'app/models/Resume';
import { Authentication } from 'app/services/Authentication';
import { Validation } from 'app/services/Validation';

@Component({
  selector: 'main-resume-viewer',
  templateUrl: './viewer.component.html',
  styleUrls: [ './viewer.component.css' ],
  imports: [FormsModule, CommonModule, RouterModule]
})
export class ResumesViewerComponent {

  public resume: Resume = new Resume;

  public ErrorMsg: string = "";

  constructor( private http: HttpClient, private router: Router, private route: ActivatedRoute, private title: Title, public auth: Authentication, public validator: Validation ) {
    this.title.setTitle("Resume - Viewer | BoredCareers");
    if (!this.auth.isLoggedIn){
      this.router.navigate(["/"]);
    }
  };

  ngOnInit(){
    this.route.queryParams.subscribe(params => {
      const ResumeID = params['ResumeID'] ? +params['ResumeID'] : null;
      if (ResumeID !== null){
        this.http.get<Resume>("api/resume?ResumeID=" + ResumeID).subscribe({
          next: data => {

            data.trackUUID = crypto.randomUUID();
            data.certifications.forEach(element => {
              element.trackUUID = crypto.randomUUID();
            });
            data.educations.forEach(element => {
              element.trackUUID = crypto.randomUUID();
            });
            data.experiences.forEach(element => {
              element.trackUUID = crypto.randomUUID();
              element.experienceBullets.forEach(subelement => {
                subelement.trackUUID = crypto.randomUUID();
              });
            });
            data.languages.forEach(element => {
              element.trackUUID = crypto.randomUUID();
            });
            if (data.military){
              data.military.trackUUID = crypto.randomUUID();
              data.military.militaryBullets.forEach(element => {
                element.trackUUID = crypto.randomUUID();
              });
            }
            data.projects.forEach(element => {
              element.trackUUID = crypto.randomUUID();
            });
            data.skills.forEach(element => {
              element.trackUUID = crypto.randomUUID();
            });

            this.resume = data;
          },
          error: err => {
            this.ErrorMsg = err.error;
          }
        });
      }
    });
  }

  PrintResume(){
    const divToPrint = document.getElementsByClassName("paper")[0];

    const printContents = divToPrint.innerHTML;
    const originalContents = document.body.innerHTML; // Store original body content

    // Temporarily replace the body content with the div's content
    document.body.innerHTML = printContents;

    // Trigger the print dialog
    window.print();

    // Restore the original body content
    document.body.innerHTML = originalContents;
  }

}