import { Component, OnInit, ViewChild, ElementRef } from '@angular/core';
import { Router } from '@angular/router';
import { BackendService } from '../../../../services/backend/backend.service';
import { Log } from '../../../../models/log.model';
import { ToastrService } from 'ngx-toastr';

@Component({
	selector: 'app-logs',
	templateUrl: './logs.component.html',
	styleUrls: ['./logs.component.css']
})
export class LogsComponent implements OnInit {
	isProcessing:boolean = true;
	confLogs:Log[];
	appLogs:Log[];
	gravity:string[] = ['DEBUG', 'INFO', 'WARNING', 'ERROR'];

	constructor( private bs:BackendService,
			 private router:Router,
			 private toast:ToastrService ) { }

	ngOnInit(): void {
		this.bs.retreiveConfLogs().subscribe(
			(datas:Log[]) => {
				this.confLogs = datas;
				this.isProcessing = false;
			}, err => {
				if(err.status == 401) {
					this.router.navigateByUrl("/login");
				} else {
					this.toast.error(err.error.description);
				}
			}
		);
 	}

	scrollToBottom(): void {
		//this.myList.nativeElement.scrollTop = this.myList.nativeElement.scrollHeight;
	}

	onTabChanged(event:any): void {
		switch(event.tab.textLabel) {
			case 'Configurateur.log':
				this.isProcessing = true;
				this.bs.retreiveConfLogs().subscribe(
					(datas:Log[]) => {
						this.confLogs = datas;
						this.isProcessing = false;
					}, err => {
						if(err.status == 401) {
							this.router.navigateByUrl("/login");
						} else {
							this.toast.error(err.error.description);
						}
					}
				);
				break;
			case 'KineIntercom.log':
				this.isProcessing = true;
				this.bs.retreiveAppLogs().subscribe(
					(datas:Log[]) => {
						this.appLogs = datas;
						this.isProcessing = false;
					}, err => {
						if(err.status == 401) {
							this.router.navigateByUrl("/login");
						} else {
							this.toast.error(err.error.description);
						}
					}
				);
				break;
			default:
				break;
		}
	}
}
