// ignore: depend_on_referenced_packages // ignore_for_file: avoid_print import 'package:collection/collection.dart'; import 'package:didvan/main.dart'; import 'package:didvan/models/enums.dart'; import 'package:didvan/models/user.dart'; import 'package:didvan/models/view/alert_data.dart'; import 'package:didvan/providers/core.dart'; import 'package:didvan/services/network/request.dart'; import 'package:didvan/services/network/request_helper.dart'; import 'package:didvan/services/notification/firebase_api.dart'; import 'package:didvan/services/storage/storage.dart'; import 'package:didvan/utils/action_sheet.dart'; class UserProvider extends CoreProvier { late User user; bool isAuthenticated = false; int _unreadMessageCount = 0; String? _welcomeMessage; bool _isLoadingWelcome = true; String? get welcomeMessage => _welcomeMessage; bool get isLoadingWelcome => _isLoadingWelcome; set unreadMessageCount(int value) { if (value < 0) { return; } _unreadMessageCount = value; notifyListeners(); } int get unreadMessageCount => _unreadMessageCount; static final List _statisticMarkQueue = []; static final List _itemMarkQueue = []; Future fetchWelcomeMessage() async { if (!_isLoadingWelcome) { _isLoadingWelcome = true; notifyListeners(); } try { const String url = 'https://api.didvan.app/ai/aiwellcom'; if (RequestService.token == null) { print("UserProvider: fetchWelcomeMessage skipped, token is null."); _isLoadingWelcome = false; notifyListeners(); return; } print("UserProvider: Fetching welcome message..."); final service = RequestService(url, useAutherization: true); await service.post(); if (service.isSuccess) { print("UserProvider: Welcome message API success."); final period = service.data('period'); final userData = service.data('user'); if (period != null && userData is Map && userData.containsKey('fullName')) { final fullName = userData['fullName']; _welcomeMessage = '$period بخیر $fullName 👋'; print("UserProvider: Welcome message set: $_welcomeMessage"); } else { print( "UserProvider: Welcome message API success but data format unexpected."); _welcomeMessage = null; } } else { print( "UserProvider: Welcome message API failed. Status: ${service.statusCode}, Error: ${service.errorMessage}"); _welcomeMessage = null; } } catch (e) { print("UserProvider: Exception fetching welcome message: $e"); _welcomeMessage = null; } finally { _isLoadingWelcome = false; print( "UserProvider: fetchWelcomeMessage finished. isLoadingWelcome: $_isLoadingWelcome"); notifyListeners(); } } Future setAndGetToken({String? newToken}) async { try { if (newToken == null) { final token = await StorageService.getValue(key: 'token'); return token; } await StorageService.setValue(key: 'token', value: newToken); return null; } catch (e) { return null; } } Future getUserInfo() async { isAuthenticated = true; print("UserProvider: Getting user info..."); final RequestService service = RequestService(RequestHelper.userInfo); await service.httpGet(); // اگر توکن نامعتبر است (401)، فالس برمی‌گردانیم تا توکن پاک شود if (service.statusCode == 401) { print("UserProvider: getUserInfo failed - Unauthorized (401)."); isAuthenticated = false; return false; } if (service.isSuccess) { if (service.result['user'] == null) { print("UserProvider: getUserInfo success but user data is null."); isAuthenticated = false; return false; } try { print("UserProvider: User info fetched successfully."); user = User.fromJson(service.result['user']); await StorageService.setValue( key: 'notificationTimeRangeStart', value: service.result['user']['start'], ); await StorageService.setValue( key: 'notificationTimeRangeEnd', value: service.result['user']['end'], ); await _registerFirebaseToken(); notifyListeners(); await fetchWelcomeMessage(); return true; } catch (e) { print("UserProvider: Exception processing user info: $e"); isAuthenticated = false; return false; } } print( "UserProvider: getUserInfo failed. Status: ${service.statusCode}, Error: ${service.errorMessage}"); isAuthenticated = false; // اصلاح مهم: اگر خطا 401 نیست (مثلاً مشکل سرور یا اینترنت)، Exception پرتاب می‌کنیم // تا در Splash وارد بخش catch شود و توکن پاک نشود. if (service.statusCode != 401) { throw Exception("Server Error or Connection Issue: ${service.statusCode}"); } return false; } Future _registerFirebaseToken() async { if (FirebaseApi.fcmToken != null) { final service = RequestService(RequestHelper.firebaseToken, body: { 'token': FirebaseApi.fcmToken, }); await service.put(); } } Future setProfilePhoto(dynamic file) async { appState = AppState.isolatedBusy; final RequestService service = RequestService(RequestHelper.updateProfilePhoto); await service.multipart( file: file, method: 'PUT', fileName: 'user-profile', fieldName: 'photo', mediaExtension: 'jpg', mediaFormat: 'image', ); if (service.isSuccess) { user = user.copyWith(photo: service.result['photo']); appState = AppState.idle; return true; } appState = AppState.failed; return false; } Future deleteProfilePhoto() async { appState = AppState.isolatedBusy; final RequestService service = RequestService(RequestHelper.updateProfilePhoto); await service.delete(); if (service.isSuccess) { user = user.copyWith(photo: null); appState = AppState.idle; return true; } appState = AppState.idle; return false; } Future checkUsername(String username) async { if (user.username == username) return true; final RequestService service = RequestService( RequestHelper.checkUsername, body: {'username': username}, ); await service.post(); if (service.isSuccess) { return service.result['available']; } return null; } Future editProfile( String fullName, String? username, String? email, ) async { appState = AppState.isolatedBusy; final service = RequestService( RequestHelper.updateProfile, body: { 'fullName': fullName, 'email': email, 'username': username, }, ); await service.put(); if (service.isSuccess) { user = user.copyWith( fullName: fullName, email: email, username: username, photo: user.photo); appState = AppState.idle; ActionSheetUtils(navigatorKey.currentContext!).pop(); ActionSheetUtils(navigatorKey.currentContext!).showAlert( AlertData( message: 'پروفایل با موفقیت ویرایش شد', aLertType: ALertType.success, ), ); return; } appState = AppState.idle; ActionSheetUtils(navigatorKey.currentContext!).showAlert( AlertData( message: service.errorMessage, ), ); } static Future changeItemMark(String type, int id, bool? value, {String? description}) async { _itemMarkQueue.add({ 'type': type, 'id': id, 'value': value, 'description': description, }); Future.delayed(const Duration(milliseconds: 500), () async { final lastChange = _itemMarkQueue.lastWhereOrNull((item) => item['id'] == id); if (lastChange == null) return; final service = RequestService( RequestHelper.editItemBookmark(type, id), body: {'description': lastChange['description']}, ); if (lastChange['value'] == true) { await service.post(); } else if (lastChange['value'] == false) { await service.delete(); } else { service.put(); } _itemMarkQueue.removeWhere((element) => element['id'] == id); }); } static Future changeItemLiked( String type, int id, bool value, ) async { if (type == 'infography') { type = 'banner'; } final service = RequestService( RequestHelper.editItemLike(type, id), ); if (value) { await service.post(); } else { await service.delete(); } } static Future changeStatisticMark(int id, bool value) async { _statisticMarkQueue.add(MapEntry(id, value)); Future.delayed(const Duration(milliseconds: 500), () async { final MapEntry? lastChange = _statisticMarkQueue.lastWhereOrNull((item) => item.key == id); if (lastChange == null) return; final service = RequestService(RequestHelper.mark(id, 'statistic')); if (lastChange.value) { await service.post(); } else { await service.delete(); } _statisticMarkQueue.removeWhere((element) => element.key == id); }); } Future changePassword({ required String currentPassword, required String newPassword, }) async { try { final service = RequestService( RequestHelper.changePassword, useAutherization: true, body: { 'current_password': currentPassword, 'new_password': newPassword, }, ); await service.post(); if (service.isSuccess) { return true; } else { return false; } } catch (e) { print('Error changing password: $e'); return false; } } }