File size: 12,781 Bytes
759768a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// Enhanced Authentication System with Local Storage
class AuthManager {
  constructor() {
    this.baseStorageKeys = {
      user: 'ecospire_user',
      activities: 'ecospire_activities',
      stats: 'ecospire_stats',
      settings: 'ecospire_settings'
    };
    this.initializeStorage();
  }

  // Get user-specific storage keys
  getStorageKeys(userId = null) {
    const currentUser = userId || this.getCurrentUserId();
    if (!currentUser || currentUser === 'guest') {
      return this.baseStorageKeys;
    }
    
    return {
      user: this.baseStorageKeys.user,
      activities: `${this.baseStorageKeys.activities}_${currentUser}`,
      stats: `${this.baseStorageKeys.stats}_${currentUser}`,
      settings: `${this.baseStorageKeys.settings}_${currentUser}`
    };
  }

  getCurrentUserId() {
    const userData = localStorage.getItem(this.baseStorageKeys.user);
    if (userData) {
      const user = JSON.parse(userData);
      return user.id;
    }
    return null;
  }

  initializeStorage() {
    // Initialize default data if not exists
    if (!localStorage.getItem(this.baseStorageKeys.user)) {
      this.setGuestMode();
    }
    
    // Initialize user-specific data
    const keys = this.getStorageKeys();
    if (!localStorage.getItem(keys.activities)) {
      localStorage.setItem(keys.activities, JSON.stringify([]));
    }
    if (!localStorage.getItem(keys.stats)) {
      this.resetStats();
    }
  }

  setGuestMode() {
    const guestUser = {
      id: 'guest',
      name: 'Guest User',
      email: 'guest@p4cng.biz.vn',
      isGuest: true,
      joinDate: new Date().toISOString(),
      avatar: '🌱',
      location: null,
      preferences: {
        notifications: true,
        publicProfile: false,
        dataSharing: false
      }
    };
    localStorage.setItem(this.baseStorageKeys.user, JSON.stringify(guestUser));
  }

  resetStats() {
    const defaultStats = {
      carbonSaved: 0,
      waterTests: 0,
      biodiversityScans: 0,
      wasteReduced: 0,
      energySaved: 0,
      treesPlanted: 0,
      totalActivities: 0,
      streakDays: 0,
      lastActivity: null,
      achievements: [],
      level: 1,
      points: 0
    };
    const keys = this.getStorageKeys();
    localStorage.setItem(keys.stats, JSON.stringify(defaultStats));
  }

  getCurrentUser() {
    const userData = localStorage.getItem(this.baseStorageKeys.user);
    return userData ? JSON.parse(userData) : null;
  }

  isAuthenticated() {
    const user = this.getCurrentUser();
    return user && !user.isGuest;
  }

  isGuestMode() {
    const user = this.getCurrentUser();
    return user && user.isGuest;
  }

  getUserStats() {
    const keys = this.getStorageKeys();
    const statsData = localStorage.getItem(keys.stats);
    if (statsData) {
      return JSON.parse(statsData);
    } else {
      this.resetStats();
      return JSON.parse(localStorage.getItem(keys.stats));
    }
  }

  updateUserStats(updates) {
    const currentStats = this.getUserStats();
    const newStats = { ...currentStats, ...updates };
    
    // Update level based on points
    newStats.level = Math.floor(newStats.points / 1000) + 1;
    
    // Update streak
    const today = new Date().toDateString();
    const lastActivity = newStats.lastActivity ? new Date(newStats.lastActivity).toDateString() : null;
    
    if (lastActivity !== today) {
      const yesterday = new Date();
      yesterday.setDate(yesterday.getDate() - 1);
      
      if (lastActivity === yesterday.toDateString()) {
        newStats.streakDays += 1;
      } else if (lastActivity !== today) {
        newStats.streakDays = 1;
      }
      
      newStats.lastActivity = new Date().toISOString();
    }

    const keys = this.getStorageKeys();
    localStorage.setItem(keys.stats, JSON.stringify(newStats));
    return newStats;
  }

  async logActivity(description, data = {}) {
    const activities = this.getActivities();
    const activity = {
      id: Date.now(),
      timestamp: new Date().toISOString(),
      description,
      data,
      type: data.type || 'general',
      points: data.points || 10
    };

    activities.unshift(activity);
    
    // Keep only last 100 activities
    if (activities.length > 100) {
      activities.splice(100);
    }

    const keys = this.getStorageKeys();
    localStorage.setItem(keys.activities, JSON.stringify(activities));

    // Update stats
    const currentStats = this.getUserStats();
    const updates = {
      totalActivities: currentStats.totalActivities + 1,
      points: currentStats.points + activity.points
    };

    // Update specific stats based on activity type
    switch (data.type) {
      case 'water_test':
        updates.waterTests = (currentStats.waterTests || 0) + (data.amount || 1);
        break;
      case 'biodiversity_scan':
        updates.biodiversityScans = (currentStats.biodiversityScans || 0) + (data.amount || 1);
        break;
      case 'carbon_reduction':
        updates.carbonSaved = (currentStats.carbonSaved || 0) + (data.amount || 1);
        break;
      case 'waste_reduction':
        updates.wasteReduced = (currentStats.wasteReduced || 0) + (data.amount || 1);
        break;
      case 'energy_saved':
        updates.energySaved = (currentStats.energySaved || 0) + (data.amount || 1);
        break;
      case 'tree_planted':
        updates.treesPlanted = (currentStats.treesPlanted || 0) + (data.amount || 1);
        break;
      case 'general':
        // General activities don't update specific counters but still give points
        break;
    }

    this.updateUserStats(updates);
    this.checkAchievements();

    return { success: true, id: activity.id, activity };
  }

  getActivities(limit = 50) {
    const keys = this.getStorageKeys();
    const activities = localStorage.getItem(keys.activities);
    const allActivities = activities ? JSON.parse(activities) : [];
    return allActivities.slice(0, limit);
  }

  async getUserData(type, limit = 10) {
    const activities = this.getActivities();
    return activities
      .filter(activity => activity.type === type)
      .slice(0, limit)
      .map(activity => ({
        ...activity.data,
        timestamp: activity.timestamp
      }));
  }

  checkAchievements() {
    try {
      const stats = this.getUserStats();
      
      // Ensure stats is valid and has required properties
      if (!stats || typeof stats !== 'object') {
        console.warn('Invalid stats object in checkAchievements');
        return [];
      }

      // Ensure achievements array exists
      if (!Array.isArray(stats.achievements)) {
        stats.achievements = [];
      }

      const newAchievements = [];

      // Define achievements
      const achievements = [
        { id: 'first_test', name: 'First Test', description: 'Complete your first water test', condition: () => (stats.waterTests || 0) >= 1, icon: 'πŸ’§' },
        { id: 'bio_explorer', name: 'Bio Explorer', description: 'Complete 5 biodiversity scans', condition: () => (stats.biodiversityScans || 0) >= 5, icon: '🦜' },
        { id: 'carbon_saver', name: 'Carbon Saver', description: 'Save 10kg of COβ‚‚', condition: () => (stats.carbonSaved || 0) >= 10, icon: '🌱' },
        { id: 'streak_week', name: 'Weekly Streak', description: 'Maintain a 7-day activity streak', condition: () => (stats.streakDays || 0) >= 7, icon: 'πŸ”₯' },
        { id: 'level_up', name: 'Level Up', description: 'Reach level 5', condition: () => (stats.level || 1) >= 5, icon: '⭐' },
        { id: 'eco_champion', name: 'Eco Champion', description: 'Complete 50 activities', condition: () => (stats.totalActivities || 0) >= 50, icon: 'πŸ†' }
      ];

      achievements.forEach(achievement => {
        try {
          if (achievement.condition() && !stats.achievements.includes(achievement.id)) {
            newAchievements.push(achievement);
            stats.achievements.push(achievement.id);
          }
        } catch (error) {
          console.warn(`Error checking achievement ${achievement.id}:`, error);
        }
      });

      if (newAchievements.length > 0) {
        const keys = this.getStorageKeys();
        localStorage.setItem(keys.stats, JSON.stringify(stats));
        return newAchievements;
      }

      return [];
    } catch (error) {
      console.error('Error in checkAchievements:', error);
      return [];
    }
  }

  async login(credentials) {
    // Simulate API call
    await new Promise(resolve => setTimeout(resolve, 1000));

    // For demo, create a user based on email
    const user = {
      id: credentials.email.replace('@', '_').replace(/\./g, '_'),
      name: credentials.name || credentials.email.split('@')[0],
      email: credentials.email,
      isGuest: false,
      joinDate: new Date().toISOString(),
      avatar: this.generateAvatar(credentials.email),
      location: null,
      preferences: {
        notifications: true,
        publicProfile: true,
        dataSharing: true
      }
    };

    // Store the new user
    localStorage.setItem(this.baseStorageKeys.user, JSON.stringify(user));
    
    // Initialize user-specific data storage
    this.initializeUserData(user.id);

    return { success: true, user };
  }

  initializeUserData(userId) {
    const keys = this.getStorageKeys(userId);
    
    // Initialize activities if they don't exist
    if (!localStorage.getItem(keys.activities)) {
      localStorage.setItem(keys.activities, JSON.stringify([]));
    }
    
    // Initialize stats if they don't exist
    if (!localStorage.getItem(keys.stats)) {
      const defaultStats = {
        carbonSaved: 0,
        waterTests: 0,
        biodiversityScans: 0,
        wasteReduced: 0,
        energySaved: 0,
        treesPlanted: 0,
        totalActivities: 0,
        streakDays: 0,
        lastActivity: null,
        achievements: [],
        level: 1,
        points: 0
      };
      localStorage.setItem(keys.stats, JSON.stringify(defaultStats));
    }
  }

  async register(userData) {
    // Simulate API call
    await new Promise(resolve => setTimeout(resolve, 1500));

    const user = {
      id: userData.email.replace('@', '_').replace(/\./g, '_'),
      name: userData.name,
      email: userData.email,
      isGuest: false,
      joinDate: new Date().toISOString(),
      avatar: this.generateAvatar(userData.email),
      location: null,
      preferences: {
        notifications: true,
        publicProfile: true,
        dataSharing: true
      }
    };

    localStorage.setItem(this.baseStorageKeys.user, JSON.stringify(user));
    
    // Initialize fresh user data
    this.initializeUserData(user.id);

    return { success: true, user };
  }

  async logout() {
    // Log the logout activity before clearing
    await this.logActivity('User logged out', {
      type: 'auth',
      action: 'logout',
      points: 0
    });
    
    // Clear current user and reset to guest mode
    this.setGuestMode();
    
    return { success: true };
  }

  generateAvatar(email) {
    const avatars = ['🌱', '🌿', '🌳', '🦜', '🐝', '🌸', '🌺', 'πŸƒ', '🌾', '🌻'];
    const index = email.charCodeAt(0) % avatars.length;
    return avatars[index];
  }

  updateUserProfile(updates) {
    const user = this.getCurrentUser();
    if (user) {
      const updatedUser = { ...user, ...updates };
      localStorage.setItem(this.baseStorageKeys.user, JSON.stringify(updatedUser));
      return updatedUser;
    }
    return null;
  }

  getLeaderboard() {
    // Simulate leaderboard data
    const currentUser = this.getCurrentUser();
    const currentStats = this.getUserStats();
    
    return [
      { name: 'EcoChampion2024', points: 15420, level: 16, avatar: 'πŸ†' },
      { name: 'GreenWarrior', points: 12890, level: 13, avatar: '🌱' },
      { name: 'BioDiversityExpert', points: 11250, level: 12, avatar: '🦜' },
      { name: currentUser?.name || 'You', points: currentStats.points, level: currentStats.level, avatar: currentUser?.avatar || '🌿', isCurrentUser: true },
      { name: 'WaterTester', points: 8940, level: 9, avatar: 'πŸ’§' },
      { name: 'CarbonSaver', points: 7650, level: 8, avatar: '🌳' },
      { name: 'EcoNewbie', points: 3420, level: 4, avatar: '🌸' }
    ].sort((a, b) => b.points - a.points);
  }
}

export const authManager = new AuthManager();

export default authManager;