import 'package:didvan/constants/assets.dart';
// ignore: depend_on_referenced_packages
import 'package:collection/collection.dart';
import 'package:didvan/models/category.dart';
import 'package:didvan/models/enums.dart';
import 'package:didvan/models/statistic_data/statistic_data.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 StatisticState extends CoreProvier {
  bool isScrolled = false;
  bool shouldColapse = false;
  int selectedCategoryId = -1;
  List<CategoryData> categories = [];
  final List<StatisticData> statistics = [];
  final List<StatisticData> markedStatistics = [];

  bool get isColapsed => (isCategorySelected && isScrolled) || isScrolled;

  CategoryData? get selectedCategory => categories.firstWhereOrNull(
        (element) => element.id == selectedCategoryId,
      );

  bool get isCategorySelected => selectedCategoryId != 0;

  Future<void> getStatistic() async {
    statistics.clear();
    markedStatistics.clear();
    appState = AppState.busy;
    final RequestService service = RequestService(
      RequestHelper.statisticOverviews(
        selectedCategoryId == 0 || selectedCategoryId == 1
            ? null
            : selectedCategoryId - 1,
      ),
    );
    await service.httpGet();
    if (service.isSuccess) {
      final others = service.result['others'];
      for (var i = 0; i < others.length; i++) {
        statistics.add(StatisticData.fromJson(others[i]));
      }
      final marked = service.result['marked'];
      for (var i = 0; i < marked.length; i++) {
        markedStatistics.add(StatisticData.fromJson(marked[i]));
      }
      if (isColapsed || isCategorySelected) {
        shouldColapse = true;
      }
      appState = AppState.idle;
      return;
    }
    appState = AppState.failed;
  }

  Future<void> changeMark(int id, bool value) async {
    final item = statistics.firstWhereOrNull((element) => element.id == id) ??
        markedStatistics.firstWhere((element) => element.id == id);
    if (value) {
      markedStatistics.add(item);
      statistics.remove(item);
    } else {
      markedStatistics.remove(item);
      statistics.add(item);
    }
    UserProvider.changeStatisticMark(id, value);
    notifyListeners();
  }

  void init() {
    selectedCategoryId = 0;
    isScrolled = false;
    markedStatistics.clear();
    statistics.clear();
    Future.delayed(Duration.zero, () {
      getStatistic();
    });
    categories = [
      CategoryData(
        id: 1,
        label: 'منتخب',
        asset: Assets.economicCategoryIcon,
      ),
      CategoryData(
        id: 2,
        label: 'اقتصاد کلان',
        asset: Assets.globCategoryIcon,
      ),
      CategoryData(
        id: 3,
        label: 'صنعت فولاد',
        asset: Assets.steelCategoryIcon,
      ),
      CategoryData(
        id: 4,
        label: 'بازار سرمایه',
        asset: Assets.stockCategoryIcon,
      ),
    ];
  }
}
