class AnswerPhone{
	visualise=()=>{
		const bLen=this.analyser.frequencyBinCount;
		const data=new Uint8Array(bLen);
		this.analyser.getByteTimeDomainData(data);
		this.signalCtx.fillRect(0,0,this.signal.width,this.signal.height);
		this.signalCtx.beginPath();this.signalCtx.moveTo(0,this.signal.height/2)
		for (var x=0,i=0;i<bLen;i++,x+=this.signal.width/bLen){var y=this.signal.height*(data[i]/256);this.signalCtx.lineTo(x,y);}
		this.signalCtx.stroke();
		if (this.recording) requestAnimationFrame(this.visualise);
		else this.signalNone();
	}
	signalNone=()=>{
		this.signalCtx.fillStyle=this.signalBg;
		this.signalCtx.fillRect(0,0,this.signal.width,this.signal.height);
		this.signalCtx.fillStyle='white';
		this.signalCtx.fillText("NO AUDIO",this.signal.width/2,this.signal.height*5/8,this.signal.width);
		this.signalCtx.fillStyle=this.signalBg;
	}
	record=()=>{
		this.recording=true;this.recBtn.disabled=true;this.playBtn.disabled=true;this.stopBtn.disabled=false;this.sendBtn.disabled=true;
		if (!this.recorder) this.startRecorder(); else this.recorder.start();
	}
	play=()=>{
		this.recBtn.disabled=true;this.playBtn.disabled=true;this.stopBtn.disabled=false;this.sendBtn.disabled=true;
		this.node=this.audioCtx.createBufferSource();
		this.node.connect(this.audioCtx.destination);
		this.node.onended=()=>{this.stop()};
		this.blob.arrayBuffer().then(compressed=>{this.audioCtx.decodeAudioData(compressed).then(pcm=>{this.node.buffer=pcm;this.node.start();});});
	}
	stop=()=>{
		if (this.recording){this.recorder.stop();this.recording=false;this.recBtn.disabled=false;this.playBtn.disabled=false;this.stopBtn.disabled=true;this.sendBtn.disabled=false;}
		else{this.node.stop();this.recBtn.disabled=false;this.playBtn.disabled=false;this.stopBtn.disabled=true;this.sendBtn.disabled=false;}
	}
	send=()=>{
		var form=new FormData();form.append('c','post');form.append('media',this.blob);
		fetch(this.wroot+'/main/modules/answerPhone/AnswerPhone.php',{method:"post",body:form});
		this.reset();
	}
	reset=()=>{this.mode='none';this.recBtn.disabled=false;this.playBtn.disabled=true;this.stopBtn.disabled=true;this.sendBtn.disabled=true;}
	startRecorder=()=>{
		this.recording=false;
		this.audioCtx=new AudioContext();
		navigator.mediaDevices.getUserMedia({audio:true}).then((stream)=>{
			const source=this.audioCtx.createMediaStreamSource(stream);
			this.analyser=this.audioCtx.createAnalyser();this.analyser.fftSize=2048;source.connect(this.analyser);
			this.recorder=new MediaRecorder(stream,{mimeType:'audio/webm'});
			this.recorder.onstart=()=>{this.chunks=[];this.visualise();}
			this.recorder.ondataavailable=(e)=>{this.chunks.push(e.data);}
			this.recorder.onstop=(e)=>{this.blob=new Blob(this.chunks,{type:this.recorder.mimeType});this.chunks=[];}
			this.record();
		},console.warn).catch(console.warn);
	}
	createElement=(parent,type,className='')=>{var el=document.createElement(type);el.className=className;parent.append(el);return el;}
	createButton=(parent,name,func)=>{var button=this.createElement(parent,'button');button.innerHTML="<img class='button' src='"+this.wroot+"/main/modules/answerPhone/"+name+".png' /> "+name;button.onclick=func;return button;}
	create=()=>{
		this.signal=this.createElement(this.el,'canvas','signal');
		this.signalCtx=this.signal.getContext("2d");
		this.signalCtx.lineWidth=3;this.signalCtx.font="3rem mono";this.signalCtx.textAlign='center';this.signalCtx.strokeStyle="#ccc";
		this.signalBg=this.signalCtx.createLinearGradient(0,255,0,0);this.signalBg.addColorStop(0,'#444');this.signalBg.addColorStop(1,'#000');
		this.signalNone();
		var buttons=this.createElement(this.el,'div');
		this.recBtn=this.createButton(buttons,'record',this.record);
		this.playBtn=this.createButton(buttons,'play',this.play);
		this.stopBtn=this.createButton(buttons,'stop',this.stop);
		this.sendBtn=this.createButton(buttons,'send',this.send);
		this.recorder=false;
	}
	constructor(elid,wroot,user){
		this.user=user;this.wroot=wroot;this.el=document.getElementById(elid);
		this.create();
		this.reset();
	}
}
