import animals.Animal;
import animals.Background;

import modules.AnimalModule;
import modules.Createobject;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;

public class DrawPanel extends JPanel implements ActionListener, MouseMotionListener {
    private final int PANEL_WIDTH;
    private final int PANEL_HEIGHT;
    private final int TIMER_DELAY;
    private Timer timer;
    private BufferedImage backgroundImage;
    private ArrayList<Animal> animals; // List of animals
    private ArrayList<Background> backgrounds; // List of animals
    private int ticksFromStart = 0;
    private KeyHandler keyHandler;

    public DrawPanel(final int width, final int height, final int timerDelay, List<AnimalModule> modules) {
        this.PANEL_WIDTH = width;
        this.PANEL_HEIGHT = height;
        this.TIMER_DELAY = timerDelay;

        timer = new Timer(timerDelay, this);
        timer.start();
        this.setFocusable(true);
        this.requestFocusInWindow();

        this.keyHandler = new KeyHandler();

        this.addKeyListener(keyHandler);


        setBackground(Color.green);

        animals = new ArrayList<>(); // Initialize the list of animals
        backgrounds = new ArrayList<>();

        Createobject.createAnimals(animals, getBackground(), modules);
        Createobject.createBackground(backgrounds, getBackground(), modules);

        // Add mouse motion listener
        addMouseMotionListener(this);
    }

    @Override
    protected void paintComponent(final Graphics gr) {
        super.paintComponent(gr);
        Graphics2D g2d = (Graphics2D) gr;
        if (backgroundImage != null) {
            g2d.drawImage(backgroundImage, 0, 0, getWidth(), getHeight(), null);
        } else {
            // Fill the background with the panel's color if the image is unavailable
            g2d.setColor(getBackground());
            g2d.fillRect(0, 0, getWidth(), getHeight());
        }

        for (Background background : backgrounds) {
            background.draw(g2d);
        }

        for (Animal animal : animals) {
            animal.draw(g2d);
        }
    }


    @Override
    public void actionPerformed(final ActionEvent e) {
        if (e.getSource() == timer) {
            ticksFromStart += 2;
            repaint();
        }
        if (keyHandler.isLeftPressed()) {
            for (Background background : backgrounds) {
                background.leftMove();
            }
            repaint();
        }
        if (keyHandler.isRightPressed()) {
            for (Background background : backgrounds) {
                background.rightMove();
            }
            repaint();
        }

    }

    @Override
    public void mouseDragged(MouseEvent e) {
        // Optional: Implement if needed
    }

    @Override
    public void mouseMoved(MouseEvent e) {
        for (Animal animal : animals) {
            if (animal.getMove()) {
                animal.setPosition(e.getX(), e.getY());
                updateAnimalsLookingAt(animal);
            }
        }
        repaint();
    }

    private void updateAnimalsLookingAt(Animal forMouseAnimals) {
        // Update the direction animals are looking at the sparrow
        for (Animal animal : animals) {
            if (animal.getLookAt()) {
                animal.lookAt(forMouseAnimals.getXCenter(), forMouseAnimals.getYCenter());
            }
        }
    }


}  я хочу, чтобы пользователь мог инициализировать объекты во время работы программы