<!DOCTYPE html>
<html lang="pt-BR">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Conversor de Texto para SRT Aprimorado</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 600px;
            margin: 0 auto;
            padding: 20px;
            display: flex;
            flex-direction: column;
            min-height: 100vh;
        }
        textarea {
            width: 100%;
            height: 200px;
        }
        #resultado {
            white-space: pre-wrap;
            background-color: #f0f0f0;
            padding: 10px;
            margin-top: 20px;
        }
        .botoes {
            display: flex;
            justify-content: space-between;
            margin-top: 10px;
        }
        #downloadBtn, #limparBtn {
            display: none;
        }
        footer {
            margin-top: auto;
            text-align: center;
            padding: 10px;
            background-color: #f0f0f0;
            font-size: 0.9em;
        }
    </style>
</head>
<body>
    <h1>Conversor de Texto para SRT Aprimorado</h1>
    <form id="conversorForm">
        <label for="textoInput">Cole seu texto aqui:</label><br>
        <textarea id="textoInput" required></textarea><br>
        <button type="submit">Converter para SRT</button>
    </form>
    <div class="botoes">
        <button id="downloadBtn">Download SRT</button>
        <button type="button" id="limparBtn">Limpar Campo</button>
    </div>
    <div id="resultado"></div>

    <footer>
        Desenvolvido por WEBDARK
    </footer>

    <script>
        (function() {
            const CARACTERES_POR_BLOCO = 500;
            const PALAVRAS_MIN_BLOCO = 80;
            const PALAVRAS_MAX_BLOCO = 100;
            const DURACAO_BLOCO = 30;
            const INTERVALO_ENTRE_BLOCOS = 10; // Mudança feita aqui

            function converterParaSRT(texto) {
                let srt = '';
                let contador = 1;
                let tempoAcumulado = 0;
                let palavras = texto.split(/\s+/);
                let blocoAtual = '';
                let palavrasNoBloco = 0;

                for (let palavra of palavras) {
                    if (blocoAtual.length + palavra.length <= CARACTERES_POR_BLOCO && palavrasNoBloco < PALAVRAS_MAX_BLOCO) {
                        blocoAtual += palavra + ' ';
                        palavrasNoBloco++;
                    } else {
                        let ultimoPontoFinal = blocoAtual.lastIndexOf('.');
                        if (ultimoPontoFinal !== -1 && ultimoPontoFinal !== blocoAtual.length - 1) {
                            let resto = blocoAtual.substring(ultimoPontoFinal + 1);
                            blocoAtual = blocoAtual.substring(0, ultimoPontoFinal + 1);
                            srt += formatarBlocoSRT(contador, tempoAcumulado, blocoAtual);
                            contador++;
                            tempoAcumulado += DURACAO_BLOCO + INTERVALO_ENTRE_BLOCOS;
                            blocoAtual = resto + palavra + ' ';
                            palavrasNoBloco = resto.split(/\s+/).length + 1;
                        } else {
                            srt += formatarBlocoSRT(contador, tempoAcumulado, blocoAtual);
                            contador++;
                            tempoAcumulado += DURACAO_BLOCO + INTERVALO_ENTRE_BLOCOS;
                            blocoAtual = palavra + ' ';
                            palavrasNoBloco = 1;
                        }
                    }
                }

                if (blocoAtual) {
                    srt += formatarBlocoSRT(contador, tempoAcumulado, blocoAtual);
                }

                return srt.trim();
            }

            function formatarBlocoSRT(contador, tempoInicio, texto) {
                const tempoFim = tempoInicio + DURACAO_BLOCO;
                return `${contador}\n${formatarTempo(tempoInicio)} --> ${formatarTempo(tempoFim)}\n${texto.trim()}\n\n`;
            }

            function formatarTempo(segundos) {
                const horas = Math.floor(segundos / 3600);
                const minutos = Math.floor((segundos % 3600) / 60);
                const segsRestantes = Math.floor(segundos % 60);
                const milissegundos = Math.floor((segundos % 1) * 1000);

                return `${pad(horas)}:${pad(minutos)}:${pad(segsRestantes)},${pad(milissegundos, 3)}`;
            }

            function pad(numero, tamanho = 2) {
                return numero.toString().padStart(tamanho, '0');
            }

            function downloadSRT(conteudo) {
                const blob = new Blob([conteudo], { type: 'text/plain' });
                const url = URL.createObjectURL(blob);
                const a = document.createElement('a');
                a.href = url;
                a.download = 'legendas.srt';
                document.body.appendChild(a);
                a.click();
                document.body.removeChild(a);
                URL.revokeObjectURL(url);
            }

            document.getElementById('conversorForm').addEventListener('submit', function(e) {
                e.preventDefault();
                const texto = document.getElementById('textoInput').value;
                const srt = converterParaSRT(texto);
                document.getElementById('resultado').textContent = srt;
                document.getElementById('downloadBtn').style.display = 'block';
                document.getElementById('limparBtn').style.display = 'block';
            });

            document.getElementById('downloadBtn').addEventListener('click', function() {
                const srt = document.getElementById('resultado').textContent;
                downloadSRT(srt);
            });

            document.getElementById('limparBtn').addEventListener('click', function() {
                document.getElementById('textoInput').value = '';
                document.getElementById('resultado').textContent = '';
                document.getElementById('downloadBtn').style.display = 'none';
                document.getElementById('limparBtn').style.display = 'none';
            });

            document.oncontextmenu = function() { return false; };
            document.onkeydown = function(e) {
                if (e.keyCode == 123 || (e.ctrlKey && e.shiftKey && e.keyCode == 'I'.charCodeAt(0))) {
                    return false;
                }
            };
        })();
    </script>
</body>
</html>