File size: 12,811 Bytes
73566f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import { Component } from '@angular/core';
import { Router } from '@angular/router';
import * as XLSX from 'xlsx';
import { saveAs } from 'file-saver';
// @ts-ignore
import jsPDF from 'jspdf';
// @ts-ignore
import autoTable from 'jspdf-autotable';
import { QuestionDataService } from '../question-data.service';

@Component({
  selector: 'app-question-summary-page',
  templateUrl: './question-summary-page.component.html',
  styleUrls: ['./question-summary-page.component.css']
})
export class QuestionSummaryPageComponent {
  // pagination
  pageSize = 5;
  currentPage = 1;

  // Example investigation/case details (replace/fetch as needed)
  caseDetails = {
    caseId: 'CASE-007',
    officer: 'Ganesh',
    suspect: 'Jeeva',
    date: '2025-10-15',
    verdict: 'Consistent',
    summary: 'The suspect displayed calm emotions overall but showed minor inconsistency in hand gestures. Recommendation: Conduct a short follow-up session.',
    observations: 'During questioning, the suspect showed hesitation when discussing the time of the incident. Eye movement frequency decreased by25% during key questions. Speech tone remained steady, indicating partial honesty. Recommendation: Further questioning advised for financial motive discussion.',
    location: 'Chennai',
    sessionTime: '00:42:18',
    progress: 92,
    status: 'Closed'
  };

  questions = [
    { text: 'Did you visit the location on 12th?', answer: 'Yes, I was there for about 20 minutes.', duration: '00:18', truthProbability: 78, dominantEmotion: 'Nervous 😟', body: 'ajslkdfjsa ldfjlska', bodyScore: 33, voice: 'aksjdfkls jadflk', voiceScore: 23, overallScore: '66%', emotion: 'Calm', videoUrl: '', audioUrl: '', eyeContact: '78%', blinkRate: '12/min', posture: 'Neutral', handMovement: 'Low', legMovement: 'Moderate', microExpressions: '2 detected', stressLevel: 68, confidence: 'Moderate', sentiment: 'Negative (-0.45)', responseDelay: '3.1 sec' },
    { text: 'Were you alone at the scene?', answer: 'No, my friend was with me.', duration: '00:22', truthProbability: 62, dominantEmotion: 'Nervous 😟', body: '', bodyScore: '', voice: '', voiceScore: '', overallScore: '', emotion: 'Nervous', videoUrl: '', audioUrl: '', eyeContact: '65%', blinkRate: '15/min', posture: 'Defensive', handMovement: 'Medium', legMovement: 'Low', microExpressions: '3 detected', stressLevel: 72, confidence: 'Low', sentiment: 'Negative (-0.32)', responseDelay: '2.7 sec' },
    { text: 'Did you know the victim?', answer: 'Yes, we worked together.', duration: '00:15', truthProbability: 85, dominantEmotion: 'Calm 😌', body: '', bodyScore: '', voice: '', voiceScore: '', overallScore: '', emotion: 'Calm', videoUrl: '', audioUrl: '', eyeContact: '82%', blinkRate: '10/min', posture: 'Relaxed', handMovement: 'Low', legMovement: 'Low', microExpressions: '1 detected', stressLevel: 38, confidence: 'High', sentiment: 'Positive (+0.22)', responseDelay: '1.2 sec' },
    { text: 'Did you handle any objects?', answer: 'I picked up a bag to check for ID.', duration: '00:19', truthProbability: 44, dominantEmotion: 'Defensive 🛡️', body: '', bodyScore: '', voice: '', voiceScore: '', overallScore: '', emotion: 'Defensive', videoUrl: '', audioUrl: '', eyeContact: '55%', blinkRate: '18/min', posture: 'Tense', handMovement: 'High', legMovement: 'High', microExpressions: '4 detected', stressLevel: 81, confidence: 'Low', sentiment: 'Negative (-0.61)', responseDelay: '4.0 sec' },
    // Demo questions added so paginator is visible
    { text: 'What time did the incident occur?', answer: 'Around 9 PM.', duration: '00:12', truthProbability: 71, dominantEmotion: 'Calm 😌', body: '', bodyScore: '', voice: '', voiceScore: '', overallScore: '', emotion: 'Calm', videoUrl: '', audioUrl: '', eyeContact: '70%', blinkRate: '11/min', posture: 'Relaxed', handMovement: 'Low', legMovement: 'Low', microExpressions: '1 detected', stressLevel: 40, confidence: 'High', sentiment: 'Neutral (0.00)', responseDelay: '1.0 sec' },
    { text: 'Did anyone else accompany you?', answer: 'No one else was present.', duration: '00:10', truthProbability: 79, dominantEmotion: 'Calm 😌', body: '', bodyScore: '', voice: '', voiceScore: '', overallScore: '', emotion: 'Calm', videoUrl: '', audioUrl: '', eyeContact: '80%', blinkRate: '9/min', posture: 'Neutral', handMovement: 'Low', legMovement: 'Low', microExpressions: '0 detected', stressLevel: 34, confidence: 'High', sentiment: 'Positive (+0.15)', responseDelay: '0.8 sec' }
  ];

  constructor(private router: Router, public questionDataService: QuestionDataService) { }

  // pagination helpers
  get totalPages(): number {
    return Math.max(1, Math.ceil(this.questions.length / this.pageSize));
  }

  get pagedQuestions() {
    const start = (this.currentPage - 1) * this.pageSize;
    return this.questions.slice(start, start + this.pageSize);
  }

  goToPage(page: number) {
    if (page < 1) page = 1;
    if (page > this.totalPages) page = this.totalPages;
    this.currentPage = page;
    window.scrollTo({ top: 0, behavior: 'smooth' });
  }

  prevPage() { this.goToPage(this.currentPage - 1); }
  nextPage() { this.goToPage(this.currentPage + 1); }

  // Helpers required by template
  get totalResults(): number {
    return this.questions.length;
  }

  get resultsStart(): number {
    if (this.totalResults === 0) return 0;
    return (this.currentPage - 1) * this.pageSize + 1;
  }

  get resultsEnd(): number {
    return Math.min(this.currentPage * this.pageSize, this.totalResults);
  }

  changePageSize(eventOrSize: any) {
    let newSize: any = eventOrSize;
    // If event passed from template, extract value safely
    if (eventOrSize && typeof eventOrSize === 'object' && 'target' in eventOrSize) {
      const target = eventOrSize.target as HTMLSelectElement | null;
      newSize = target?.value;
    }
    const size = Number(newSize) || 5;
    this.pageSize = size;
    this.currentPage = 1;
  }

  visiblePages(): (number | string)[] {
    const total = this.totalPages;
    const current = this.currentPage;
    const pages: (number | string)[] = [];
    if (total <= 7) {
      for (let i = 1; i <= total; i++) pages.push(i);
      return pages;
    }
    pages.push(1);
    if (current > 4) pages.push('...');
    const start = Math.max(2, Math.min(current - 1, total - 4));
    const end = Math.min(total - 1, start + 2);
    for (let i = start; i <= end; i++) pages.push(i);
    if (end < total - 1) pages.push('...');
    pages.push(total);
    return pages;
  }

  goBack() {
    this.router.navigate(['/validationpage']);
  }

  navigateHome() {
    // TODO: Implement navigation to home page
  }

  navigateBackToPyDetect() {
    // TODO: Implement navigation to PyDetect investigation page
  }

  downloadExcel() {
    // First header row (group headers)
    const header1 = [
      'S. No', 'Case ID', 'Officer', 'Date',
      'Question', 'Answer', 'Duration', 'Truth Probability', 'Dominant Emotion', 'Emotion',
      'Audio Analysis', '', '', '', '',
      'Video Analysis', '', '', '', '', '',
      'Physical Expression', 'Physical Score', 'Voice Expression', 'Voice Score', 'Overall Score'
    ];
    // Second header row (sub-headers)
    const header2 = [
      '', '', '', '',
      '', '', '', '', '', '',
      'Stress Level', 'Confidence', 'Sentiment', 'Response Delay',
      'Eye Contact', 'Blink Rate', 'Posture', 'Hand Movement', 'Leg Movement', 'Micro Expressions',
      '', '', '', '', '', ''
    ];
    // Data rows for main table, now with S. No and summary columns
    const dataRows = this.questions.map((q, i) => [
      i + 1,
      this.caseDetails.caseId || '',
      this.caseDetails.officer || '',
      this.caseDetails.date || '',
      q.text,
      q.answer,
      q.duration,
      (q.truthProbability !== undefined ? q.truthProbability + '%' : ''),
      q.dominantEmotion || '',
      q.emotion || '',
      q.stressLevel || '',
      q.confidence || '',
      q.sentiment || '',
      q.responseDelay || '',
      q.eyeContact || '',
      q.blinkRate || '',
      q.posture || '',
      q.handMovement || '',
      q.legMovement || '',
      q.microExpressions || '',
      this.getPhysicalExpressionSummary(q),
      this.getPhysicalScore(q),
      this.getVoiceExpressionSummary(q),
      this.getVoiceScore(q),
      this.getOverallScore(q)
    ]);

    // Combine all data for export
    const wsData = [header1, header2, ...dataRows];
    const ws = XLSX.utils.aoa_to_sheet(wsData);

    // Merge cells for group headers (main table only)
    ws['!merges'] = [
      { s: { r: 0, c: 0 }, e: { r: 1, c: 0 } }, // S. No
      { s: { r: 0, c: 1 }, e: { r: 1, c: 1 } }, // Case ID
      { s: { r: 0, c: 2 }, e: { r: 1, c: 2 } }, // Officer
      { s: { r: 0, c: 3 }, e: { r: 1, c: 3 } }, // Date
      { s: { r: 0, c: 4 }, e: { r: 1, c: 4 } }, // Question
      { s: { r: 0, c: 5 }, e: { r: 1, c: 5 } }, // Answer
      { s: { r: 0, c: 6 }, e: { r: 1, c: 6 } }, // Duration
      { s: { r: 0, c: 7 }, e: { r: 1, c: 7 } }, // Truth Probability
      { s: { r: 0, c: 8 }, e: { r: 1, c: 8 } }, // Dominant Emotion
      { s: { r: 0, c: 9 }, e: { r: 1, c: 9 } }, // Emotion
      { s: { r: 0, c: 10 }, e: { r: 0, c: 14 } }, // Audio Analysis group
      { s: { r: 0, c: 15 }, e: { r: 0, c: 20 } }, // Video Analysis group
      { s: { r: 0, c: 21 }, e: { r: 1, c: 21 } }, // Physical Expression
      { s: { r: 0, c: 22 }, e: { r: 1, c: 22 } }, // Physical Score
      { s: { r: 0, c: 23 }, e: { r: 1, c: 23 } }, // Voice Expression
      { s: { r: 0, c: 24 }, e: { r: 1, c: 24 } }, // Voice Score
      { s: { r: 0, c: 25 }, e: { r: 1, c: 25 } } // Overall Score
    ];

    const wb = XLSX.utils.book_new();
    XLSX.utils.book_append_sheet(wb, ws, 'Questions');
    const wbout = XLSX.write(wb, { bookType: 'xlsx', type: 'array' });
    saveAs(new Blob([wbout], { type: 'application/octet-stream' }), 'questions-and-answers.xlsx');
  }

  getPhysicalExpressionSummary(q: any): string {
    let parts = [];
    if (q.posture) parts.push(q.posture);
    if (q.handMovement) parts.push(q.handMovement + ' hand');
    if (q.legMovement) parts.push(q.legMovement + ' leg');
    if (q.microExpressions) parts.push(q.microExpressions);
    return parts.length ? parts.join(', ') : '—';
  }
  getPhysicalScore(q: any): string {
    let scores = [];
    if (typeof q.handMovement === 'number') scores.push(q.handMovement);
    if (typeof q.legMovement === 'number') scores.push(q.legMovement);
    const match = (q.microExpressions || '').match(/(\d+)/);
    if (match) scores.push(Number(match[1]));
    if (scores.length === 0) return '—';
    return Math.round(scores.reduce((a, b) => a + b, 0) / scores.length) + '%';
  }
  getVoiceExpressionSummary(q: any): string {
    let parts = [];
    if (q.stressLevel !== undefined) parts.push('Stress ' + q.stressLevel);
    if (q.confidence) parts.push('Conf ' + q.confidence);
    if (q.sentiment) parts.push('Sent ' + this.getSentimentPercent(q.sentiment));
    if (q.responseDelay) parts.push('Delay ' + q.responseDelay);
    return parts.length ? parts.join(', ') : '—';
  }
  getVoiceScore(q: any): string {
    let scores = [];
    if (typeof q.stressLevel === 'number') scores.push(q.stressLevel);
    if (typeof q.confidence === 'number') scores.push(q.confidence);
    else if (q.confidence === 'High') scores.push(90);
    else if (q.confidence === 'Moderate') scores.push(60);
    else if (q.confidence === 'Low') scores.push(30);
    if (scores.length === 0) return '—';
    return Math.round(scores.reduce((a, b) => a + b, 0) / scores.length) + '%';
  }
  getOverallScore(q: any): string {
    // Example: average of physical and voice scores
    const phys = this.getPhysicalScore(q);
    const voice = this.getVoiceScore(q);
    const physNum = parseInt(phys);
    const voiceNum = parseInt(voice);
    if (isNaN(physNum) && isNaN(voiceNum)) return '—';
    if (isNaN(physNum)) return voice;
    if (isNaN(voiceNum)) return phys;
    return Math.round((physNum + voiceNum) / 2) + '%';
  }
  getSentimentPercent(sentiment: string): string {
    if (!sentiment) return '';
    const match = sentiment.match(/([+-]?\d*\.?\d+)/);
    if (match) {
      const value = parseFloat(match[1]);
      const percent = Math.round(value * 100);
      return (percent > 0 ? '+' : '') + percent + '%';
    }
    return sentiment;
  }

  viewDetails(index: number) {
    // Set live data in service before navigation
    const normalized = this.questions.map((q: any) => ({ ...q, question: q.question ?? q.text ?? '' }));
    this.questionDataService.setQuestions(normalized);
    this.questionDataService.setCaseDetails(this.caseDetails);
    const absoluteIndex = (this.currentPage - 1) * this.pageSize + index;
    this.router.navigate(['/view-details', absoluteIndex]);
  }
}