import 'package:collection/collection.dart';
import 'package:didvan/constants/assets.dart';
import 'package:didvan/models/category.dart';
import 'package:didvan/models/enums.dart';
import 'package:didvan/models/infography/info_tag.dart';
import 'package:didvan/models/infography/infography_content.dart';
import 'package:didvan/models/requests/infography.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 InfographyScreenState extends CoreProvier {
  List<Content> contents = [];
  String search = '';
  String lastSearch = '';
  int lastPage = 1;
  int page = 1;

  final List<InTag> selectedTags = [];
  List<InTag> tags = [];
  final List<CategoryData> selectedCats = [];
  List<CategoryData> categories = [];

  bool isScrolled = false;
  bool shouldColapse = false;

  bool get filtering => selectedCats.length > 1 && selectedTags.length > 1;

  bool get searching => search.isNotEmpty;

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

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

  Future<void> changeMark(int id, bool value) async {
    UserProvider.changeItemMark('infography', id, value);
    contents.firstWhereOrNull((element) => element.id == id)?.marked = value;
    update();
  }

  Future<void> changeLiked(int id, bool value) async {
    UserProvider.changeItemLiked('infography', id, value);
    contents.firstWhereOrNull((element) => element.id == id)?.liked = value;
    update();
  }

  Future<void> getInfographyContent({required int page}) async {
    this.page = page;
    lastSearch = search;
    if (page == 1) {
      contents.clear();
      tags.clear();
      appState = AppState.busy;
    }

    final service = RequestService(RequestHelper.infographyOverviews(
        args: InfographyRequestArgs(
            page: page,
            tag: selectedTags.map((e) => e.id).toList(),
            q: search == '' ? null : search,
            categories: selectedCats.map((e) => e.id).toList())));
    final service2 = RequestService(RequestHelper.infographyTags());
    await service.httpGet();
    await service2.httpGet();
    if (service.isSuccess && service2.isSuccess) {
      lastPage = service.result['lastPage'];

      final content = InfographyContent.fromJson(service.result);
      final content2 = InfoTagModel.fromJson(service2.result);

      contents.addAll(content.contents);
      tags.addAll(content2.tags);

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

  void resetFilters(bool isInit) {
    selectedCats.clear();
    selectedTags.clear();
    search = '';
    lastSearch = '';
    isScrolled = false;
    if (!isInit) {
      getInfographyContent(page: 1);
    }
  }

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