package com.dynamicwallpaper.gui;

import com.dynamicwallpaper.util.ThumbnailLoader;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.io.File;

/** Lets the user pick exactly one image for Light Mode and one for Dark Mode. */
public final class AppearancePanel extends JPanel {

    private final ImagePickerCard lightCard = new ImagePickerCard("Light Mode Image", "Shown when the Mac uses Light appearance.");
    private final ImagePickerCard darkCard = new ImagePickerCard("Dark Mode Image", "Shown when the Mac uses Dark appearance.");

    public AppearancePanel() {
        setLayout(new GridLayout(1, 2, 16, 0));
        setBorder(BorderFactory.createEmptyBorder(16, 16, 16, 16));
        add(lightCard);
        add(darkCard);
    }

    public File lightImage() {
        return lightCard.file();
    }

    public File darkImage() {
        return darkCard.file();
    }

    /** A single "choose an image" card with a thumbnail and file name. */
    private static final class ImagePickerCard extends JPanel {
        private File file;
        private final JLabel thumbnailLabel = new JLabel("", SwingConstants.CENTER);
        private final JLabel fileNameLabel = new JLabel("No image selected", SwingConstants.CENTER);

        ImagePickerCard(String title, String subtitle) {
            setLayout(new BorderLayout(8, 8));
            setBorder(BorderFactory.createCompoundBorder(
                    BorderFactory.createTitledBorder(title),
                    BorderFactory.createEmptyBorder(8, 8, 8, 8)));

            JLabel subtitleLabel = new JLabel(subtitle, SwingConstants.CENTER);
            subtitleLabel.setEnabled(false);

            thumbnailLabel.setPreferredSize(new java.awt.Dimension(220, 220));
            thumbnailLabel.setBorder(BorderFactory.createDashedBorder(null));

            JButton chooseButton = new JButton("Choose Image…");
            chooseButton.addActionListener(e -> choose());

            JPanel top = new JPanel(new BorderLayout());
            top.add(subtitleLabel, BorderLayout.NORTH);
            top.add(thumbnailLabel, BorderLayout.CENTER);

            JPanel bottom = new JPanel(new GridLayout(2, 1));
            bottom.add(fileNameLabel);
            bottom.add(chooseButton);

            add(top, BorderLayout.CENTER);
            add(bottom, BorderLayout.SOUTH);
        }

        private void choose() {
            JFileChooser chooser = new JFileChooser();
            chooser.setFileFilter(new FileNameExtensionFilter(
                    "Images", "png", "jpg", "jpeg", "tif", "tiff", "bmp", "gif", "heic"));
            if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
                file = chooser.getSelectedFile();
                fileNameLabel.setText(file.getName());
                thumbnailLabel.setIcon(ThumbnailLoader.thumbnail(file, 200));
                thumbnailLabel.setText(null);
            }
        }

        File file() {
            return file;
        }
    }
}
