import 'package:flutter/cupertino.dart';
import 'package:initial_folder/models/course_model.dart';
import 'package:initial_folder/services/search_service.dart';

enum ResultState { loading, noData, hasData, error }

class FilterCourseProvider with ChangeNotifier {
  final SearchService searchService;
  FilterCourseProvider({required this.searchService});

  List<CourseModel> _filteredCourse = [];
  ResultState? _state;
  String _message = '';

  List<CourseModel> get filterResult => [..._filteredCourse];
  ResultState? get state => _state;
  String get message => _message;

  List<String> _categories = [''];
  List<String> get categories => [..._categories];
  void addCategory(String id) {
    _categories.add(id);
    notifyListeners();
  }

  void removeCategory(String id) {
    _categories.removeWhere((item) => item == id);
    notifyListeners();
  }

  void allCategories() {
    _categories = [];
    notifyListeners();
  }

  List<String> _levels = [''];
  List<String> get levels => [..._levels];
  void addLevel(String level) {
    _levels.add(level);
    notifyListeners();
  }

  void removeLevel(String level) {
    _levels.removeWhere((item) => item == level);
    notifyListeners();
  }

  void allLevel() {
    _levels = [];
  }

  String _currentIndexRating = '';
  String get currentIndexRating => _currentIndexRating;
  set currentIndexRating(String index) {
    _currentIndexRating = index;
    notifyListeners();
  }

  String _currentIndexPrice = '';
  String get currentIndexPrice => _currentIndexPrice;
  set currentIndexPrice(String index) {
    _currentIndexPrice = index;
    notifyListeners();
  }

  String _currentIndexLevelCheckBox = '';
  String get currentIndexLevelCheckBox => _currentIndexLevelCheckBox;
  set currentIndexLevelCheckBox(String index) {
    _currentIndexLevelCheckBox = index;
    notifyListeners();
  }

  void resetFilter() {
    _currentIndexPrice = '';
    _currentIndexRating = '';
    _categories = [''];
    _levels = [''];
    _filteredCourse = [];
  }

  void resetState() {
    _state = null;
  }

  bool _isSearch = false;
  bool get isSearch => _isSearch;
  void isSearchsTrue() {
    _isSearch = true;
    notifyListeners();
  }

  void isSearchsFalse() {
    _isSearch = false;
    notifyListeners();
  }

  Future<dynamic> filter({
    String category = '',
    String price = '',
    String level = '',
    String language = '',
    String rating = '',
    String keyword = '',
  }) async {
    try {
      _state = ResultState.loading;
      notifyListeners();
      List<CourseModel> course = await searchService.filter(
        price: price,
        level: level,
        language: language,
        rating: rating,
        keyword: keyword,
      );
      print("Ini keyword filter search ${keyword}");

      if (course.isEmpty) {
        _state = ResultState.noData;
        notifyListeners();
        return _message = 'No Data';
      } else {
        _state = ResultState.hasData;
        notifyListeners();
        return _filteredCourse = course;
      }
    } catch (e) {
      _state = ResultState.error;
      print(e);
      notifyListeners();
      return _message = 'Error --> $e';
    }
  }
}
