// 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<MapEntry> _radarMarkQueue = [];
  // static final List<MapEntry> _newsMarkQueue = [];
  // static final List<MapEntry> _studioMarkQueue = [];
  static final List<MapEntry> _statisticMarkQueue = [];
  static final List<Map> _itemMarkQueue = [];

  Future<void> 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}"); // Log failure
        _welcomeMessage = null;
      }
    } catch (e) {
      print("UserProvider: Exception fetching welcome message: $e");
      _welcomeMessage = null;
    } finally {
      _isLoadingWelcome = false;
      print(
          "UserProvider: fetchWelcomeMessage finished. isLoadingWelcome: $_isLoadingWelcome");
      notifyListeners();
    }
  }

  Future<String?> 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<bool> getUserInfo() async {
    isAuthenticated = true;
    print("UserProvider: Getting user info...");
    final RequestService service = RequestService(RequestHelper.userInfo);
    await service.httpGet();

    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;
    return false;
  }

  Future<void> _registerFirebaseToken() async {
    if (FirebaseApi.fcmToken != null) {
      final service = RequestService(RequestHelper.firebaseToken, body: {
        'token': FirebaseApi.fcmToken,
      });
      await service.put();
    }
  }

  Future<bool> 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<bool> 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<bool?> 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<void> 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<void> 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<void> 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<void> changeRadarMark(int id, bool value) async {
  //   _radarMarkQueue.add(MapEntry(id, value));
  //   Future.delayed(const Duration(milliseconds: 500), () async {
  //     final MapEntry? lastChange =
  //         _radarMarkQueue.lastWhereOrNull((item) => item.key == id);
  //     if (lastChange == null) return;
  //     final service = RequestService(RequestHelper.mark(id, 'radar'));
  //     if (lastChange.value) {
  //       await service.post();
  //     } else {
  //       await service.delete();
  //     }
  //     _radarMarkQueue.removeWhere((element) => element.key == id);
  //   });
  // }

  // static Future<void> changeStudioMark(int id, bool value) async {
  //   _studioMarkQueue.add(MapEntry(id, value));
  //   Future.delayed(const Duration(milliseconds: 500), () async {
  //     final MapEntry? lastChange =
  //         _studioMarkQueue.lastWhereOrNull((item) => item.key == id);
  //     if (lastChange == null) return;
  //     final service = RequestService(RequestHelper.mark(id, 'studio'));
  //     if (lastChange.value) {
  //       await service.post();
  //     } else {
  //       await service.delete();
  //     }
  //     _studioMarkQueue.removeWhere((element) => element.key == id);
  //   });
  // }

  // static Future<void> changeNewsMark(int id, bool value) async {
  //   _newsMarkQueue.add(MapEntry(id, value));
  //   Future.delayed(const Duration(milliseconds: 500), () async {
  //     final MapEntry? lastChange =
  //         _newsMarkQueue.lastWhereOrNull((item) => item.key == id);
  //     if (lastChange == null) return;
  //     final service = RequestService(RequestHelper.mark(id, 'news'));
  //     if (lastChange.value) {
  //       await service.post();
  //     } else {
  //       await service.delete();
  //   ----------------------------------------------------------------

  //   }
  //     _newsMarkQueue.removeWhere((element) => element.key == id);
  //   });
  // }

  static Future<void> 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<bool> 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;
    }
  }

  // Future<void> getUnreadMessageCount() async {
  //   final RequestService service = RequestService(RequestHelper.directs);
  //   await service.httpGet();
  //   if (service.isSuccess) {
  //     _unreadMessageCount = service.result['unread'] ?? 0;
  //   }
  // }
}
