`;
if (st_avgEfficiency > 0 && mt_avgEfficiency > 0 && st_avgEfficiency > mt_avgEfficiency) {
html += `
Insight:
Your average perceived efficiency is higher during Single-Task Focus sessions (${st_avgEfficiency.toFixed(1)}/5) compared to Multi-Tasking sessions (${mt_avgEfficiency.toFixed(1)}/5). Consider dedicating more blocks to single-tasking for critical work.
`;
} else if (st_avgEfficiency > 0 && mt_avgEfficiency > 0 && mt_avgEfficiency > st_avgEfficiency) {
html += `
Insight:
Your average perceived efficiency is higher during Multi-Tasking sessions (${mt_avgEfficiency.toFixed(1)}/5) compared to Single-Task Focus sessions (${st_avgEfficiency.toFixed(1)}/5). This is unusual but could reflect the nature of tasks logged. Ensure your ratings are consistent!
`;
}
if(mt_avgSwitchesPerHour > 5 && multiTaskSessions.length > 0) { // Example threshold
html += `
Insight:
You average ${mt_avgSwitchesPerHour.toFixed(1)} context switches per hour during multi-tasking. Frequent switching can reduce deep focus. Try techniques to batch similar small tasks or minimize interruptions.
`;
}
resultsDiv.innerHTML = html;
}
function mea_clearAllLoggedData() {
if (confirm("Are you sure you want to clear ALL logged sessions? This cannot be undone.")) {
if (mea_currentSession && mea_currentSession.timerInterval) clearInterval(mea_currentSession.timerInterval);
mea_currentSession = null;
mea_loggedSessions = []; mea_logIdCounter = 0;
mea_editLogId = null;
document.getElementById('mea-post-session-rating').style.display = 'none';
document.getElementById('mea-live-session-display').style.display = 'none';
document.getElementById('mea-session-start-btn').disabled = false;
document.getElementById('mea-session-stop-btn').disabled = true;
mea_applyFiltersAndAnalyze();
}
}
function mea_downloadPDF() {
if (!tpc_jsPDF_loaded_MEA || !tpc_autoTable_loaded_MEA) { alert("MEA Error: PDF libraries not loaded."); return; }
const fromDateStr = document.getElementById('mea-filter-from-date').value;
const toDateStr = document.getElementById('mea-filter-to-date').value;
const typeFilter = document.getElementById('mea-filter-session-type').value;
const fromDate = fromDateStr ? new Date(fromDateStr) : null;
const toDate = toDateStr ? new Date(toDateStr) : null;
const dataForPdf = mea_loggedSessions.filter(s => {
if (!s.startTimeObj) return false;
if (fromDate && s.startTimeObj < fromDate) return false;
if (toDate && s.startTimeObj > toDate) return false;
if (typeFilter && s.sessionType !== typeFilter) return false;
return true;
});
if (dataForPdf.length === 0) { alert("No sessions (matching current filters) to export."); return; }
const doc = new TPC_jsPDF_MEA();
const primaryColor = getComputedStyle(document.documentElement).getPropertyValue('--primary-color').trim();
const darkColor = getComputedStyle(document.documentElement).getPropertyValue('--dark-color').trim();
let finalY = 15;
const reportDateStr = new Date().toLocaleDateString();
doc.setFontSize(18); doc.setTextColor(primaryColor);
doc.text("Multitasking Efficiency Analysis Report", doc.internal.pageSize.getWidth() / 2, finalY, { align: 'center' });
finalY += 8; doc.setFontSize(10); doc.setTextColor(darkColor);
doc.text(`Report Date: ${reportDateStr} | Period: ${fromDate ? fromDate.toLocaleDateString() : 'All'} to ${toDate ? toDate.toLocaleDateString() : 'All'}`, doc.internal.pageSize.getWidth() / 2, finalY, { align: 'center'});
if(typeFilter) { finalY += 5; doc.text(`Session Type Filter: ${typeFilter}`, doc.internal.pageSize.getWidth() / 2, finalY, {align:'center'}); }
finalY += 10;
// Analysis Summary for PDF
const singleTaskSessions = dataForPdf.filter(s => s.sessionType === 'Single Task Focus' && s.durationMinutes !== undefined);
const multiTaskSessions = dataForPdf.filter(s => s.sessionType === 'Multi-Tasking' && s.durationMinutes !== undefined);
let st_totalTime = 0, st_totalEfficiencyScore = 0, st_count = 0;
singleTaskSessions.forEach(s => { st_totalTime += s.durationMinutes; if(s.perceivedEfficiency) {st_totalEfficiencyScore += s.perceivedEfficiency; st_count++;} });
const st_avgEfficiency = st_count > 0 ? (st_totalEfficiencyScore / st_count).toFixed(1) : 'N/A';
let mt_totalTime = 0, mt_totalEfficiencyScore = 0, mt_count_eff = 0, totalSwitches = 0;
multiTaskSessions.forEach(s => { mt_totalTime += s.durationMinutes; if(s.perceivedEfficiency){ mt_totalEfficiencyScore += s.perceivedEfficiency; mt_count_eff++;} totalSwitches += s.contextSwitchCount;});
const mt_avgEfficiency = mt_count_eff > 0 ? (mt_totalEfficiencyScore / mt_count_eff).toFixed(1) : 'N/A';
const mt_avgSwitchesPerHour = mt_totalTime > 0 ? (totalSwitches / (mt_totalTime / 60)).toFixed(1) : '0.0';
doc.setFontSize(12); doc.setTextColor(primaryColor); doc.text("Analysis Summary:", 14, finalY); finalY += 6;
doc.setFontSize(9); doc.setTextColor(darkColor);
doc.text(`Single-Tasking Sessions: ${singleTaskSessions.length} | Total Time: ${mea_formatDurationForDisplay(st_totalTime)} | Avg. Efficiency: ${st_avgEfficiency}/5`, 14, finalY); finalY +=5;
doc.text(`Multi-Tasking Sessions: ${multiTaskSessions.length} | Total Time: ${mea_formatDurationForDisplay(mt_totalTime)} | Avg. Efficiency: ${mt_avgEfficiency}/5 | Avg. Switches/Hour: ${mt_avgSwitchesPerHour}`, 14, finalY); finalY+=7;
doc.setFontSize(12); doc.setTextColor(primaryColor); doc.text("Detailed Session Log (Filtered):", 14, finalY); finalY += 6;
const tableBody = dataForPdf.map(s => [
s.startTimeObj.toLocaleString(), s.endTimeObj ? s.endTimeObj.toLocaleString() : 'Active',
s.durationMinutes !== undefined ? mea_formatDurationForDisplay(s.durationMinutes) : '-',
s.sessionType, s.taskName,
s.sessionType === 'Multi-Tasking' ? s.contextSwitchCount : 'N/A',
s.perceivedEfficiency !== undefined ? s.perceivedEfficiency + '/5' : 'N/A'
]);
doc.autoTable({
startY: finalY,
head: [['Start', 'End', 'Duration', 'Type', 'Primary Task', 'Switches', 'Efficiency']],
body: tableBody, theme: 'grid',
headStyles: { fillColor: primaryColor, textColor: '#ffffff', fontSize: 9 },
styles: { fontSize: 8, cellPadding: 1.5, overflow: 'linebreak' }
});
doc.save(`Multitasking_Efficiency_Report_${new Date().toISOString().slice(0,10)}.pdf`);
}