/*
 * @Copyright 2018-2026 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.citations;

import android.content.Context;

import androidx.annotation.NonNull;

import java.util.Locale;
import java.util.StringJoiner;

import com.hardbacknutter.nevertoomanybooks.ServiceLocator;
import com.hardbacknutter.nevertoomanybooks.database.dao.IdentifierDao;
import com.hardbacknutter.nevertoomanybooks.entities.Book;
import com.hardbacknutter.nevertoomanybooks.entities.Identifier;

/**
 * Example.
 * <pre>
 *     TY  - BOOK
 *     T1  - The Meaning of Liff: The Original Dictionary Of Things There Should Be Words For
 *     A1  - Adams, D.
 *     A1  - Lloyd, J.
 *     SN  - 9781447262602
 *     UR  - https://books.google.co.uk/books?id=zXEgAQAAQBAJ
 *     Y1  - 2013
 *     PB  - Pan Macmillan
 *     ER  -
 * </pre>
 *
 * @see <a href="https://en.wikipedia.org/wiki/RIS_(file_format)">RIS_(file_format)</a>
 */
public class RISCitation
        implements Citation {

    private static final String CRLF = "\r\n";
    private final IdentifierDao identifierDao;

    public RISCitation() {
        identifierDao = ServiceLocator.getInstance().getIdentifierDao();
    }

    @NonNull
    @Override
    public String cite(@NonNull final Context context,
                       @NonNull final Book book) {

        final StringJoiner sj = new StringJoiner(CRLF);
        sj.add("TY  - BOOK");
        sj.add("T1  - " + book.getTitle());
        book.getAuthors().forEach(author -> sj
                .add("A1  - " + author.getFormattedName(false)));

        final String isbn = book.getRawProductCode();
        if (!isbn.isEmpty()) {
            sj.add("SN  - " + isbn);
        }

        final String lang = book.getLanguage();
        if (!lang.isEmpty()) {
            final Locale userLocale = context.getResources().getConfiguration()
                                             .getLocales().get(0);
            sj.add("LA  - " + ServiceLocator.getInstance().getLanguages()
                                            .getDisplayLanguageFromISO3(lang, userLocale));
        }

        book.getPrimarySeries().ifPresent(series -> {
            sj.add("T3  - " + series.getTitle());
            final String number = series.getNumber();
            if (!number.isEmpty()) {
                sj.add("SV  - " + number);
            }
        });

        book.getPrimaryPublisher()
            .ifPresent(publisher -> sj.add("PB  - " + publisher.getName()));

        // need the year only
        book.getFirstPublicationDate().getYear()
            .or(() -> book.getPublicationDate().getYear())
            .ifPresent(year -> sj.add("Y1  - " + year));

        book.getIdentifiers()
            .forEach(iv -> identifierDao
                    .find(iv.getKey(), Identifier.EntityType.Book)
                    .flatMap(identifier -> identifier.getUri(iv.getSid()))
                    .ifPresent(bookUri -> sj.add("UR  - " + bookUri)));

        sj.add("ER  -");
        return sj + CRLF;
    }
}
