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'));
    let price;
    for(var i=0; i<cartArray.length; i++) {
        if(cartArray[i] == "Good Laptop"){
            price = 399;
        }else if(cartArray[i] == "Better Laptop"){
            price = 499;
        }else{
            price = 599;
        }
        document.querySelector('#cart-items-list').innerHTML += '<tr><td>' +cartArray[i]+ '</td><td><input type="number" value="1" id="product' +i+ '"></td><td id="productPrice' +i+ '">' +price+ '</td></tr>';
    }
}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();
});

let productOnePrice = parseInt(document.querySelector('#productPrice0').innerHTML);
let productTwoPrice = parseInt(document.querySelector('#productPrice1').innerHTML);
let productThreePrice = parseInt(document.querySelector('#productPrice2').innerHTML);
document.querySelector('#totalPrice').innerHTML = (productOnePrice + productTwoPrice + productThreePrice).toString();

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('increase good laptop quantity')) {
	    document.querySelector('#product0').value++;
	    let productPrice = 399;
	    document.querySelector('#productPrice0').innerHTML = parseInt(document.querySelector('#productPrice0').innerHTML)+productPrice;
	    productOnePrice = parseInt(document.querySelector('#productPrice0').innerHTML);
	    document.querySelector('#totalPrice').innerHTML = (productOnePrice + productTwoPrice + productThreePrice).toString();
	    localStorage.setItem('totalPrice', document.querySelector('#totalPrice').innerHTML);
	    speech.text = 'cart updated';
    }else if (message.includes('increase better laptop quantity')) {
	    document.querySelector('#product1').value++;
	    let productPrice = 499;
	    document.querySelector('#productPrice1').innerHTML = parseInt(document.querySelector('#productPrice1').innerHTML)+productPrice;
	    productTwoPrice = parseInt(document.querySelector('#productPrice1').innerHTML);
	    document.querySelector('#totalPrice').innerHTML = (productOnePrice + productTwoPrice + productThreePrice).toString();
	    localStorage.setItem('totalPrice', document.querySelector('#totalPrice').innerHTML);
	    speech.text = 'cart updated';
    }else if (message.includes('increase best laptop quantity')) {
	    document.querySelector('#product2').value++;
	    let productPrice = 599;
	    document.querySelector('#productPrice2').innerHTML = parseInt(document.querySelector('#productPrice2').innerHTML)+productPrice;
	    productThreePrice = parseInt(document.querySelector('#productPrice2').innerHTML);
	    document.querySelector('#totalPrice').innerHTML = (productOnePrice + productTwoPrice + productThreePrice).toString();
	    localStorage.setItem('totalPrice', document.querySelector('#totalPrice').innerHTML);
	    speech.text = 'cart updated';
    }else {
		speech.text = 'Sorry! I dont understand';
	}

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