// #docregion
import { Component, OnInit, OnDestroy } from '@angular/core';

import { TwainService } from './twain.service';

@Component({
  selector: 'twain-quote',
  template: '<p class="twain"><i>{{quote}}</i></p>'
})
export class TwainComponent  implements OnInit, OnDestroy {
  intervalId: number;
  quote = '-- not initialized yet --';
  constructor(private twainService: TwainService) { }

  getQuote() {
    this.twainService.getQuote().then(quote => this.quote = quote);
  }

  ngOnInit(): void {
    this.getQuote();
    this.intervalId = window.setInterval(() => this.getQuote(), 5000);
  }

  ngOnDestroy(): void {
    clearInterval(this.intervalId);
  }
}
