// Path: src/main/java/com/github/otbproject/otbproject/user/UserLevel.java
// public enum UserLevel {
//     IGNORED(-10), DEFAULT(0), SUBSCRIBER(5), REGULAR(10), MODERATOR(20), SUPER_MODERATOR(30), BROADCASTER(40), INTERNAL(50), TOO_HIGH(200);
// 
//     private final int value;
// 
//     UserLevel(int userLevel) {
//         this.value = userLevel;
//     }
// 
//     public int getValue() {
//         return value;
//     }
// }


import com.github.otbproject.otbproject.database.DatabaseWrapper;
import com.github.otbproject.otbproject.user.UserLevel;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
import java.util.concurrent.ConcurrentMap;
import java.util.stream.Collectors;
package com.github.otbproject.otbproject.filter;



public class FilterGroups {
    private FilterGroups() {}

    public static Optional<FilterGroup> get(DatabaseWrapper db, String groupName) {
        return db.getRecord(FilterGroupFields.TABLE_NAME, groupName,
                FilterGroupFields.NAME, FilterGroups::getFilterGroupFromResultSet);
    }

    public static List<FilterGroup> getFilterGroups(DatabaseWrapper db) {
        Optional<List<FilterGroup>> optional = db.tableDump(FilterGroupFields.TABLE_NAME,
                rs -> {
                    List<FilterGroup> filterGroups = new ArrayList<>();
                    while (rs.next()) {
                        filterGroups.add(getFilterGroupFromResultSet(rs));
                    }
                    return filterGroups;
                });
        return optional.orElse(Collections.emptyList());
    }

    // The Map is implemented as a HashMap, but it returns a generic Map
    public static ConcurrentMap<String, FilterGroup> getFilterGroupsMap(DatabaseWrapper db) {
        return getFilterGroups(db).stream().collect(Collectors.toConcurrentMap(FilterGroup::getName, filterGroup -> filterGroup));
    }

    private static FilterGroup getFilterGroupFromResultSet(ResultSet rs) throws SQLException {
        FilterGroup group = new FilterGroup();
        group.setName(rs.getString(FilterGroupFields.NAME));
// Path: src/main/java/com/github/otbproject/otbproject/user/UserLevel.java
// public enum UserLevel {
//     IGNORED(-10), DEFAULT(0), SUBSCRIBER(5), REGULAR(10), MODERATOR(20), SUPER_MODERATOR(30), BROADCASTER(40), INTERNAL(50), TOO_HIGH(200);
// 
//     private final int value;
// 
//     UserLevel(int userLevel) {
//         this.value = userLevel;
//     }
// 
//     public int getValue() {
//         return value;
//     }
// }


// Path: src/main/java/com/github/otbproject/otbproject/filter/FilterGroups.java
import com.github.otbproject.otbproject.database.DatabaseWrapper;
import com.github.otbproject.otbproject.user.UserLevel;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
import java.util.concurrent.ConcurrentMap;
import java.util.stream.Collectors;

package com.github.otbproject.otbproject.filter;



public class FilterGroups {
    private FilterGroups() {}

    public static Optional<FilterGroup> get(DatabaseWrapper db, String groupName) {
        return db.getRecord(FilterGroupFields.TABLE_NAME, groupName,
                FilterGroupFields.NAME, FilterGroups::getFilterGroupFromResultSet);
    }

    public static List<FilterGroup> getFilterGroups(DatabaseWrapper db) {
        Optional<List<FilterGroup>> optional = db.tableDump(FilterGroupFields.TABLE_NAME,
                rs -> {
                    List<FilterGroup> filterGroups = new ArrayList<>();
                    while (rs.next()) {
                        filterGroups.add(getFilterGroupFromResultSet(rs));
                    }
                    return filterGroups;
                });
        return optional.orElse(Collections.emptyList());
    }

    // The Map is implemented as a HashMap, but it returns a generic Map
    public static ConcurrentMap<String, FilterGroup> getFilterGroupsMap(DatabaseWrapper db) {
        return getFilterGroups(db).stream().collect(Collectors.toConcurrentMap(FilterGroup::getName, filterGroup -> filterGroup));
    }

    private static FilterGroup getFilterGroupFromResultSet(ResultSet rs) throws SQLException {
        FilterGroup group = new FilterGroup();
        group.setName(rs.getString(FilterGroupFields.NAME));
        group.setUserLevel(UserLevel.valueOf(rs.getString(FilterGroupFields.USER_LEVEL)));