package de.thpeetz.kontor.comics.views;

import static org.junit.jupiter.api.Assertions.*;

import java.util.concurrent.atomic.AtomicReference;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import de.thpeetz.kontor.comics.data.Artist;

@SpringBootTest
class ArtistformTest {

    private Artist artist1;
    private static final String ARTISTNAME= "Lee, Stan";

    @BeforeEach
    void setupData() {
        artist1 = new Artist();
        artist1.setName(ARTISTNAME);
    }

    @Test
    void formFieldsPopulated() {
        ArtistForm form = new ArtistForm();
        form.setArtist(artist1);
        assertEquals(ARTISTNAME, form.name.getValue());
    }

    @Test
    void saveEventHasCorrectValues() {
        ArtistForm form = new ArtistForm();
        Artist artist = new Artist();
        form.setArtist(artist);
        form.name.setValue(ARTISTNAME);

        AtomicReference<Artist> savedArtistReference = new AtomicReference<>(null);
        form.addSaveListener(e -> {
            savedArtistReference.set(e.getArtist());
        });
        form.save.click();
        Artist savedArtist = savedArtistReference.get();
        assertEquals(ARTISTNAME, savedArtist.getName());
    }

    @Test
    void deleteEventHasCorrectValues() {
        ArtistForm form = new ArtistForm();
        Artist artist = new Artist();
        form.setArtist(artist);
        form.name.setValue(ARTISTNAME);

        AtomicReference<Artist> deletedArtistReference = new AtomicReference<>(null);
        form.addDeleteListener(e -> {
            deletedArtistReference.set(e.getArtist());
        });
        form.delete.click();
        Artist deletedArtist = deletedArtistReference.get();
        assertEquals(ARTISTNAME, deletedArtist.getName());
    }
}
