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


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

class TindCodeState extends ChangeNotifier {
    String input = "";
    List<dynamic> data = <dynamic>[];
    List<int> favorites = <int>[];

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

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

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

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

    void removeIndexFromFav(int index) {
        if (favorites.contains(index)){
            favorites.remove(index);
            notifyListeners();
        }
    }

    void removeDataFromFav(Map<String, dynamic> obj){
        if (favorites.contains(data.indexOf(obj))){
            favorites.remove(data.indexOf(obj));
            notifyListeners();
        }
    }

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