package galerie.view;

import galerie.model.ImageList;
import galerie.control.HalfClickNavigator;

import javax.swing.*;
import java.awt.*;

public class GalleryReplacementFrame extends JFrame {
    private final ImageList images;
    private final JLabel viewer = new JLabel("", SwingConstants.CENTER);

    public GalleryReplacementFrame(ImageList images) {
        super("Galerie — Remplacement d'un seul JLabel");
        this.images = images;
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setSize(800, 600);
        setLocationRelativeTo(null);

        viewer.setVerticalAlignment(SwingConstants.CENTER);
        viewer.setIcon(images.currentIcon());

        JPanel surface = new JPanel(new BorderLayout());
        surface.add(viewer, BorderLayout.CENTER);
        surface.addMouseListener(new HalfClickNavigator(surface, this::next, this::previous));

        setContentPane(surface);
    }

    private void next() {
        images.next();
        viewer.setIcon(images.currentIcon());
    }

    private void previous() {
        images.previous();
        viewer.setIcon(images.currentIcon());
    }
}
