function dropEvent(){
    const body = document.body;
    body.addEventListener('dragover',function(event){
        event.preventDefault()
        body.style.border = '1px solid black';
        console.log('file over the body');
    });
    body.addEventListener('dragleave',function(event){
        body.style.backgroundColor = '';
        console.log('file out of the body');
    });
    body.addEventListener('drop',function(event){
        event.preventDefault();
        let file = event.dataTransfer.files[0];
        if (file.type === 'application/pdf') {
            alert(file.name);
            readPDF(file);
        } else {
            alert("Please drop a PDF file.");
        }
    });
}
function readInput(){
    var inputElement = document.getElementById("inputElement");
    const file = inputElement.files[0];
    readPDF(file);

}
function readPDF(file){
    const reader = new FileReader();
    reader.onload = async function(event){
        const buffer = event.target.result;
        const typedArray = new Uint8Array(buffer);
        const pdf = await pdfjsLib.getDocument(typedArray).promise;
        let text = '';
        for (let pageNum = 1; pageNum <= pdf.numPages; pageNum++) {
            const page = await pdf.getPage(pageNum);
            const content = await page.getTextContent();
            content.items.forEach(item => {
                text += item.str + '\n';
            });
        }
        displayText(text);
    }
    reader.readAsArrayBuffer(file);
}
function displayText(text){
    // Split the text into lines
    const lines = text.split('\n');
    
    const holder = document.querySelector('.textHolder');

    // Iterate over each line
    lines.forEach(line => {
        holder.value = holder.value + line;
    });

}
function voice_preload(){
    window.speechSynthesis.cancel();
    window.speechSynthesis.onvoiceschanged = function() {
        var voices = window.speechSynthesis.getVoices();
        console.log(voices); // Log voices to check if they are loaded
        if(voices){
            const holder = document.getElementById('voices_box');
            for(var voice of voices){
                const option = document.createElement('option');
                option.innerText = voice.name;
                option.id = voice.id;
                holder.appendChild(option);
            }
        }
        //log.innerText = 'Web Speech API is supported '+'(voci gasite: '+voices.length+')';
    };
}
function generateAndPlaySpeech() {
    console.log("Citire text");
    const input_holder = document.getElementById('input_holder');
    const voices_holder = document.getElementById('voices_box');
    const selectedOption = voices_holder.options[voices_holder.selectedIndex];

    if (selectedOption) {
        const voice = window.speechSynthesis.getVoices().find(v => v.name === selectedOption.value);

        if (voice) {
            console.log(voice.lang);

            var text = input_holder.value;
            
            var utterance = new SpeechSynthesisUtterance(text);

            utterance.rate = document.getElementById('Uterrance_value').value;
            utterance.pitch = document.getElementById('Pitch_value').value;
            if(voice.lang === 'ro-RO'){
                utterance.lang = 'ro-RO-x-dfc#Dfc-phrase1';
                
            }else{
                utterance.lang = voice.lang; 
            }  
            const autoplay = document.getElementById('autoplay');
            
            if(autoplay.checked){
                const current_pos = get_curent_pos();
                const pages = get_pages();
                
                if(current_pos < pages.length-1){
                    console.log('reading page:',current_pos+1,'/',pages.length);
                    utterance.addEventListener('end',function(){
                        next_page();
                        generateAndPlaySpeech();
                    });
                }else{
                    console.log('reading page:',current_pos+1,'/',pages.length);
                    console.log('pagina finala')
                }
            }else{
              console.log('reading curent text');  
            }
            window.speechSynthesis.speak(utterance);
        } else {
            console.error('Voice not found.');
        }
    } else {
        console.error('No voice selected.');
    }
    
}
function pause_speech(){
    window.speechSynthesis.pause();
    console.log("pauza");
}
function resume_speech(){
    window.speechSynthesis.resume();
    console.log("resumed");
}
function stop_speech(){
    window.speechSynthesis.cancel();
    console.log("stoped");
}