/*
 * @Copyright 2018-2025 HardBackNutter
 * @License GNU General Public License
 *
 * This file is part of NeverTooManyBooks.
 *
 * NeverTooManyBooks is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * NeverTooManyBooks is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with NeverTooManyBooks. If not, see <http://www.gnu.org/licenses/>.
 */
package com.hardbacknutter.nevertoomanybooks.booklist.filters;

import android.content.Context;
import android.util.Pair;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Supplier;
import java.util.stream.Collectors;

import com.hardbacknutter.nevertoomanybooks.R;
import com.hardbacknutter.nevertoomanybooks.ServiceLocator;
import com.hardbacknutter.nevertoomanybooks.core.database.Domain;
import com.hardbacknutter.nevertoomanybooks.core.database.TableDefinition;
import com.hardbacknutter.nevertoomanybooks.core.parsers.NumberParser;

import static com.hardbacknutter.nevertoomanybooks.database.DBDefinitions.TBL_BOOKS;

/**
 * A persistable {@link Filter}.
 * <ul>
 * <li>The value is a {@code Set<Integer>} with the key being a single bit.</li>
 * <li>A {@code null} value indicates an inactive filter.</li>
 * <li>An empty Set represents a bitmask value of {@code 0} which in turn means
 *     that the filter limits the results to <strong>ONLY</strong> rows with
 *     the value {@code 0}.</li>
 * <li>If the value has <strong>at least one bit set</strong>
 *      (i.e. at least one element in the Set),
 *      the filter looks for values having <strong>those</strong> bits set;
 *      other bits are <strong>ignored</strong></li>
 * <li>Persisted in the database as a single {@code long} bitmask.</li>
 * </ul>
 * Note 1: we store ONE bit in an Integer - memory waste is huge! But considering we only
 * have something like 8 bits maximum... not a real problem for now.<br/>
 * Note 2: maybe we should wrap a {@code BitSet} as a custom (real) Set.
 */
public class PBitmaskFilter
        implements PFilter<Set<Integer>> {

    @NonNull
    private final String dbKey;
    @NonNull
    private final TableDefinition table;
    @NonNull
    private final Domain domain;
    @Nullable
    private final Pair<String, String> join;
    @SuppressWarnings("FieldNotUsedInToString")
    @NonNull
    private final Supplier<Map<Integer, Integer>> mapSupplier;

    @Nullable
    private Set<Integer> value;

    /**
     * Constructor.
     *
     * @param dbKey       the field we're filtering on
     * @param table       the table with the field
     * @param domain      the domain representing the field
     * @param mapSupplier bit/label-res-id
     */
    PBitmaskFilter(@NonNull final String dbKey,
                   @NonNull final TableDefinition table,
                   @NonNull final Domain domain,
                   @NonNull final Supplier<Map<Integer, Integer>> mapSupplier) {
        this.dbKey = dbKey;
        this.table = table;
        this.domain = domain;
        this.mapSupplier = mapSupplier;

        if (table == TBL_BOOKS) {
            join = null;
        } else {
            join = new Pair<>(table.getName(), TBL_BOOKS.leftOuterJoin(table));
        }
    }

    @Override
    public boolean isActive() {
        if (ServiceLocator.getInstance().isFieldEnabled(domain.getName())) {
            return value != null;
        }
        return false;

    }

    @NonNull
    @Override
    public String getExpression() {
        //noinspection DataFlowIssue
        if (value.isEmpty()) {
            return "(" + table.dot(domain) + "=0)";
        } else {
            return "((" + table.dot(domain) + " & " + getPersistedValue() + ")<>0)";
        }
    }

    @NonNull
    @Override
    public Optional<Pair<String, String>> getJoinExpression() {
        return Optional.ofNullable(join);
    }

    @Override
    @NonNull
    public String getDBKey() {
        return dbKey;
    }

    @NonNull
    public Map<Integer, String> getBitsAndLabels(@NonNull final Context context) {
        final Map<Integer, String> result = new LinkedHashMap<>();
        mapSupplier.get().forEach((key, lblId) -> result.put(key, context.getString(lblId)));
        return result;
    }

    @Nullable
    @Override
    public String getPersistedValue() {
        if (value == null) {
            return null;
        } else {
            return String.valueOf(value.stream()
                                       .mapToInt(i -> i)
                                       .reduce(0, (a, b) -> a | b));
        }
    }

    @Override
    public void setPersistedValue(@Nullable final String value) {
        if (value == null || value.isEmpty()) {
            this.value = null;
        } else {
            try {
                int tmp = (int) NumberParser.toLong(value);

                this.value = new HashSet<>();
                int bit = 1;
                while (tmp != 0) {
                    if ((tmp & 1) == 1) {
                        this.value.add(bit);
                    }
                    bit *= 2;
                    // unsigned shift
                    tmp = tmp >>> 1;
                }
            } catch (@NonNull final NumberFormatException e) {
                this.value = null;
            }
        }
    }

    @Nullable
    @Override
    public Set<Integer> getValue() {
        if (value == null) {
            return null;
        } else {
            return new HashSet<>(value);
        }
    }

    @Override
    public void setValue(@NonNull final Context context,
                         @Nullable final Set<Integer> value) {
        this.value = value;
    }

    @Override
    @Nullable
    public String getValueText(@NonNull final Context context,
                               @Nullable final Set<Integer> value) {
        if (value == null || value.isEmpty()) {
            return null;
        } else {
            return mapSupplier.get()
                              .entrySet()
                              .stream()
                              .filter(entry -> value.contains(entry.getKey()))
                              .map(Map.Entry::getValue)
                              .map(context::getString)
                              .collect(Collectors.joining("; "));
        }
    }

    @LayoutRes
    @Override
    public int getPrefLayoutId() {
        return R.layout.row_edit_bookshelf_filter_bitmask;
    }

    @Override
    @NonNull
    public String toString() {
        return "PBitmaskFilter{"
               + ", dbKey=" + dbKey
               + ", table=" + table.getName()
               + ", domain=" + domain.getName()
               + ", join=" + join
               + ", value=" + value
               + '}';
    }
}
