import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
import { Observable } from 'rxjs';
import { environment } from '../../../environments/environment';
import { Utilisateur } from '../../models/utilisateur.model';
import { Parameters } from '../../models/parameters.model';
import { Scheduler } from '../../models/scheduler.model';
import { Log } from '../../models/log.model';
import { Info } from '../../models/info.model';

@Injectable({
	providedIn: 'root'
})
export class BackendService {
	constructor(private http:HttpClient) {}

	loginUser(val:any):Observable<any> {
		let host=environment.host;
		const options = {
			headers: new HttpHeaders({
				'Content-Type' : 'application/json',
				'Authorization' : 'Basic ' + btoa(val['login'] + ':' + val['password']),
			}),
			withCredentials: true
		};

		return this.http.post<any>(host+"/api/configurateur/login", {}, options);
	}

	isConnected():Observable<any> {
		let host=environment.host;
		const options = {
			headers: new HttpHeaders({
				'Content-Type' : 'application/json',
			}),
			withCredentials: true
		};
		return this.http.get<any>(host+"/api/configurateur/userConnected", options);
	}

	logoutUser():Observable<any> {
		let host=environment.host;
		const options = {
			headers: new HttpHeaders({
				'Content-Type' : 'application/json',
			}),
			withCredentials: true
		};
		return this.http.post<any>(host+"/api/configurateur/logout", {}, options);
	}

	getCurrentUser():Observable<Utilisateur> {
		let host=environment.host;
		const options = {
			headers: new HttpHeaders({
				'Content-Type' : 'application/json',
			}),
			withCredentials: true
		};
		return this.http.get<Utilisateur>(host+"/api/configurateur/current", options);
	}

	updatePasswd(val:any):Observable<any> {
		let host=environment.host;
		const options = {
			headers: new HttpHeaders({
				'Content-Type' : 'application/json',
			}),
			withCredentials: true
		};

		return this.http.post<any>(host+"/api/configurateur/update_passwd", val, options);
	}

	retreiveParams():Observable<Parameters> {
		let host=environment.host;
		const options = {
			headers: new HttpHeaders({
				'Content-Type' : 'application/json',
			}),
			withCredentials: true
		};
		return this.http.get<Parameters>(host+"/api/configurateur/params", options);
	}

	updateParams(val:Parameters):Observable<any> {
		let host=environment.host;
		const options = {
			headers: new HttpHeaders({
				'Content-Type' : 'application/json',
			}),
			withCredentials: true
		};

		return this.http.post<any>(host+"/api/configurateur/update_params", val, options);
	}

	retreiveScheduler():Observable<Scheduler> {
		let host=environment.host;
		const options = {
			headers: new HttpHeaders({
				'Content-Type' : 'application/json',
			}),
			withCredentials: true
		};
		return this.http.get<Scheduler>(host+"/api/configurateur/scheduler", options);
	}

	updateScheduler(val:Scheduler):Observable<any> {
		let host=environment.host;
		const options = {
			headers: new HttpHeaders({
				'Content-Type' : 'application/json',
			}),
			withCredentials: true
		};
		return this.http.post<any>(host+"/api/configurateur/update_schedulers", val, options);
	}

	retreiveConfLogs():Observable<Log[]> {
		let host=environment.host;
		const options = {
			headers: new HttpHeaders({
				'Content-Type' : 'application/json',
			}),
			withCredentials: true
		};
		return this.http.get<Log[]>(host+"/api/configurateur/conf_logs", options);
	}

	retreiveAppLogs():Observable<Log[]> {
		let host=environment.host;
		const options = {
			headers: new HttpHeaders({
				'Content-Type' : 'application/json',
			}),
			withCredentials: true
		};
		return this.http.get<Log[]>(host+"/api/configurateur/app_logs", options);
	}

	retreiveInfos():Observable<Info> {
		let host=environment.host;
		const options = {
			headers: new HttpHeaders({
				'Content-Type' : 'application/json',
			}),
			withCredentials: true
		};
		return this.http.get<Info>(host+"/api/configurateur/infos", options);
	}

	processAlive():Observable<any> {
		let host=environment.host;
		const options = {
			headers: new HttpHeaders({
				'Content-Type' : 'application/json',
			}),
			withCredentials: true
		};
		return this.http.get<any>(host+"/api/configurateur/alive", options);
	}

	rebootSys():Observable<any> {
		let host=environment.host;
		const options = {
			headers: new HttpHeaders({
				'Content-Type' : 'application/json',
			}),
			withCredentials: true
		};
		return this.http.post<any>(host+"/api/configurateur/reboot", {}, options);
	}

	shutdownSys():Observable<any> {
		let host=environment.host;
		const options = {
			headers: new HttpHeaders({
				'Content-Type' : 'application/json',
			}),
			withCredentials: true
		};
		return this.http.post<any>(host+"/api/configurateur/shutdown", {}, options);
	}
}
