import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { BookService } from '../services/book.service';
import { Book } from '../models/book';
import { User } from '../models/user';

@Component({
  selector: 'app-user',
  templateUrl: './user.component.html',
  styleUrls: ['./user.component.css']
})
export class UserComponent implements OnInit {
  /*constructor(private activeRoute: ActivatedRoute){
    alert("constructor");
  }*/

  constructor(private bookS: BookService){}

  ngOnInit(): void {
    /*alert("init")
    alert(this.activeRoute.snapshot.paramMap.get("param1"))

    let userLS = localStorage.getItem("logged");
    if(userLS!==null){
      this.user = JSON.parse(userLS);
      alert('Hello' + user.firstname)
    }*/

    this.bookS.getAllBooks().subscribe(
      data=>{
        this.allBooks  = data;
      }
    )
  }

  allBooks: Book[]= []

  likeAll(){
    for(let i=0; i<this.allBooks.length; i++){
      if(this.allBooks[i].toLike){
        this.bookS.likeBook(this.allBooks[i]).subscribe(
          data=>{
            //mozemo ovako jer se u onInit samo radi dovlacenje knjiga sto nam i treba,
            //ali da imamo i neke druge stvari u OnInit, nema potrebe da ponovo sve inicijalizujemo, vec samo treba da promenimo podatak koji zelimo,
            //u nasem slucaju knjige, i onda ne moramo da zovemo this.ngOnInit vec samo kada upisemo i poslednji lajk, da tad dovucemo knjige iz baze,
            //detektovace se promena knjiga i re-renderovace se u html-u
            this.ngOnInit()
          }
        )
      }
    }
  }

}
