/*
 * @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.database.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Optional;

import com.hardbacknutter.nevertoomanybooks.BaseDBTest;
import com.hardbacknutter.nevertoomanybooks.core.storage.StorageException;
import com.hardbacknutter.nevertoomanybooks.core.utils.PartialDate;
import com.hardbacknutter.nevertoomanybooks.database.dao.TocEntryDao;
import com.hardbacknutter.nevertoomanybooks.entities.Author;
import com.hardbacknutter.nevertoomanybooks.entities.TocEntry;
import com.hardbacknutter.nevertoomanybooks.utils.AppLocale;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

class TocEntryTest
        extends BaseDBTest {

    private static final String ISAAC_ASIMOV = "Isaac Asimov";
    private TocEntryDao tocEntryDao;

    @BeforeEach
    void setup()
            throws StorageException {
        super.setup(AppLocale.SYSTEM_LANGUAGE);

        tocEntryDao = serviceLocator.getTocEntryDao();
    }

    @Test
    void pruneTocEntries01() {
        final Locale bookLocale = Locale.getDefault();

        final Author author0 = Author.from(ISAAC_ASIMOV);
        author0.setId(100);

        final List<TocEntry> list = new ArrayList<>();
        TocEntry tocEntry;

        // keep, position 0, merged with next entry with the same title
        tocEntry = new TocEntry(author0, "title 1");
        tocEntry.setId(1001);
        list.add(tocEntry);

        // keep, position 1
        tocEntry = new TocEntry(author0, "title 2", new PartialDate(2019, 0, 0));
        tocEntry.setId(1002);
        list.add(tocEntry);

        // discard after merging with position 0
        tocEntry = new TocEntry(author0, "title 1", new PartialDate(1978, 0, 0));
        tocEntry.setId(1001);
        list.add(tocEntry);

        // discard in favour of position 1
        tocEntry = new TocEntry(author0, "title 2");
        tocEntry.setId(0);
        list.add(tocEntry);

        final boolean modified = tocEntryDao.pruneList(context, list, false,
                                                       item -> bookLocale,
                                                       (p, l) -> {
                                                       });

        assertTrue(modified, list.toString());
        assertEquals(2, list.size(), list.toString());

        Optional<Integer> fpd;

        // first element is the first "title 1" but with the data from the second entry
        tocEntry = list.get(0);
        assertEquals("title 1", tocEntry.getTitle());
        assertEquals(1001, tocEntry.getId());
        fpd = tocEntry.getFirstPublicationDate().getYear();
        assertTrue(fpd.isPresent());
        assertEquals(1978, (long) fpd.get());

        // second element is the first "title 2"
        tocEntry = list.get(1);
        assertEquals("title 2", tocEntry.getTitle());
        assertEquals(1002, tocEntry.getId());
        fpd = tocEntry.getFirstPublicationDate().getYear();
        assertTrue(fpd.isPresent());
        assertEquals(2019, (long) fpd.get());
    }

    @Test
    void pruneTocEntries02() {
        final Locale bookLocale = Locale.getDefault();

        final Author author0 = Author.from(ISAAC_ASIMOV);
        author0.setId(100);

        final List<TocEntry> list = new ArrayList<>();
        TocEntry tocEntry;

        tocEntry = new TocEntry(1, author0, "title 1", PartialDate.NOT_SET, 0);
        list.add(tocEntry);

        tocEntry = new TocEntry(2, author0, "title 2", new PartialDate(2019, 0, 0), 0);
        list.add(tocEntry);

        tocEntry = new TocEntry(1, author0, "title 1", new PartialDate(1978, 0, 0), 0);
        list.add(tocEntry);

        tocEntry = new TocEntry(2, author0, "title 2", PartialDate.NOT_SET, 0);
        list.add(tocEntry);

        final boolean modified = tocEntryDao.pruneList(context, list, false,
                                                       item -> bookLocale,
                                                       (p, l) -> {
                                                       });

        assertTrue(modified, list.toString());
        assertEquals(2, list.size(), list.toString());

        Optional<Integer> fpd;

        // first element is the first "title 1" but with the data from the second entry
        tocEntry = list.get(0);
        assertEquals(1, tocEntry.getId());
        assertEquals("title 1", tocEntry.getTitle());
        fpd = tocEntry.getFirstPublicationDate().getYear();
        assertTrue(fpd.isPresent());
        assertEquals(1978, (long) fpd.get());

        // second element is the first "title 2"
        tocEntry = list.get(1);
        assertEquals(2, tocEntry.getId());
        assertEquals("title 2", tocEntry.getTitle());
        fpd = tocEntry.getFirstPublicationDate().getYear();
        assertTrue(fpd.isPresent());
        assertEquals(2019, (long) fpd.get());
    }


    @Test
    void pruneTocEntries03() {
        final Locale bookLocale = Locale.getDefault();

        final Author author0 = Author.from(ISAAC_ASIMOV);
        author0.setId(100);

        final List<TocEntry> list = new ArrayList<>();
        TocEntry tocEntry;

        // keep, position 0, but merged with entry 3 -> "1978"
        tocEntry = new TocEntry(1, author0, "title 1", PartialDate.NOT_SET, 0);
        list.add(tocEntry);

        // keep, position 1, merged with entry 4 -> id=2
        tocEntry = new TocEntry(0, author0, "title 2", new PartialDate(2019, 0, 0), 0);
        list.add(tocEntry);

        // dropped, merged with position 0
        tocEntry = new TocEntry(0, author0, "title 1", new PartialDate(1978, 0, 0), 0);
        list.add(tocEntry);

        // dropped, merged with position 1
        tocEntry = new TocEntry(2, author0, "title 2", PartialDate.NOT_SET, 0);
        list.add(tocEntry);

        // keep, position 2, with id reset to 0 because of conflict with position 1
        tocEntry = new TocEntry(2, author0, "title 2", new PartialDate(1880, 0, 0), 0);
        list.add(tocEntry);

        // keep, position 3
        tocEntry = new TocEntry(0, author0, "title 3", new PartialDate(1955, 0, 0), 0);
        list.add(tocEntry);

        // keep, position 4, with id reset to 0 because base data matches position 3
        // but still kept because extra data (the year) is different from position 3
        tocEntry = new TocEntry(4, author0, "title 3", new PartialDate(1965, 0, 0), 0);
        list.add(tocEntry);

        // keep, position 5
        tocEntry = new TocEntry(5, author0, "title 3", new PartialDate(1975, 0, 0), 0);
        list.add(tocEntry);

        final boolean modified = tocEntryDao.pruneList(context, list, false,
                                                       item -> bookLocale,
                                                       (p, l) -> {
                                                       });

        assertTrue(modified, list.toString());
        assertEquals(6, list.size(), list.toString());

        Optional<Integer> fpd;

        // first element is the first "title 1" but with the data from the second entry
        tocEntry = list.get(0);
        assertEquals(1, tocEntry.getId());
        assertEquals("title 1", tocEntry.getTitle());
        fpd = tocEntry.getFirstPublicationDate().getYear();
        assertTrue(fpd.isPresent());
        assertEquals(1978, (long) fpd.get());

        // second element is the first "title 2"
        tocEntry = list.get(1);
        assertEquals(2, tocEntry.getId());
        assertEquals("title 2", tocEntry.getTitle());
        fpd = tocEntry.getFirstPublicationDate().getYear();
        assertTrue(fpd.isPresent());
        assertEquals(2019, (long) fpd.get());

        // this is the third "title 2"
        tocEntry = list.get(2);
        assertEquals(2, tocEntry.getId());
        assertEquals("title 2", tocEntry.getTitle());
        fpd = tocEntry.getFirstPublicationDate().getYear();
        assertTrue(fpd.isPresent());
        assertEquals(1880, (long) fpd.get());

        tocEntry = list.get(3);
        assertEquals(0, tocEntry.getId());
        assertEquals("title 3", tocEntry.getTitle());
        fpd = tocEntry.getFirstPublicationDate().getYear();
        assertTrue(fpd.isPresent());
        assertEquals(1955, (long) fpd.get());

        tocEntry = list.get(4);
        assertEquals(4, tocEntry.getId());
        assertEquals("title 3", tocEntry.getTitle());
        fpd = tocEntry.getFirstPublicationDate().getYear();
        assertTrue(fpd.isPresent());
        assertEquals(1965, (long) fpd.get());

        tocEntry = list.get(5);
        assertEquals(5, tocEntry.getId());
        assertEquals("title 3", tocEntry.getTitle());
        fpd = tocEntry.getFirstPublicationDate().getYear();
        assertTrue(fpd.isPresent());
        assertEquals(1975, (long) fpd.get());
    }
}
