File size: 39,186 Bytes
6f093ab
463dbc3
 
 
24822a7
73566f6
463dbc3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24822a7
463dbc3
 
 
 
 
24822a7
463dbc3
 
 
 
73566f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24822a7
 
463dbc3
73566f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
463dbc3
 
 
 
 
73566f6
463dbc3
 
 
 
 
 
73566f6
 
 
 
 
463dbc3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73566f6
463dbc3
 
 
 
 
 
 
 
 
 
 
 
 
 
73566f6
 
463dbc3
 
73566f6
463dbc3
 
73566f6
463dbc3
 
 
 
73566f6
 
463dbc3
6f093ab
24822a7
 
 
 
 
 
 
6f093ab
24822a7
 
6f093ab
 
 
 
 
 
24822a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
463dbc3
24822a7
463dbc3
 
24822a7
463dbc3
24822a7
463dbc3
 
 
 
24822a7
463dbc3
 
 
 
24822a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
463dbc3
 
 
24822a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73566f6
24822a7
463dbc3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73566f6
463dbc3
73566f6
463dbc3
73566f6
463dbc3
 
 
 
 
73566f6
 
463dbc3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73566f6
463dbc3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73566f6
463dbc3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73566f6
 
463dbc3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73566f6
 
463dbc3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24822a7
463dbc3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73566f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
463dbc3
 
73566f6
 
 
 
 
463dbc3
 
73566f6
463dbc3
 
 
 
73566f6
 
 
 
 
 
 
463dbc3
 
73566f6
463dbc3
 
 
73566f6
 
463dbc3
73566f6
 
 
463dbc3
 
6f093ab
73566f6
 
 
 
24822a7
 
73566f6
 
 
 
 
 
 
 
 
 
 
6f093ab
73566f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24822a7
73566f6
 
6f093ab
24822a7
73566f6
 
 
 
 
 
 
 
 
24822a7
73566f6
 
 
 
 
 
 
 
6f093ab
73566f6
 
 
 
 
 
 
 
6f093ab
73566f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6f093ab
 
73566f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24822a7
73566f6
 
 
24822a7
73566f6
 
 
 
 
 
 
 
 
 
6f093ab
73566f6
24822a7
73566f6
463dbc3
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
import { Component, OnDestroy, OnInit, signal, ViewChild, ElementRef, ChangeDetectorRef } from '@angular/core';
import { CommonModule } from '@angular/common';
import { Router, NavigationStart } from '@angular/router';
import { Subscription } from 'rxjs';
import { FormsModule } from '@angular/forms';
import { PyDetectService } from '../services/pydetect.service';

declare global {
  interface Window {
    webkitSpeechRecognition?: any;
    SpeechRecognition?: any;
  }
}

type QAResult = {
  question: string;
  transcript: string;
  language: string;
  avgPitchHz: number | null;
  avgVolume: number | null;   // 0..1 (rough RMS)
  audioUrl: string;
  startedAt: number;
  endedAt: number;
  skipped?: boolean;
};

@Component({
  standalone: true,
  selector: 'app-py-detect',
  imports: [CommonModule, FormsModule],
  templateUrl: './py-detect.component.html',
  styleUrls: ['./py-detect.component.css']
})
export class PyDetectComponent implements OnInit, OnDestroy {
  // Store body language explanation for UI
  public bodyLanguageExplanation: string | null = null;
  public bodyLanguageMeaning: string | null = null;

  // Fetch explanation for a body language cue from backend
  public fetchBodyLanguageExplanation(cue: string) {
    this.bodyLanguageExplanation = null;
    this.bodyLanguageMeaning = null;
    this.pyDetectService.bodyLanguageExplain(cue).subscribe({
      next: (resp) => {
        if (resp?.explanation) {
          this.bodyLanguageExplanation = resp.explanation;
        }
        if (resp?.meaning) {
          this.bodyLanguageMeaning = resp.meaning;
        }
        console.log('[PyDetect] Body Language:', {
          meaning: resp?.meaning,
          explanation: resp?.explanation
        });
      },
      error: () => {
        this.bodyLanguageExplanation = 'No explanation available.';
        this.bodyLanguageMeaning = null;
        console.warn('[PyDetect] No body language explanation available.');
      }
    });
  }
  // FER emotion result for UI display
  public ferEmotion: string | null = null;
  // Face detection score for UI display
  public faceDetectionScore: number | null = null;
  // --- Patch: Add missing properties for template and logic ---
  public currentQuestionIndex: number = -1;
  public totalQuestions: number = 0;
  public currentQuestionText: string = '';
  public evidenceSummary: string = '';
  // Store the truth score for the last submitted answer
  public truthScore: number | null = null;
  // Timing & frame streaming additions
  public questionWindowStartAt: number | null = null;
  public answerStartAt: number | null = null;
  public answerEndAt: number | null = null;
  public answerMode: 'voice' | 'text' | 'mixed' = 'text';
  private frameIntervalId: any;
  private frameStreamingActive: boolean = false;
  public involvementScore: number | null = null;
  public involvementCues: string[] = [];
  public dominantInvestigativeExpression: string | null = null;
  public behaviorTagDistribution: Record<string, number> | null = null;
  public guidanceCommand: string | null = null;

  // --- Patch: Add missing stub methods for template bindings ---
  public async speakQuestion(question: string) {
    // Use TTS to speak the question (stub)
    await this.speak(question);
  }

  public startRecognitionWithRecording(index: number) {
    // Stub for starting recognition with recording
    // You may want to start voice recording and speech recognition here
  }

  public async navigateToValidationPage() {
    // Stop video recording and release camera
    this.stopVideoRecording();
    if (this.videoStream) {
      this.videoStream.getTracks().forEach(t => t.stop());
      this.videoStream = undefined;
    }
    this.isRecording = false;
    // Wait for the video to finish processing if needed
    await this.sleep(500); // Give time for onstop to fire and recordedVideoUrl to be set
    // Automatically download the recorded video if available
    if (this.recordedVideoUrl) {
      const anchor = document.createElement('a');
      anchor.href = this.recordedVideoUrl;
      anchor.download = 'investigation-video.webm';
      anchor.style.display = 'none';
      document.body.appendChild(anchor);
      anchor.click();
      setTimeout(() => {
        document.body.removeChild(anchor);
        // Optionally revoke the object URL after download
        // URL.revokeObjectURL(this.recordedVideoUrl);
      }, 100);
    }
    // Then navigate to validation page
    this.router.navigate(['/validationpage']);
  }

  public uploadDocument() {
    // Stub for document upload logic
    // You may want to handle file upload here
  }

  public onEvidenceFileSelect(event: any, type: string) {
    // Stub for evidence file selection logic
    // You may want to process selected files here
  }
  // Manual answer submission for testing
  public submitTextAnswer() {
    if (!this.textAnswer || !this.sessionId || this.currentQuestionIndex < 0 || !this.questions[this.currentQuestionIndex]) {
      this.infoText = 'Please enter an answer and ensure a question is active.';
      return;
    }
    // Call backend to submit response
    this.pyDetectService.submitResponse(
      this.sessionId,
      this.textAnswer,
      this.questions[this.currentQuestionIndex]
    ).subscribe({
      next: async (res) => {
        // Extract truth score if present
        this.truthScore = (res && (res.truth_score || res.score)) ? Number(res.truth_score || res.score) : null;
        this.infoText = 'Answer submitted.' + (this.truthScore !== null ? ` Truth Score: ${this.truthScore}` : '');
        this.textAnswer = '';
        // Fetch body language explanation for the first involvement cue
        if (this.involvementCues.length) {
          this.fetchBodyLanguageExplanation(this.involvementCues[0]);
        }
        const response = await this.pyDetectService.askQuestion(
          this.sessionId,
          this.crimeType,
          this.briefDescription
        ).toPromise();
        if (response && response.question) {
          this.questions.push(response.question);
          this.currentQuestionIndex++;
          this.cdr.detectChanges();
          await this.speakQuestion(response.question);
        } else {
          this.infoText = 'No more questions.';
        }
      },
      error: (err) => {
        this.infoText = 'Error submitting answer.';
      }
    });
  }
  public showDetailsPanel: boolean = false;
  public metadata: any = null;

  // Backend-driven session and investigation state
  sessionId: string = '';
  caseData: any = null;
  briefDescription: string = '';
  isSessionStarted: boolean = false;
  isLoading: boolean = false;
  currentQuestion: string = '';
  textAnswer: string = '';
  lastAnalysisResult: any = null;
  questionCount: number = 0;
  currentInvestigationStage: string = 'Initial Investigation';
  questionNumber: number = 1;
  cameraActive: boolean = false;
  voiceRecordingActive: boolean = false;
  investigationActive: boolean = false;
  investigationStarted: boolean = false;
  caseSummary: any = null;
  processingResponse: boolean = false;
  videoStatus: string = 'Camera Ready';
  ttsEnabled: boolean = false;
  isListening: boolean = false;
  speechRecognition: any = null;
          // Combined answer submission: prefer text box, fallback to transcript
          public submitCombinedAnswer() {
            // Accept answer from text box or voice transcript
            let answerText = (this.textAnswer && this.textAnswer.trim()) ? this.textAnswer.trim() : (this.transcriptSoFar && this.transcriptSoFar.trim()) ? this.transcriptSoFar.trim() : '';
            if (!answerText || !this.sessionId || !this.questions[this.currentQuestionIndex]) {
              this.infoText = 'Please provide your answer before submitting.';
              return;
            }
            this.stopAudioRecording();
            this.infoText = 'Submitting answer...';
            this.textAnswer = '';
            this.transcriptSoFar = '';
            const endTs = Date.now();
            this.answerEndAt = endTs;
            if (!this.answerStartAt) this.answerStartAt = this.questionWindowStartAt || endTs;
            const durationMs = this.answerEndAt - this.answerStartAt;
            this.stopFrameStreaming();
            this.pyDetectService.submitResponse(
              this.sessionId,
              answerText,
              this.questions[this.currentQuestionIndex],
              {
                answer_start_at: this.answerStartAt,
                answer_end_at: this.answerEndAt,
                duration_ms: durationMs,
                mode: this.answerMode
              }
            ).subscribe({
              next: async (res) => {
                // Extract truth score if present
                this.truthScore = (res && (res.truth_score || res.score)) ? Number(res.truth_score || res.score) : null;
                this.infoText = 'Answer submitted.' + (this.truthScore !== null ? ` Truth Score: ${this.truthScore}` : '');
                // Pull involvement metrics
                this.fetchLatestInvolvement();
                // Fetch next question from backend
                const response = await this.pyDetectService.askQuestion(
                  this.sessionId,
                  this.crimeType,
                  this.briefDescription
                ).toPromise();
                if (response && response.question) {
                  this.questions.push(response.question);
                  this.currentQuestionIndex++;
                  this.questionNumber = this.currentQuestionIndex + 1;
                  this.cdr.detectChanges();
                  await this.startCamera();
                  await this.startVideoRecording();
                  await this.speakQuestion(response.question);
                  // Restart window for next question
                  this.startQuestionWindow();
                  // Reset answer timing
                  this.answerStartAt = null;
                  this.answerEndAt = null;
                  this.answerMode = 'text';
                } else {
                  this.infoText = 'No more questions.';
                  this.showSummary = true;
                }
              },
              error: () => {
                this.infoText = 'Error submitting answer.';
              }
            });
          }
  public stopAudioRecording() {
    if (this.mediaRecorder && this.mediaRecorder.state !== 'inactive') {
      this.mediaRecorder.stop();
      // Show the transcribed answer in the text box
      this.textAnswer = this.transcriptSoFar;
      this.infoText = 'Voice recording stopped.';
      // Stop speech recognition when recording stops
      if (this.recognition) {
        try { this.recognition.stop(); } catch {}
      }
    }
  }
  speechSynthesis: any = null;
  voiceSupported: boolean = false;
  microphoneSupported: boolean = false;
  microphonePermissionDenied: boolean = false;
  permissionStatus: string = 'unknown';

  // ---- TTS active flag ----
  private isActive = false;

  // ---- Q/A data ----
  // log: QAResult[] = [];
  log: QAResult[] = [];


  // ---- Constructor with Router Injection ----
  private routerSubscription?: Subscription;

  constructor(
    private router: Router,
    private cdr: ChangeDetectorRef,
    private pyDetectService: PyDetectService
  ) {
    // Cancel TTS on any navigation away
    this.routerSubscription = this.router.events.subscribe(event => {
      if (event instanceof NavigationStart) {
        if (window.speechSynthesis) {
          window.speechSynthesis.cancel();
        }
      }
    });
  }

  // ---- Recording/analysis handles ----
  private mediaStream?: MediaStream;
  private mediaRecorder?: MediaRecorder;
  private audioChunks: Blob[] = [];

  private audioCtx?: AudioContext;
  private analyser?: AnalyserNode;
  private sourceNode?: MediaStreamAudioSourceNode;

  private pitchSamples: number[] = [];
  private volumeSamples: number[] = [];
  private analyserBuffer: Float32Array = new Float32Array(2048);
  private analyserTimer?: any;

  // ---- Speech Recognition ----
  private recognition?: any;  // webkitSpeechRecognition
  private transcriptSoFar = '';
  private detectedLang = 'auto';

  // ---- Settings ----
  private maxAnswerMs = 10_000; // per answer recording window
  private silenceTimeout?: any;  // Declare silenceTimeout here

  private analyserWindowMs = 100;  // Declare analyserWindowMs property

  // Example question source (replace with API call when ready)
  // Remove legacy seedQuestions
  public questions: string[] = [];

  // Button state signals
  // Remove legacy button state signals

  // Add missing public methods and properties for template binding
  // videoStatus already declared above for backend workflow
  public videoStream?: MediaStream;
  @ViewChild('videoElement', { static: false }) videoElement?: ElementRef<HTMLVideoElement>;
  public videoChunks: Blob[] = [];
  public videoAnswers: Blob[] = [];
  public videoRecorder?: MediaRecorder;
  public recordedVideoUrl: string = '';

  // UI properties for template
  caseId: string = '';
  crimeType: string = '';
  dateTime: string = '';
  location: string = '';
  suspectName: string = '';
  statusText: string = '';
  investigationOfficer: string = '';
  progress: number = 0;
  progressStage: string = '';
  sessionTime: string = '';
  isRecording: boolean = false;
  isProcessing: boolean = false;
  transcriptLines: string[] = [];
  showSummary: boolean = false;
  summaryData: { question: string; answer: string; duration: number }[] = [];

  // Evidence panel state and placeholder data
  public showEvidencePanel: boolean = false;
  public showSummaryPanel: boolean = false;
  public uploadedDocuments: string[] = ['Report.pdf', 'Statement.docx'];
  public capturedPhotos: string[] = ['Photo1.jpg', 'Photo2.png'];
  public previousRecordings: string[] = ['Recording1.webm', 'Recording2.mp3'];

  // Info text for HUD below question
  public floatingInfoText: string | null = null;
  public infoText: string | null = null;

  // Elapsed and remaining time for recording status
  public elapsedTime: string = '00:00';
  public remainingTime: string = '00:00';
  private recordingTimerInterval: any;

  // Repeat current question using TTS
  public async repeatQuestion() {
    await this.speakQuestion(this.currentQuestionText);
    await this.sleep(1000);
    this.infoText = 'Recording in progress – Listening to answer.';
  }

  // Navigate back to the homepage

  navigateHome() {
    if (window.speechSynthesis) {
      window.speechSynthesis.cancel();
    }
    this.router.navigate(['/']);
  }

  goToInfoPage() {
    if (window.speechSynthesis) {
      window.speechSynthesis.cancel();
    }
    this.router.navigate(['/infopage']);
  }

  navigateBackToCaseDetails() {
    if (window.speechSynthesis) {
      window.speechSynthesis.cancel();
    }
    // Stop any <audio> elements
    const audioElems = document.querySelectorAll('audio');
    audioElems.forEach((audioElem) => {
      (audioElem as HTMLAudioElement).pause();
      (audioElem as HTMLAudioElement).currentTime = 0;
    });
    // Stop custom Audio object if present
    if ((window as any).pyDetectAudioObj && typeof (window as any).pyDetectAudioObj.pause === 'function') {
      (window as any).pyDetectAudioObj.pause();
      (window as any).pyDetectAudioObj.currentTime = 0;
    }
    // Stop any media streams
    if (this.mediaStream) {
      this.mediaStream.getTracks().forEach(t => t.stop());
      this.mediaStream = undefined;
    }
    this.router.navigate(['/case-details']);
  }


  // ======== Lifecycle ========
  ngOnInit(): void {
    this.isActive = true;
    // Do NOT set isRecording = true here; only set when question starts

    let metadata: any = null;
    const nav = this.router.getCurrentNavigation();
    if (nav?.extras?.state && nav.extras.state['metadata']) {
      metadata = nav.extras.state['metadata'];
      localStorage.setItem('pyDetectMetadata', JSON.stringify(metadata)); // Update localStorage on navigation
      console.log('Received metadata from navigation:', metadata);
    } else {
      // Fallback: read from localStorage
      const stored = localStorage.getItem('pyDetectMetadata');
      if (stored) {
        metadata = JSON.parse(stored);
        console.log('Loaded metadata from localStorage:', metadata);
      } else {
        console.warn('No metadata found in navigation state or localStorage');
      }
    }
    if (metadata) {
      this.caseId = metadata.caseId || '';
      this.crimeType = metadata.crimeType || '';
      this.dateTime = metadata.dateTime || '';
      this.location = metadata.location || '';
      this.suspectName = metadata.suspectName || '';
      this.statusText = metadata.status || '';
      this.investigationOfficer = metadata.investigationOfficer || '';
      this.progress = metadata.progress || 0;
      this.progressStage = metadata.progressStage || '';
      this.sessionTime = metadata.sessionTime || '';
      this.briefDescription = metadata.briefDescription || '';
    }
  }

  ngOnDestroy(): void {
    this.isActive = false;
    if (this.routerSubscription) {
      this.routerSubscription.unsubscribe();
    }
    this.cleanupAll();
    this.stopVideoRecording();
    this.videoStatus = '';
    if (this.videoStream) {
      this.videoStream.getTracks().forEach(t => t.stop());
      this.videoStream = undefined;
    }
    this.videoRecorder = undefined;
    if (window.speechSynthesis) {
      window.speechSynthesis.cancel(); // Stop any TTS audio
    }
  }

  // ======== Main flow ========
  // Legacy start method removed. Use backend-driven workflow only.

  // Legacy stopAll method removed. Use backend-driven workflow only.

  // Legacy resume method removed. Use backend-driven workflow only.

  // ======== Question source ========
  private async fetchNextQuestion(): Promise<string> {
    // Replace this with HTTP call to your backend if needed.
    // Example: const { question } = await this.http.get<{question:string}>('/api/next-question').toPromise();
  // Legacy fetchNextQuestion logic removed. Use backend-driven workflow only.
  return '';
  }

  // ======== TTS (question playback) ========
  private speak(text: string): Promise<void> {
    return new Promise<void>((resolve) => {
      if (!this.isActive) return resolve(); // Only play TTS if component is active
      const synth = window.speechSynthesis;
      if (!synth) return resolve(); // gracefully continue without TTS

      const utter = new SpeechSynthesisUtterance(text);
      // Optional voice selection: pick an Indian English voice if available
      const prefer = ['en-IN', 'en-GB', 'en-US'];
      const voices = synth.getVoices();
      const v = voices.find(v =>
        prefer.includes(v.lang) || v.lang.toLowerCase().startsWith('en'));
      if (v) utter.voice = v;

      utter.rate = 1.0;
      utter.pitch = 1.0;
      utter.onend = () => resolve();
      utter.onerror = () => resolve(); // do not block flow
      synth.cancel(); // ensure clean queue
      synth.speak(utter);
    });
  }

  // ======== Recording + Recognition + Analysis ========
  private async captureAnswerWithAnalysis(ms: number): Promise<{
    audioUrl: string; avgPitchHz: number | null; avgVolume: number | null; transcript: string; language: string;
  }> {
    // reset buffers
    this.audioChunks = [];
    this.pitchSamples = [];
    this.volumeSamples = [];
    this.transcriptSoFar = '';
    this.detectedLang = 'auto';

    // 1) mic stream
    this.mediaStream = await navigator.mediaDevices.getUserMedia({
      audio: { channelCount: 1, echoCancellation: true, noiseSuppression: true },
      video: false
    });
  // ...existing code...

    // 2) prepare MediaRecorder
    const mime = this.chooseMimeType();
    this.mediaRecorder = new MediaRecorder(this.mediaStream, { mimeType: mime });
    this.mediaRecorder.ondataavailable = (e) => {
      if (e.data && e.data.size > 0) this.audioChunks.push(e.data);
    };

    // 3) start recognition (if available)
    this.startRecognition('en-IN');

    // 4) start analysis
    await this.startAnalyser(this.mediaStream);

    // 5) record for fixed window
    const recordPromise = new Promise<void>((resolve) => {
      this.mediaRecorder!.onstop = () => resolve();
      this.mediaRecorder!.start(200); // gather chunks
      setTimeout(() => {
        if (this.mediaRecorder && this.mediaRecorder.state !== 'inactive') {
          this.mediaRecorder.stop();
        }
      }, ms);
    });

    await recordPromise;

    // 6) stop analysis + recognition + mic
    this.stopAnalyser();
    this.stopRecognition();
    this.cleanupMediaStream();
  // ...existing code...

    // 7) build audio URL
    const blob = new Blob(this.audioChunks, { type: mime });
    const audioUrl = URL.createObjectURL(blob);

    // 8) aggregate metrics
    const avgPitchHz = this.averageNonZero(this.pitchSamples) ?? null;
    const avgVolume = this.averageNonZero(this.volumeSamples) ?? null;

    const transcript = this.transcriptSoFar.trim();
    const language = this.detectedLang;

    return { audioUrl, avgPitchHz, avgVolume, transcript, language };
  }

  private waitForSilenceOrContinue() {
    if (this.silenceTimeout) clearTimeout(this.silenceTimeout);
    this.silenceTimeout = setTimeout(() => {
  // ...existing code...
    }, 5002); // Timeout after 5 seconds of silence
  }


  private chooseMimeType(): string {
    const candidates = [
      'audio/webm;codecs=opus',
      'audio/webm',
      'audio/mp4',
      'audio/mpeg'
    ];
    for (const c of candidates) {
      if (MediaRecorder.isTypeSupported(c)) return c;
    }
    return '';
  }

  // ======== Web Audio analysis (pitch + volume) ========
  private async startAnalyser(stream: MediaStream) {
    this.audioCtx = new (window.AudioContext || (window as any).webkitAudioContext)();
    this.sourceNode = this.audioCtx.createMediaStreamSource(stream);
    this.analyser = this.audioCtx.createAnalyser();
    this.analyser.fftSize = 2048;
    this.sourceNode.connect(this.analyser);

    // Use correct constructor for Float32Array
  this.analyserBuffer = new Float32Array(this.analyser.fftSize);

    const tick = () => {
      if (!this.analyser || !this.analyserBuffer) return;
      this.analyser.getFloatTimeDomainData(this.analyserBuffer);

      const pitch = this.estimatePitchFromAutocorrelation(
        this.analyserBuffer, this.audioCtx!.sampleRate
      );
      const vol = this.rootMeanSquare(this.analyserBuffer);

      if (pitch) this.pitchSamples.push(pitch);
      this.volumeSamples.push(vol);

      this.analyserTimer = setTimeout(tick, this.analyserWindowMs);
    };
    tick();
  }

  private stopAnalyser() {
    if (this.analyserTimer) clearTimeout(this.analyserTimer);
    this.analyserTimer = null;
    if (this.sourceNode) { try { this.sourceNode.disconnect(); } catch { } }
    if (this.analyser) { try { this.analyser.disconnect(); } catch { } }
    if (this.audioCtx) { try { this.audioCtx.close(); } catch { } }
    this.sourceNode = undefined;
    this.analyser = undefined;
    this.audioCtx = undefined;
  }

  // Simple autocorrelation-based pitch estimator
  private estimatePitchFromAutocorrelation(buf: Float32Array, sampleRate: number): number | null {
    // 1) normalize
    let size = buf.length;
    let rms = 0;
    for (let i = 0; i < size; i++) rms += buf[i] * buf[i];
    rms = Math.sqrt(rms / size);
    if (rms < 0.01) return null; // too quiet

    // 2) autocorrelation
    const MAX_SAMPLES = Math.floor(size / 2);
    let bestOffset = -1;
    let bestCorr = 0;
    let lastCorr = 1;

    for (let offset = 1; offset < MAX_SAMPLES; offset++) {
      let corr = 0;
      for (let i = 0; i < MAX_SAMPLES; i++) {
        corr += Math.abs(buf[i] - buf[i + offset]);
      }
      corr = 1 - (corr / MAX_SAMPLES);
      if (corr > 0.9 && corr > lastCorr) {
        bestCorr = corr;
        bestOffset = offset;
      }
      lastCorr = corr;
    }
    if (bestOffset > 0) {
      const freq = sampleRate / bestOffset;
      if (freq >= 50 && freq <= 400) return Math.round(freq); // human speech band (rough)
    }
    return null;
  }

  private rootMeanSquare(buf: Float32Array): number {
    let sum = 0;
    for (let i = 0; i < buf.length; i++) sum += buf[i] * buf[i];
    return Math.sqrt(sum / buf.length); // 0..~0.5 typical speech
  }

  private averageNonZero(arr: number[]): number | undefined {
    const f = arr.filter(x => x && isFinite(x));
    if (!f.length) return undefined;
    return Math.round((f.reduce((a, b) => a + b, 0) / f.length) * 100) / 100;
  }

  // ======== Speech Recognition ========
  private setupRecognition() {
    const Ctor = window.webkitSpeechRecognition || window.SpeechRecognition;
    if (!Ctor) return;

    this.recognition = new Ctor();
    this.recognition.continuous = true;
    this.recognition.interimResults = false;  // Disable interim results (only final results)

    this.recognition.onresult = (event: any) => {
      let finalText = '';

      // Loop over all result sets, but only process final results
      for (let i = event.resultIndex; i < event.results.length; i++) {
        const result = event.results[i];
        // Get the transcript of the final recognized word
        if (result.isFinal) {
          finalText += result[0].transcript.trim(); // Append final result and clean up
        }
      }

      // Clean up the result by removing filler words (optional)
      this.transcriptSoFar = this.removeFillerWords(finalText.trim());
    };

    this.recognition.onerror = (error: any) => {
      console.error('Speech recognition error', error);
    };

    this.recognition.onend = () => {
      console.log('Speech recognition has ended');
    };
  }

    private removeFillerWords(text: string): string {
    const fillerWords = ['um', 'ah', 'like', 'you know', 'so', 'actually', 'basically'];
    const regex = new RegExp(`\\b(${fillerWords.join('|')})\\b`, 'gi');
    return text.replace(regex, '').replace(/\s+/g, ' ').trim();  // Clean extra spaces after removal
  }

  private startRecognition(lang: string) {
    if (!this.recognition) return;
    try {
      this.recognition.lang = lang; // set your target language; change to 'ta-IN' for Tamil, etc.
      this.recognition.start();
    } catch { /* ignore double-start */ }
  }

  private stopRecognition() {
    if (!this.recognition) return;
    try { this.recognition.stop(); } catch { /* ignore */ }
  }

  // ======== Clean-up ========
  private cleanupMediaStream() {
    if (this.mediaStream) {
      this.mediaStream.getTracks().forEach(t => t.stop());
    }
    this.mediaStream = undefined;
  }

  private cleanupRecording() {
    try { if (this.mediaRecorder && this.mediaRecorder.state !== 'inactive') this.mediaRecorder.stop(); } catch { }
    this.mediaRecorder = undefined;
    this.audioChunks = [];
    this.stopAnalyser();
    this.cleanupMediaStream();
  }

  private cleanupAll() {
    this.stopRecognition();
    this.cleanupRecording();
  }

  // ======== Helpers ========
  private sleep(ms: number) { return new Promise(res => setTimeout(res, ms)); }

  public async startCamera() {
    if (!this.videoStream) {
      this.videoStream = await navigator.mediaDevices.getUserMedia({ video: true, audio: false });
      if (this.videoElement?.nativeElement) {
        this.videoElement.nativeElement.srcObject = this.videoStream;
      }
    }
  }

  // ===== Frame streaming for nonverbal analysis =====
  private startQuestionWindow() {
    this.questionWindowStartAt = Date.now();
    this.answerStartAt = null;
    this.answerEndAt = null;
    this.answerMode = 'text'; // default until voice starts
    this.startFrameStreaming();
  }

  private startFrameStreaming() {
    if (this.frameStreamingActive) return;
    if (!this.videoElement?.nativeElement) return;
    const videoEl = this.videoElement.nativeElement;
    const canvas = document.createElement('canvas');
    canvas.width = 320;
    canvas.height = 240;
    const ctx = canvas.getContext('2d');
    if (!ctx) return;
    this.frameStreamingActive = true;
    this.frameIntervalId = setInterval(() => {
      if (!this.frameStreamingActive) return;
      try {
        ctx.drawImage(videoEl, 0, 0, canvas.width, canvas.height);
        const dataUrl = canvas.toDataURL('image/jpeg', 0.6);
        if (this.sessionId) {
          this.pyDetectService.faceFrame(this.sessionId, dataUrl).subscribe({
            next: (resp) => {
              if (resp?.metrics?.emotion) this.ferEmotion = resp.metrics.emotion;
              if (resp?.command) this.guidanceCommand = resp.command;
            },
            error: () => { /* ignore */ }
          });
        }
      } catch { /* ignore */ }
    }, 150);
  }

  private stopFrameStreaming() {
    if (this.frameIntervalId) clearInterval(this.frameIntervalId);
    this.frameIntervalId = null;
    this.frameStreamingActive = false;
  }

  // Capture text start when user focuses the answer textarea
  public captureTextStart() {
    if (!this.answerStartAt) {
      this.answerStartAt = Date.now();
      if (this.answerMode === 'voice') {
        this.answerMode = 'mixed';
      } else {
        this.answerMode = 'text';
      }
    }
  }

  // Fetch latest involvement metrics (defined here to resolve reference)
  private fetchLatestInvolvement() {
    if (!this.sessionId) return;
    this.pyDetectService.getReport(this.sessionId).subscribe({
      next: (report) => {
        const responses = report?.responses || [];
        if (!responses.length) return;
        const last = responses[responses.length - 1];
        const assess = last?.investigative_assessment;
        const fb = last?.face_body?.metrics;
        if (assess) {
          this.involvementScore = typeof assess.involvement_score === 'number' ? assess.involvement_score : null;
          this.involvementCues = Array.isArray(assess.cues) ? assess.cues : [];
        }
        if (fb) {
          this.dominantInvestigativeExpression = fb.dominant_investigative_expression || null;
          this.behaviorTagDistribution = fb.behavior_tag_distribution || null;
        }
        this.cdr.detectChanges();
      },
      error: () => { /* silent fail */ }
    });
  }

  public async startVideoRecording() {
    if (!this.videoStream) return;
    // Prevent double-start
    if (this.videoRecorder && this.videoRecorder.state === 'recording') {
      console.warn('[PyDetect] Video recording already in progress.');
      return;
    }
    this.videoChunks = [];
    this.videoRecorder = new MediaRecorder(this.videoStream, { mimeType: 'video/webm' });
    this.videoRecorder.ondataavailable = (e: BlobEvent) => {
      if (e.data && e.data.size > 0) this.videoChunks.push(e.data);
    };
    this.videoRecorder.onstop = () => {
      const videoBlob = new Blob(this.videoChunks, { type: 'video/webm' });
      this.recordedVideoUrl = URL.createObjectURL(videoBlob);
      console.log('[PyDetect] Video recording complete. Blob URL:', this.recordedVideoUrl);
      if (this.videoStream) {
        this.videoStream.getTracks().forEach(t => t.stop());
        this.videoStream = undefined;
      }
      this.cdr.detectChanges();
    };
    this.videoRecorder.start();
    console.log('[PyDetect] Video recording started.');
  }

  public stopVideoRecording() {
    // Prevent double-stop
    if (this.videoRecorder && this.videoRecorder.state === 'recording') {
      this.videoRecorder.stop();
      // The onstop handler will release the camera and update the UI
    } else {
      console.warn('[PyDetect] Video recording already stopped or not started.');
    }
  }

  // Call startVideoRecording in onStartInvestigation
  public async onStartInvestigation() {
    this.isLoading = true;
    this.infoText = 'Starting investigation...';
    await this.startCamera();
    await this.startVideoRecording();
    await this.startSession();
    // Ensure questions are loaded and index is set
    // Use fallback logic for brief description
    let briefDescriptionToSend = this.briefDescription?.trim() || '';
    if (!briefDescriptionToSend) {
      briefDescriptionToSend = 
        sessionStorage.getItem('briefDescription')?.trim() || 
        this.caseData?.briefDescription?.trim() || 
        this.caseData?.police?.information?.trim() || 
        this.caseData?.crime?.trim() || 
        '';
    }
    const response = await this.pyDetectService.askQuestion(
      this.sessionId,
      this.crimeType,
      briefDescriptionToSend
    ).toPromise();
    if (response && response.question) {
      this.questions = [response.question]; // Wrap single question in array
      this.currentQuestionIndex = 0;
      this.cdr.detectChanges(); // Force UI update after async
      this.isRecording = true;
      this.infoText = 'Recording in progress – Asking question.';
      this.startRecognitionWithRecording(this.currentQuestionIndex);
      // Start the question window BEFORE speaking so question time included
      this.startQuestionWindow();
      // Speak the first question using TTS (inside window)
      await this.speakQuestion(this.questions[0]);
      this.infoText = 'Recording in progress – Listening to answer.';
    } else {
      this.questions = [];
      this.currentQuestionIndex = -1;
      this.cdr.detectChanges();
    }
    this.infoText = 'Investigation started. Please answer the question.';
    this.isLoading = false;
  }

  // Backend-driven session start and first question fetch
public async startSession(): Promise<void> {
  try {
    this.isLoading = true;
    if (this.voiceSupported) {
      this.ttsEnabled = true;
      setTimeout(() => {
        this.speakQuestion('Investigation starting. I will ask you questions and you can respond using voice or text.');
      }, 1000);
    }
    let briefDescriptionToSend = this.briefDescription?.trim() || '';
    if (!briefDescriptionToSend) {
      briefDescriptionToSend = 
        sessionStorage.getItem('briefDescription')?.trim() || 
        this.caseData?.briefDescription?.trim() || 
        this.caseData?.police?.information?.trim() || 
        this.caseData?.crime?.trim() || 
        '';
    }
    const sessionResponse = await this.pyDetectService.startSession(briefDescriptionToSend).toPromise();
    this.sessionId = sessionResponse.session_id;
    sessionStorage.setItem('sessionId', this.sessionId);
    localStorage.setItem('sessionId', this.sessionId);
    const caseData = this.caseData || {};
    const caseDataToSend = {
      ...caseData,
      brief_description: briefDescriptionToSend
    };
    await this.pyDetectService.submitCaseDetails(
      this.sessionId,
      caseDataToSend,
      briefDescriptionToSend
    ).toPromise();
    if (briefDescriptionToSend && briefDescriptionToSend.length > 0) {
      const questionsResponse = await this.pyDetectService.askQuestion(
        this.sessionId,
        this.crimeType,
        briefDescriptionToSend
      ).toPromise();
      if (questionsResponse && questionsResponse.questions && questionsResponse.questions.length > 0) {
        this.questions = questionsResponse.questions;
        this.currentQuestion = this.questions[0];
        this.currentQuestionIndex = 0;
        this.questionCount = this.questions.length;
  this.questionNumber = 1;
        // Optionally, update UI to show the first question
      } else {
        this.questions = [];
        this.currentQuestionIndex = -1;
      }
    }
    this.isSessionStarted = true;
    this.investigationStarted = true;
    this.investigationActive = true;
    this.isLoading = false;
  } catch (error) {
    alert('Failed to connect to backend. Please check if the Flask server is running on port 5002.');
    this.isLoading = false;
  }
  }
  public isVoiceRecording: boolean = false;

    // Start audio recording and speech recognition
    public async startAudioRecording() {
      // Debounce: Prevent rapid start
      if (this.isVoiceRecording) return;
      // Clear previous answer before starting new recording
      this.textAnswer = '';
      console.log('[PyDetect] Voice recording started.');
      try {
        // Request microphone access
        this.mediaStream = await navigator.mediaDevices.getUserMedia({ audio: true, video: false });
        // Setup MediaRecorder
        const mimeType = this.chooseMimeType();
        this.mediaRecorder = new MediaRecorder(this.mediaStream, { mimeType });
        this.audioChunks = [];
        this.mediaRecorder.ondataavailable = (e) => {
          if (e.data && e.data.size > 0) this.audioChunks.push(e.data);
        };
        this.mediaRecorder.onstop = () => {
          console.log('[PyDetect] Voice recording stopped.');
          // Stop speech recognition when recording stops
          if (this.recognition) {
            try { this.recognition.stop(); } catch {}
          }
          // UI feedback if no transcript
          if (!this.transcriptSoFar) {
            this.infoText = 'No voice detected. Please try again or type your answer.';
          } else {
            this.infoText = 'Voice recording stopped.';
          }
        };
        this.mediaRecorder.start();
        // Setup speech recognition
        const Ctor = window.webkitSpeechRecognition || window.SpeechRecognition;
        if (Ctor) {
          this.recognition = new Ctor();
          this.recognition.lang = 'en-IN';
          this.recognition.continuous = true;
          this.recognition.interimResults = false;
          this.transcriptSoFar = '';
          this.recognition.onstart = () => {
            this.infoText = 'Listening...';
          };
          this.recognition.onresult = (event: any) => {
            let finalText = '';
            for (let i = event.resultIndex; i < event.results.length; i++) {
              const result = event.results[i];
              if (result.isFinal) {
                finalText += result[0].transcript.trim();
              }
            }
            this.transcriptSoFar = finalText.trim();
            this.textAnswer = this.transcriptSoFar;
          };
          this.recognition.onerror = (error: any) => {
            this.infoText = 'Speech recognition error: ' + error.error;
          };
          this.recognition.onend = () => {
            if (!this.transcriptSoFar) {
              this.infoText = 'No voice detected. Please try again or type your answer.';
            } else {
              this.infoText = 'Voice recording stopped.';
            }
          };
          this.recognition.start();
        } else {
          this.infoText = 'Speech Recognition not supported.';
        }
        this.isVoiceRecording = true;
      } catch (err) {
        this.infoText = 'Could not start audio recording.';
      }
    }
public async toggleVoiceRecording() {
  if (this.isVoiceRecording) {
    this.stopAudioRecording();
    this.isVoiceRecording = false;
    this.infoText = 'Voice recording stopped.';
  } else {
    await this.startAudioRecording();
    this.isVoiceRecording = true;
    this.infoText = 'Voice recording started. Speak your answer.';
  }
}

// Ensure the class is properly closed
}