package edu.school.galery;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import net.miginfocom.swing.MigLayout;
import java.awt.Color;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;

import javax.swing.JScrollPane;
import javax.swing.ListModel;
import javax.swing.JList;
import javax.swing.JButton;
import javax.imageio.ImageIO;
import javax.swing.AbstractListModel;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.awt.event.ActionEvent;
import java.awt.Dimension;
import javax.swing.JSlider;
import javax.swing.ListSelectionModel;
/**
 * Информатика, 8 клас, издателство Просвета, 2017
 * Автори: Светла Бойчева, Николина Николова, Елиза Стефанова, Антон Денев
 * Урок 3.4.2.3. Работа с изображения
 * Задача: С времето RxC001 започва да забравя какъв
 * обект и къде е снимал.
 * До списъчното поле с имената на графичните
 * файлове добавете списъчно поле, съдържащо
 * кратки текстове – заглавия на изображенията.
 * Извеждайте заглавията в горната
 * част на панела върху изображението.
 * 
 * Вариант 1.
 */
public class GaleryForm extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JLabel lblSubtitles;
	private JSlider sldTime;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					GaleryForm frame = new GaleryForm();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public GaleryForm() {
		setTitle("\u0413\u0430\u043B\u0435\u0440\u0438\u044F");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 645, 400);
		contentPane = new JPanel();
		contentPane.setForeground(new Color(51, 102, 102));
		contentPane.setBackground(new Color(255, 255, 204));
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new MigLayout("", "[][20%,trailing][20%,trailing][grow 50,right]", "[][grow][][][][]"));

		JLabel lblPictures = new JLabel("\u0421\u043D\u0438\u043C\u043A\u0438");
		lblPictures.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblPictures.setForeground(new Color(0, 102, 102));
		contentPane.add(lblPictures, "cell 0 0 2 1");

		JScrollPane scrlPaneImages = new JScrollPane();
		contentPane.add(scrlPaneImages, "cell 1 1,grow");

		JList<String> lstImages = new JList<String>();
		lstImages.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		lstImages.setModel(new AbstractListModel<String>() {
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;
			String[] values = new String[] {"IMAG1452.jpg", "IMAG1455.jpg", "IMAG1458.jpg", "IMAG1488.jpg", "IMAG1491.jpg"};
			public int getSize() {
				return values.length;
			}
			public String getElementAt(int index) {
				return values[index];
			}
		});
		scrlPaneImages.setViewportView(lstImages);
		
		JScrollPane scrlPaneTitles = new JScrollPane();
		contentPane.add(scrlPaneTitles, "cell 2 1,grow");
		
		JList<String> lstTistles = new JList<String>();
		lstTistles.setModel(new AbstractListModel<String>() {
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;
			String[] values = new String[] {"\u041D\u0430\u0447\u0430\u043B\u043E\u0442\u043E", "\u041C\u0438\u043D\u0438-\u0432\u043E\u0434\u043E\u043F\u0430\u0434\u0438", "\u0412\u043D\u0438\u043C\u0430\u043D\u0438\u0435! \u041F\u0430\u0434\u0430\u0442 \u043A\u0430\u043C\u044A\u043D\u0438!", "\u041F\u043E\u0434 \u0432\u043E\u0434\u043E\u043F\u0430\u0434 \u0421\u043A\u0430\u043A\u043B\u044F", "\u0427\u0430\u0441 \u0438 \u043F\u043E\u043B\u043E\u0432\u0438\u043D\u0430 \u043A\u0440\u0430\u0441\u043E\u0442\u0430!"};
			public int getSize() {
				return values.length;
			}
			public String getElementAt(int index) {
				return values[index];
			}
		});
		scrlPaneTitles.setViewportView(lstTistles);

		JPanel pnlPreview = new JPanel();
		pnlPreview.setPreferredSize(new Dimension(480, 270));
		pnlPreview.setSize(new Dimension(480, 270));
		pnlPreview.setBackground(new Color(255, 255, 255));
		contentPane.add(pnlPreview, "cell 3 1");

		JButton btnShow = new JButton("\u041F\u043E\u043A\u0430\u0436\u0438");
		btnShow.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Graphics g = pnlPreview.getGraphics();
				g.setColor(Color.WHITE);

				ListModel<String> model = lstImages.getModel();
				ListModel<String> titlesModel = lstTistles.getModel();
				int size = model.getSize();
				
				final String PATH = "images/";
				Image img;

				for (int i = 0; i < size; i++) {
					try {
						String fileName = PATH + model.getElementAt(i);
						File imageFile = new File(fileName);
						img = ImageIO.read(imageFile);

						g.drawImage(img, 0, 0, null);
						g.drawString(titlesModel.getElementAt(i), 20, 20);
						
						Thread.sleep(sldTime.getValue());
						
					} catch (IOException e1) {
						e1.printStackTrace();
					} catch (InterruptedException e1) {
						e1.printStackTrace();
					}
				}

			}
		});
		
		lblSubtitles = new JLabel("");
		lblSubtitles.setFont(new Font("Tahoma", Font.PLAIN, 12));
		contentPane.add(lblSubtitles, "cell 3 2,alignx center");
		
		sldTime = new JSlider();
		sldTime.setForeground(new Color(51, 102, 102));
		sldTime.setBackground(new Color(255, 255, 204));
		sldTime.setPaintLabels(true);
		sldTime.setPaintTicks(true);
		sldTime.setMajorTickSpacing(500);
		sldTime.setMaximum(5000);
		contentPane.add(sldTime, "cell 3 3,growx");
		contentPane.add(btnShow, "flowx,cell 1 5");

		JButton btnExit = new JButton("\u0418\u0437\u0445\u043E\u0434");
		btnExit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		contentPane.add(btnExit, "cell 3 5");
	}

}
