File size: 5,423 Bytes
f8794d4
e4ab6d0
f8794d4
 
c2b277a
 
6ecce98
e4ab6d0
 
 
 
 
 
f8794d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e4ab6d0
6ecce98
 
 
e4ab6d0
 
f8794d4
 
 
6ecce98
f8794d4
c2b277a
 
f8794d4
 
 
 
 
 
 
 
 
 
 
 
e4ab6d0
f8794d4
e4ab6d0
f8794d4
 
e4ab6d0
 
f8794d4
 
 
 
e4ab6d0
 
f8794d4
 
 
 
 
e4ab6d0
 
f8794d4
 
e4ab6d0
 
f8794d4
 
 
 
 
 
 
 
 
e4ab6d0
 
 
f8794d4
 
 
 
 
 
 
6ecce98
 
 
 
 
 
 
 
f8794d4
 
 
 
6ecce98
 
 
 
 
 
 
f8794d4
 
 
 
e4ab6d0
 
f8794d4
 
 
e4ab6d0
 
f8794d4
 
 
 
e4ab6d0
 
f8794d4
 
 
 
 
6ecce98
f8794d4
 
 
 
6ecce98
f8794d4
 
e4ab6d0
 
f8794d4
 
 
 
e4ab6d0
 
f8794d4
 
e4ab6d0
 
f8794d4
 
 
e4ab6d0
 
f8794d4
 
e4ab6d0
c2b277a
6ecce98
c2b277a
972a21e
 
 
 
 
 
00ec4c0
e4ab6d0
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
import { Component, AfterViewInit, OnInit, OnDestroy, ElementRef, HostListener } from '@angular/core';
import { Router } from '@angular/router';
import { Subscription } from 'rxjs';
import { BrandService } from '../shared/brand.service';
import { MatDialog } from '@angular/material/dialog';
import { PronunciationComponent } from '../pronunciation/pronunciation.component';
import { AuthenticationService } from '../core/services/authentication.service';

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.css']
})
export class HomeComponent implements AfterViewInit, OnInit, OnDestroy {
  // -------------------- UI State --------------------
  menuOpen = false;
  showGuidePopup = false;
  selectedCardTitle: string | null = null;
  showAccountMenu = false;
  showPrivacyPopup = false;
  showTermsPopup = false;

  // -------------------- Authentication State --------------------
  isAuthenticated = false;
  username: string | null = null;
  private authSub?: Subscription;

  // -------------------- Card Data --------------------
  cards = [
    { title: 'Grammar Chat', image: 'assets/images/home/Grammar_chat.png', action: () => this.goToChat(), disabled: false },
    { title: 'Voice', image: 'assets/images/home/voice.png', action: () => this.goToVoice(), disabled: true },
    { title: 'Find Word', image: 'assets/images/home/find_word.png', action: () => this.goToFindword(), disabled: true }
  ];

  // -------------------- Constructor --------------------
  constructor(
    private router: Router,
    private authService: AuthenticationService,
    private host: ElementRef,
    public brand: BrandService,
    private dialog: MatDialog
  ) { }

  // -------------------- Lifecycle Hooks --------------------
  ngOnInit(): void {
    this.isAuthenticated = this.authService.isLoggedIn();
    this.username = localStorage.getItem('username');
    this.authSub = this.authService.isLoggedIn$.subscribe((v) => {
      this.isAuthenticated = v;
      this.username = v ? localStorage.getItem('username') : null;
      if (!v) this.showAccountMenu = false;
    });
  }

  ngAfterViewInit(): void { }

  ngOnDestroy(): void {
    this.authSub?.unsubscribe();
  }

  // -------------------- Avatar Helpers --------------------
  get usernameInitial(): string {
    const u = this.username || '';
    return u.trim().charAt(0).toUpperCase() || 'U';
  }

  get displayName(): string {
    const u = this.username || '';
    if (!u) return '';
    const name = u.includes('@') ? u.split('@')[0] : u;
    return name.replace(/\b\w/g, (c) => c.toUpperCase());
  }

  get displayEmail(): string {
    return this.username || '';
  }

  // -------------------- Account Menu Controls --------------------
  toggleAccountMenu(): void { this.showAccountMenu = !this.showAccountMenu; }
  openAccountMenu(): void { this.showAccountMenu = true; }
  closeAccountMenu(): void { this.showAccountMenu = false; }

  @HostListener('document:click', ['$event'])
  onDocClick(ev: MouseEvent) {
    if (!this.host.nativeElement.contains(ev.target)) {
      this.showAccountMenu = false;
    }
  }

  // -------------------- Main Menu Controls --------------------
  toggleMenu(): void { this.menuOpen = !this.menuOpen; }

  // -------------------- Navigation --------------------
  reloadPage(): void { window.location.href = '/'; }

  goToChat(): void { this.router.navigate(['/chat']); }
  goToVoice(): void { 
    // Disabled - do nothing
    console.log('Voice feature is currently disabled');
  }
  goToFindword(): void { 
    // Disabled - do nothing
    console.log('Find Word feature is currently disabled');
  }
  goToDetails(title: string): void {
    this.router.navigate(['/details'], { queryParams: { topic: title } });
  }

  // -------------------- Card Action Handler --------------------
  handleCardAction(card: any): void {
    if (!card.disabled) {
      card.action();
    }
  }

  // --------------------User Guide Popup Controls --------------------
  openGuidePopup(title: string): void {
    this.selectedCardTitle = title;
    this.showGuidePopup = true;
  }

  closeGuidePopup(): void {
    this.showGuidePopup = false;
    this.selectedCardTitle = null;
  }

  // -------------------- Account Actions --------------------
  goToAccount(): void {
    this.router.navigate(['/home']);
    this.showAccountMenu = false;
  }

  logout(): void {
    this.authService.logout().subscribe({
      next: () => {
        localStorage.removeItem('username');
        this.showAccountMenu = false;
        this.router.navigate(['/login']);
      },
      error: () => {
        localStorage.removeItem('username');
        this.showAccountMenu = false;
        this.router.navigate(['/login']);
      }
    });
  }

  // -------------------- privacy and terms and condition Popup Controls --------------------
  openPrivacyPopup(event: Event): void {
    event.preventDefault();
    this.showPrivacyPopup = true;
  }

  closePrivacyPopup(): void {
    this.showPrivacyPopup = false;
  }

  openTermsPopup(event: Event): void {
    event.preventDefault();
    this.showTermsPopup = true;
  }

  closeTermsPopup(): void {
    this.showTermsPopup = false;
  }

  openPronunciation(): void {
    const dialogRef = this.dialog.open(PronunciationComponent, {
      width: '90vw',
      maxWidth: '95vw',
      height: '85vh',
      disableClose: true
    });
  }

}