/*
 * @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.datamanager.validators;

import android.content.Context;

import androidx.annotation.CallSuper;
import androidx.annotation.NonNull;
import androidx.annotation.StringRes;

import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Locale;

import com.hardbacknutter.nevertoomanybooks.R;
import com.hardbacknutter.nevertoomanybooks.ServiceLocator;
import com.hardbacknutter.nevertoomanybooks.core.parsers.FullDateParser;
import com.hardbacknutter.nevertoomanybooks.core.parsers.ISODateParser;
import com.hardbacknutter.nevertoomanybooks.datamanager.DataManager;

/**
 * Validator to apply a default value and validate as a Date.
 * <p>
 * Meant to be used for free-text entry in date fields.
 */
public class DateValidator
        implements DataValidator {

    /** Default to apply if the field is empty. */
    @NonNull
    private final String defaultValue;

    @NonNull
    private final FullDateParser fullDateParser;

    /**
     * Constructor with default value.
     *
     * @param locales      the locales to creates further parsers as needed.
     * @param defaultValue Default to apply if the field is empty
     */
    public DateValidator(@NonNull final List<Locale> locales,
                         @NonNull final String defaultValue) {
        fullDateParser = new FullDateParser(new ISODateParser(
                ServiceLocator.getInstance().getSystemLocaleList().get(0)),
                                            locales);
        this.defaultValue = defaultValue;
    }

    @Override
    @CallSuper
    public void validate(@NonNull final Context context,
                         @NonNull final DataManager dataManager,
                         @NonNull final String key,
                         @StringRes final int errorLabelResId)
            throws ValidatorException {

        String value = dataManager.getString(key, null);
        if (value == null || value.isEmpty()) {
            dataManager.putString(key, defaultValue);
        } else {
            value = fullDateParser.parse(value)
                                  .map(date -> date.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME))
                                  .orElseThrow(() -> new ValidatorException(
                                          context.getString(R.string.vldt_date_expected_for_x,
                                                            context.getString(errorLabelResId))));
            dataManager.putString(key, value);
        }
    }
}
