const btn = document.querySelector('.talk');
const content = document.querySelector('.content');
let cartItemsNumber;
let cartArray;
if (localStorage.getItem('localCartItemsNumber') !== null){
    cartItemsNumber = localStorage.getItem('localCartItemsNumber');
    document.querySelector('#cart-items-number').innerHTML = localStorage.getItem('localCartItemsNumber');
}else{
    cartItemsNumber = 0;
}

if(localStorage.getItem('products') !== null){
    cartArray = JSON.parse(localStorage.getItem('products'));
}else{
     cartArray = [];
}


document.querySelector('body').style.background = localStorage.getItem('bgColor');
document.querySelector('body').style.color = localStorage.getItem('fontColor');
var nodes = document.querySelectorAll('.card');
for(var i=0; i<nodes.length; i++) {
	nodes[i].style.border = localStorage.getItem('cardBorder');
}

const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
const recognition = new SpeechRecognition();

recognition.onstart = function() {
	console.log('voice is activated!');
};

recognition.onresult = function(event) {
	const current = event.resultIndex;
	const transcript = event.results[current][0].transcript;
// 	content.textContent = transcript;
	readOutLoud(transcript);
};

//add the listener to the button

btn.addEventListener('click', () => {
	recognition.start();
});


function readOutLoud(message) {
	const speech = new SpeechSynthesisUtterance();
	if (message.includes('activate dark mode')) {
		localStorage.setItem('bgColor', '#232323');
		localStorage.setItem('fontColor', '#f2f2f2');
		localStorage.setItem('cardBorder', '1px solid #e8e8e8');
		document.querySelector('body').style.background = localStorage.getItem('bgColor');
		document.querySelector('body').style.color = localStorage.getItem('fontColor');
		var nodes = document.querySelectorAll('.card');
		for(var i=0; i<nodes.length; i++) {
			nodes[i].style.border = localStorage.getItem('cardBorder');
		}
		speech.text = 'dark mode activated';
	}else if(message.includes('activate day mode')) {
		localStorage.setItem('bgColor', '#ffffff');
		localStorage.setItem('fontColor', '#232323');
		document.querySelector('body').style.background = localStorage.getItem('bgColor');
		document.querySelector('body').style.color = localStorage.getItem('fontColor');
		speech.text = 'day mode activated';
	}else if (message.includes('increase font size')) {
	    document.querySelector('#main-body').classList.add("increased-font");
	    speech.text = 'font size increased';
    }else if (message.includes('decrease font size')) {
	    document.querySelector('#main-body').classList.remove("increased-font");
	    speech.text = 'font size decreased';
    }else if (message.includes('buy good laptop')) {
        cartArray.push("Good Laptop");
        localStorage.setItem('products', JSON.stringify(cartArray));
        speech.text = 'product added to cart';
        cartItemsNumber++;
        localStorage.setItem('localCartItemsNumber', cartItemsNumber);
        document.querySelector('#cart-items-number').innerHTML = cartItemsNumber;
    }else if (message.includes('buy better laptop')) {
        cartArray.push("Better Laptop");
        localStorage.setItem('products', JSON.stringify(cartArray));
        speech.text = 'product added to cart';
        cartItemsNumber++;
        localStorage.setItem('localCartItemsNumber', cartItemsNumber);
        document.querySelector('#cart-items-number').innerHTML = cartItemsNumber;
    }else if (message.includes('buy best laptop')) {
        cartArray.push("Best Laptop");
        localStorage.setItem('products', JSON.stringify(cartArray));
        speech.text = 'product added to cart';
        cartItemsNumber++;
        localStorage.setItem('localCartItemsNumber', cartItemsNumber);
        document.querySelector('#cart-items-number').innerHTML = cartItemsNumber;
    }else if (message.includes('go to cart page')) {
	    speech.text = 'Redirecting to cart page';
	    window.location.href = "https://syednazmussadat.com/projects/v-commerce/cart.html";
    }else if (message.includes('go to checkout page')) {
	    speech.text = 'Redirecting to checkout page';
	    window.location.href = "https://syednazmussadat.com/projects/v-commerce/checkout.html";
    }else if (message.includes('confirm payment')) {
	    speech.text = 'confirming payment';
	    localStorage.setItem('localCartItemsNumber', 0);
	    cartArray = [];
	    localStorage.setItem('products', JSON.stringify(cartArray));
	    window.location.href = "https://syednazmussadat.com/projects/v-commerce/success.html";
    }else if (message.includes('go to home page')) {
	    speech.text = 'Redirecting to home page';
	    window.location.href = "https://syednazmussadat.com/projects/v-commerce/";
    }else{
	    speech.text = 'Sorry, I did not understand!';
    }
	
	const element = document.activeElement;
	if (message.includes('Smith at gmail.com')) {
	    document.activeElement.value = "smith@gmail.com"
    }else{
        document.activeElement.value = message;
    }

	speech.volume = 1;
	speech.rate = 1;
	speech.pitch = 1;
	window.speechSynthesis.speak(speech);
}