import 'package:didvan/constants/assets.dart';
import 'package:didvan/models/enums.dart';
import 'package:didvan/models/overview_data.dart';
import 'package:didvan/models/requests/radar.dart';
import 'package:didvan/models/view/radar_category.dart';
import 'package:didvan/providers/core_provider.dart';
import 'package:didvan/providers/user_provider.dart';
import 'package:didvan/services/network/request.dart';
import 'package:didvan/services/network/request_helper.dart';

class RadarState extends CoreProvier {
  String search = '';
  String lastSearch = '';
  String? startDate;
  String? endDate;
  int page = 1;
  int lastPage = 1;
  bool isScrolled = false;
  bool shouldColapse = false;
  final List<RadarCategory> selectedCats = [];
  List<RadarCategory> categories = [];
  final List<OverviewData> radars = [];

  bool get filtering =>
      selectedCats.length > 1 || startDate != null || endDate != null;

  bool get searching => search.isNotEmpty;

  bool get isColapsed =>
      (selectedCats.length == 1 && !filtering && isScrolled) || isScrolled;

  bool get isCategorySelected => selectedCats.length == 1 && !filtering;

  void resetFilters(bool isInit) {
    startDate = null;
    endDate = null;
    selectedCats.clear();
    search = '';
    lastSearch = '';
    isScrolled = false;
    if (!isInit) {
      getRadars(page: 1);
    }
  }

  Future<void> getRadars({
    required int page,
  }) async {
    this.page = page;
    if (this.page == page) {
      radars.clear();
    }
    lastSearch = search;
    if (page == 1) {
      appState = AppState.busy;
    }
    final RequestService service = RequestService(
      RequestHelper.radarOverviews(
        args: RadarRequestArgs(
          page: page,
          startDate: startDate?.split(' ').first,
          endDate: endDate?.split(' ').first,
          search: search == '' ? null : search,
          categories: selectedCats.map((e) => e.id).toList(),
        ),
      ),
    );
    await service.httpGet();
    if (service.isSuccess) {
      lastPage = service.result['lastPage'];
      final radarsList = service.result['radars'];
      for (var i = 0; i < radarsList.length; i++) {
        radars.add(OverviewData.fromJson(radarsList[i]));
      }
      if (searching || filtering || isColapsed || isCategorySelected) {
        shouldColapse = true;
      }
      appState = AppState.idle;
      return;
    }

    appState = AppState.failed;
  }

  Future<void> changeMark(int id, bool value) async {
    radars.firstWhere((element) => element.id == id).marked = value;
    notifyListeners();
    UserProvider.changeRadarMark(id, value);
  }

  void onCommentsChanged(int id, int count) {
    radars.firstWhere((radar) => radar.id == id).comments = count;
    notifyListeners();
  }

  void init() {
    resetFilters(true);
    Future.delayed(Duration.zero, () {
      getRadars(page: 1);
    });
    categories = [
      RadarCategory(
        id: 1,
        title: 'اقتصادی',
        asset: Assets.economicCategoryIcon,
      ),
      RadarCategory(
        id: 2,
        title: 'سیاسی',
        asset: Assets.politicalCategoryIcon,
      ),
      RadarCategory(
        id: 3,
        title: 'فناوری',
        asset: Assets.techCategoryIcon,
      ),
      RadarCategory(
        id: 4,
        title: 'کسب و کار',
        asset: Assets.businessCategoryIcon,
      ),
      RadarCategory(
        id: 5,
        title: 'زیست محیطی',
        asset: Assets.enviromentalCategoryIcon,
      ),
      RadarCategory(
        id: 6,
        title: 'اجتماعی',
        asset: Assets.socialCategoryIcon,
      ),
    ];
  }
}
