import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { MatDialog } from '@angular/material/dialog';
import { ConfirmComponent } from '../../../dialog/confirm/confirm.component';
import { BackendService } from '../../../../services/backend/backend.service';
import { Info } from '../../../../models/info.model';
import { Datetime } from '../../../../models/datetime.model';
import { ToastrService } from 'ngx-toastr';

@Component({
	selector: 'app-infos',
	templateUrl: './infos.component.html',
	styleUrls: ['./infos.component.css']
})
export class InfosComponent implements OnInit {
	infos:Info;
	cpu_color:string = "green";
	mem_color:string = "green";
	disk_color:string = "green";
	qos_color:string = "green";
	isProcessing:boolean = true;
	isAlive:boolean = false;
	RTCisAlive:boolean = false;
	today:number;
	dateStr:string;

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

	ngOnInit(): void {
		this.bs.retreiveDatetime().subscribe(
			(data:Datetime) => {
				this.dateStr = data.today;
			}, err => {
				if(err.status == 401) {
					this.router.navigateByUrl("/login");
				} else {
					this.toast.error(err.error.description);
				}
			}
		);
		setInterval(() => {
			this.bs.retreiveDatetime().subscribe(
				(data:Datetime) => {
					this.dateStr = data.today;
				}, err => {
					if(err.status == 401) {
						this.router.navigateByUrl("/login");
					} else {
						this.toast.error(err.error.description);
					}
				}
			);
		}, 60000);

		this.bs.retreiveInfos().subscribe(
			(data:Info) => {
				this.infos = data;
				if(data.system.percent_used_mem >= 65) {
					this.mem_color = "orange";
				} else if(data.system.percent_used_mem > 80) {
					this.mem_color = "red";
				} else {}

				if(data.system.cpu_usage >= 70) {
					this.cpu_color = "orange";
				} else if(data.system.cpu_usage > 80) {
					this.cpu_color = "red";
				} else {}

				if(data.system.percent_used_disk >= 50) {
					this.disk_color = "orange";
				} else if(data.system.percent_used_disk > 80) {
					this.disk_color = "red";
				} else {}

				if(data.signal_qos == "Marginal") {
					this.qos_color = "red";
				} else if(data.signal_qos == "OK") {
					this.qos_color = "orange";
				} else if(data.signal_qos == "Good") {
					this.qos_color = "blue";
				} else if(data.signal_qos == "Excellent") {
					this.qos_color = "green";
				} else {}
				// demande de message de vie du process KineIntercom
				this.bs.processAlive().subscribe(
					data => {
						this.isAlive = data['alive'];
						this.bs.RTCAlive().subscribe(
							data => {
								this.RTCisAlive = data['RTCalive'];
								this.isProcessing = false;
							}, err => {
								if(err.status == 401) {
									this.router.navigateByUrl("/login");
								} else {
									this.toast.error(err.error.description);
								}
							}
						);
					}, err => {
						if(err.status == 401) {
							this.router.navigateByUrl("/login");
						} else {
							this.toast.error(err.error.description);
						}
					}
				);
			}, err => {
				if(err.status == 401) {
					this.router.navigateByUrl("/login");
				} else {
					this.toast.error(err.error.description);
				}
			}
		);
	}

	onReboot():void {
		this.bs.rebootSys().subscribe(
			data => {
				this.toast.info("Redémarrage du système en cours ...");
				this.router.navigateByUrl("/login");
			}, err => {
				if(err.status == 401) {
					this.router.navigateByUrl("/login");
				} else {
					this.toast.error(err.error.description);
				}
			}
		)
	}

	onShutdown():void {
		var title:string = "Confirmation";
		let text:string = "Voulez-vous arrêter le système ?";
		const myDialog = this.dialog.open(ConfirmComponent, {
			disableClose: true,
			data: {
				title: title,
				text: text,
				labelOK: 'Confirmer',
				labelNOK: 'Annuler'
			}
		});

		myDialog.afterClosed().subscribe(
			data => {
				if(data) {
					this.bs.shutdownSys().subscribe(
						data => {
							this.toast.info("Arrêt du système ...");
							this.router.navigateByUrl("/login");
						}, err => {
							if(err.status == 401) {
								this.router.navigateByUrl("/login");
							} else {
								this.toast.error(err.error.description);
							}
						}
					)
				}
			}
		);
	}
}
