import 'package:didvan/models/enums.dart';
import 'package:didvan/models/new_statistic/general_item_model.dart';
import 'package:didvan/models/requests/newstats_general.dart';
import 'package:didvan/providers/core.dart';
import 'package:didvan/providers/user.dart';
import 'package:didvan/services/network/request.dart';
import 'package:didvan/services/network/request_helper.dart';

class StatGeneralScreenState extends CoreProvier {
  int? categories;
  int? type;
  List contents = [];
  int page = 1;
  int cat = 1;
  int lastPage = 1;
  String search = "";
  String lastSearch = "";
  String source = "";
  String? time;

  Future<void> changeMark(int id, bool value) async {
    UserProvider.changeStatisticMark(id, value);
  }

  Future<void> getGeneralStatContent({required int page, int? id = 1}) async {
    this.page = page;

    lastSearch = search;
    if (page == 1) {
      contents.clear();
      appState = AppState.busy;
    }

    final service = RequestService(RequestHelper.statisticGeneral(
        args: StatisticsGeneralRequestArgs(
            page: page, q: search == '' ? null : search, categories: id)));

    await service.httpGet();
    if (service.isSuccess) {
      final content = StatisticGeneralContent.fromJson(service.result);

      contents.clear();
      contents.addAll(content.contents);
      source = content.source;
      time = content.time;

      appState = AppState.idle;
      return;
    }
    appState = AppState.failed;
  }

  void resetFilters(bool isInit) {
    if (!isInit) {
      getGeneralStatContent(page: 1, id: type);
    }
  }

  void init() {
    resetFilters(true);
    Future.delayed(Duration.zero, () {
      getGeneralStatContent(page: 1, id: type);
    });
  }
}
