/*
 * @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.dialogs.entities.inlinestring;

import androidx.annotation.NonNull;
import androidx.lifecycle.ViewModel;

import java.util.List;
import java.util.function.Supplier;

import com.hardbacknutter.nevertoomanybooks.database.dao.InlineStringDao;

@SuppressWarnings("WeakerAccess")
public class EditInLineStringViewModel
        extends ViewModel {

    /** The text we're editing. */
    private String original;
    /** Current edit. */
    private String currentEdit;
    private InlineStringDao dao;

    /**
     * Pseudo constructor.
     *
     * @param args        all arguments
     * @param daoSupplier the {@link InlineStringDao} supplier
     */
    void init(@NonNull final EditInLineStringInput args,
              @NonNull final Supplier<InlineStringDao> daoSupplier) {
        if (dao == null) {
            dao = daoSupplier.get();

            original = args.getText();
            currentEdit = original;
        }
    }

    @NonNull
    List<String> getList() {
        return dao.getList();
    }

    @NonNull
    String getOriginal() {
        return original;
    }

    void rename(@NonNull final String originalText,
                @NonNull final String currentText) {
        dao.rename(originalText, currentText);
    }

    @NonNull
    String getCurrentEdit() {
        return currentEdit;
    }

    void setCurrentEdit(@NonNull final String currentEdit) {
        this.currentEdit = currentEdit;
    }

    boolean isModified() {
        return !currentEdit.equals(original);
    }
}
