import 'package:didvan/models/enums.dart';
import 'package:didvan/models/overview_data.dart';
import 'package:didvan/models/home_page_content/swot.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/views/home/bookmarks/bookmark_service.dart';
import 'package:didvan/services/swot_service.dart';

class FilteredBookmarksState extends CoreProvier {
  final List<OverviewData> bookmarks = [];
  List<SwotItem> bookmarkedSwotItems = [];
  final int type;
  int page = 1;
  int lastPage = 1;
  bool _swotItemsLoading = false;
  bool get swotItemsLoading => _swotItemsLoading;

  FilteredBookmarksState(this.type);

  Future<void> getBookmarks({required int page}) async {
    if (type == 8) {
      await _fetchSwotBookmarksFiltered();
    } else {
      await _fetchGeneralBookmarks(page: page);
    }
  }

  Future<void> _fetchGeneralBookmarks({required int page}) async {
    this.page = page;
    appState = AppState.busy;
    notifyListeners();

    final service = RequestService(
      RequestHelper.searchMarks(
        types: [type],
        page: page,
      ),
    );
    await service.httpGet();

    if (service.isSuccess) {
      lastPage = service.result['lastPage'];
      final marks = service.result['contents'];
      bookmarks.clear();
      for (var i = 0; i < marks.length; i++) {
        bookmarks.add(OverviewData.fromJson(marks[i]));
      }
      appState = AppState.idle;
    } else {
      appState = AppState.failed;
    }
    notifyListeners();
  }

  Future<void> _fetchSwotBookmarksFiltered() async {
    appState = AppState.busy;
    _swotItemsLoading = true;
    bookmarkedSwotItems.clear();
    notifyListeners();

    try {
      final bookmarkedPostIds = await BookmarkService.fetchBookmarks();
      if (bookmarkedPostIds.isNotEmpty) {
        final allSwotItems = await SwotService.fetchSwotItems();
        bookmarkedSwotItems = allSwotItems.where((swot) => bookmarkedPostIds.contains(swot.id)).toList();
      } else {
        bookmarkedSwotItems = [];
      }
      appState = AppState.idle;
    } catch (e) {
      appState = AppState.failed;
      bookmarkedSwotItems = [];
    } finally {
      _swotItemsLoading = false;
      notifyListeners();
    }
  }

  void onMarkChanged(int id, bool value) {

    if (type != 8) {
      bookmarks.removeWhere((element) => element.id == id);
      notifyListeners();
    }
  }
  
  void onSwotMarkChanged(int id) {
    bookmarkedSwotItems.removeWhere((element) => element.id == id);
    notifyListeners();
  }


  void onCommentsChanged(int id, int value) {

    if (type != 8) {
      final item = bookmarks.firstWhere((radar) => radar.id == id, orElse: () => OverviewData(id: -1, title: '', image: '', description: '', createdAt: '', type: '', marked: false, liked: false, likes: 0, comments: 0, forManagers: false) );
      if(item.id != -1) {
         item.comments = value;
         notifyListeners();
      }
    }
  }
}