class GoogleAPI{
	fetch=(form,func)=>{fetch(app.wroot+'/main/plugins/googleAPI/GoogleAPI.php',{method:"post",body:form}).then(response=>response.json()).then(result=>{func(result);});}
	login(id,name,email,icon){
		var form=new FormData();form.append('c','sociallogin');form.append('id',id);form.append('name',name);form.append('email',email);form.append('icon',icon);
		this.fetch(form,response=>{
			location.replace(location.href);
		});
	}
	authenticateDummy=()=>{this.login("109687983565828193962","Stephen Duffy","scfduffy@gmail.com","https://lh3.googleusercontent.com/a/ACg8ocJmBONM_aabCP-Vq7aSnsLXD_J79KXCbYN10dHlgWfWhU3lRA=s96-c");}
	authenticate=()=>{google.accounts.id.prompt();}
	recvCredential=(response)=>{
		let json=JSON.parse(decodeURIComponent(atob(response.credential.split('.')[1].replace(/-/g,'+').replace(/_/g,'/')).split('').map((c)=>{return '%'+('00'+c.charCodeAt(0).toString(16)).slice(-2);}).join('')));
		this.login(json.sub,json.name,json.email,json.picture);
	}
	constructor(args){
		this.client_id=args[0];
		var script=document.createElement('script');
		document.head.appendChild(script);
		script.onload=()=>{
			google.accounts.id.initialize({client_id:this.client_id,callback:this.recvCredential});
			this.authenticate();
		}
		script.src='https://accounts.google.com/gsi/client';
	}
}
