import { Component, OnInit, Input } from '@angular/core';
import { ActivatedRoute, Params } from '@angular/router';//for parameterized routing
import { mdl } from '../directives/material-design-lite-upgrade-element.directive';
import { Place } from '../model/place';
import { PlaceService } from '../service/place.service';
import 'rxjs/add/operator/switchMap';
import { BookmarkService } from "../service/bookmark.service";
import { SessionService } from "../service/session.service";
import { CommonService } from "../service/common.service";

@Component({
  selector: 'app-view-place',
  providers: [PlaceService],//provides promises
  templateUrl: './view-place.component.html',
  styleUrls: ['./view-place.component.css'],
})
export class ViewPlaceComponent implements OnInit {

  name: string = "view-place";
  @Input() place: Place;
  //The links to be shared on social netwokrk
  public repoUrl: string = "https://aurageo.com" + window.location.pathname;
  public imageUrl: string;

  constructor(
    private placeService: PlaceService,
    private route: ActivatedRoute,//for parameterized routing
    private bookmarkService: BookmarkService,
    private sessionService: SessionService,
    private commonService: CommonService
  ) { }

  ngOnInit() {
    this.sessionService.checkLogin();
    this.getPlace();
  }

  getPlace() {
    this.route.params
      .switchMap((params: Params) => this.placeService.getPlace(+params['id']))
      .subscribe(place => this.place = place);
  }

  createBookmark(place: Place) {
    if (this.sessionService.isLogged()) 
 else {
      console.log('entra qui');
      this.commonService.showLogin();
    }
  }

  deleteBookmark(place: Place) {
    if (this.sessionService.isLogged()) {
      this.bookmarkService.deleteBookmark(place);
      this.place.bookmarked = false;
      console.log("bookmark deleted");
    } else {
      this.commonService.showLogin();
    }
  }
}

{
      this.bookmarkService.createBookmark(place);
      this.place.bookmarked = true;
      console.log('bookmark created');
    }