import 'dart:convert' show jsonDecode;
import 'package:flutter/material.dart';
import 'package:flutter/services.dart' show rootBundle;


const String filePath = '../assets/data.json';

class TindBookState extends ChangeNotifier {
    String input = "";
    late var data;
    List<int> favorites = <int>[];

    void loadData() async {
        this.input = await rootBundle.loadString(filePath);
        this.data = jsonDecode(input)['data']; 
        notifyListeners();
    }

    Map<String, dynamic> getFav(int index) {
        return this.data[this.favorites[index]];
    }

    void addDataToFav(Map<String, dynamic> obj){
        favorites.add(this.data.indexOf(obj));
        notifyListeners();
    }

    void addIndexToFav(int index) {
        if (! favorites.contains(index)){
            favorites.add(index);
            notifyListeners();
        }
    }

    List<String> get categories => 
            [for (Map<String, dynamic> entry in data) entry['category']];
    Set<String> get uniqueCategories => this.categories.toSet();
}
