import {Component, isDevMode, OnInit} from '@angular/core';
import {LoginService} from './services/login.service';
import {ActivatedRoute, NavigationStart, Router} from '@angular/router';
import {ModalService} from '@inst-iot/bosch-angular-ui-components';
import {HelpComponent} from './help/help.component';
import {DataService} from './services/data.service';


// TODO: get rid of chart.js (+moment.js)

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit{

  bugReport = {do: '', work: ''};
  isDocumentation = false;
  devMode = false;

  constructor(
    public login: LoginService,
    public router: Router,
    private route: ActivatedRoute,
    public window: Window,
    private modal: ModalService,
    public d: DataService
  ) {
    this.devMode = isDevMode();
    this.router.events.subscribe(event => {
      if (event instanceof NavigationStart) {
        this.isDocumentation = /\/documentation/.test(event.url);
      }
    });
  }

  ngOnInit() {
    this.login.login().then(res => {
      if (!res && !(this.route.snapshot.url.length && this.route.snapshot.url[0].path === '/documentation')) {
        this.router.navigate(['/']);
      }
    });
  }

  logout() {
    this.login.logout();
    this.router.navigate(['/']);
  }

  help() {
    this.modal.openComponent(HelpComponent);
  }

  bugReportContent() {
    return `mailto:lukas.veit@de.bosch.com?subject=Bug report&body=Thanks for sending the report! Your bug will be (hopefully) fixed soon.
%0D%0A%0D%0A--- REPORT DATA ---
%0D%0A%0D%0ATime: ${new Date().toString()}%0D%0A
URL: ${this.window.location}%0D%0A%0D%0AWhat did you do?%0D%0A${encodeURIComponent(this.bugReport.do)}
%0D%0A%0D%0AWhat did not work?%0D%0A${encodeURIComponent(this.bugReport.work)}%0D%0A%0D%0ABrowser:%0D%0A
%0D%0AappCodeName: ${navigator.appCodeName}
%0D%0AappVersion: ${navigator.appVersion}
%0D%0Alanguage: ${navigator.language}
%0D%0AonLine: ${navigator.onLine}
%0D%0Aoscpu: ${navigator.oscpu}
%0D%0Aplatform: ${navigator.platform}
%0D%0AuserAgent: ${navigator.userAgent}
%0D%0AinnerWidth: ${this.window.innerWidth}
%0D%0AinnerHeight: ${this.window.innerHeight}`;
  }

  closeBugReport(close) {
    setTimeout(() => close(), 1);
  }

  toTheTop() {
    this.window.scroll(0, 0);
  }
}