package presentation;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JSlider;
import controller.*;

import abstraction.Album;

public class AlbumPhoto  extends JFrame {
	private static final long serialVersionUID = 1L;
	public Album album;

	public AlbumPhoto(Album album){
		super("Album photo");
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.album = album;
		creerCentre();
		creerSlider();
		creerListe();
		creerBandeauBas();
		creerBoutonsHaut();
		creerMenu();
		pack();
		this.setVisible(true);
	}

	private void creerCentre() {
		JLabel label;
		JPanel centre = new JPanel(new FlowLayout(FlowLayout.LEFT,10,10));
		label = new JLabel(album.getPhotoCourante());
		centre.setMaximumSize(new Dimension(600, 500));
		ControlJLabel control = new ControlJLabel(label);
		this.album.addObserver(control);
		centre.add(label);
		this.getContentPane().add(centre,BorderLayout.CENTER);
	}

	public void creerSlider(){
		JSlider slide = new JSlider(JSlider.VERTICAL, 0,500, 100);
		slide.setMinorTickSpacing(10);
		slide.setMajorTickSpacing(100);
		slide.setPaintTicks(true);
		slide.setPaintTrack(true);
		slide.setPaintLabels(true);
		ControlSlider controlSlide = new ControlSlider(slide,album);
		slide.addChangeListener(controlSlide);
		this.album.addObserver(controlSlide);
		this.getContentPane().add(slide,BorderLayout.EAST);
	}
	
	public void creerListe(){
		JList<String> liste = new JList<String>();
		String[] list = new String[this.album.getSize()];
		int i;
		for (i=0;i<this.album.getSize();i++){
			list[i]=this.album.getPhoto(i).getNom();
		}
		liste.setListData(list);
		liste.setSelectedIndex(0);
		ControlList controlList = new ControlList(liste,album);
		this.album.addObserver(controlList);
		liste.addListSelectionListener(controlList);
		this.getContentPane().add(liste,BorderLayout.WEST);
	}
	
	@SuppressWarnings("deprecation")
	public void creerBandeauBas(){
		JPanel panelBas = new JPanel();
		panelBas.setSize(80, 480);
		List<JButton> boutons = new ArrayList<JButton>();
		panelBas.setLayout(new GridLayout(1,this.album.getSize()));
		int i;
		for (i=0;i<this.album.getSize();i++){
			boutons.add(new JButton(this.album.getPhoto(i)));
		}
		boutons.get(0).setEnabled(false);
		for (JButton B : boutons){
			B.setPreferredSize(new Dimension(80,80));
			B.setMaximumSize(B.getPreferredSize());
			B.setBorder(null);
			B.addActionListener(new ControlBas(B,album,boutons));
			this.album.addObserver(new ControlBas(B,album,boutons));
			panelBas.add(B);
		}
		panelBas.resize(20,140);
		this.getContentPane().add(panelBas,BorderLayout.SOUTH);
	}
	
	
	public  void creerBoutonsHaut(){
		JPanel panelHaut = new JPanel();
		JButton boutongauche = new JButton("précédant");
		JButton boutondroite = new JButton("suivant");
		panelHaut.add(boutongauche);
		panelHaut.add(boutondroite);
		boutongauche.setEnabled(false);
		boutongauche.addActionListener(new ControlHaut(boutongauche,album, 1));
		boutondroite.addActionListener(new ControlHaut(boutondroite,album, 2));
		this.album.addObserver(new ControlHaut(boutongauche,album, 1));
		this.album.addObserver(new ControlHaut(boutondroite,album, 2));
		this.getContentPane().add(panelHaut,BorderLayout.NORTH);
		
	}
	
	private void creerMenu() {
		JMenuBar menu = new JMenuBar();
		JMenu menu1 = new JMenu("Fichier");
		JMenuItem menu2 = new JMenuItem("Ajouter photo");
		menu1.add(menu2);
		menu2.addActionListener(new ControlMenu(menu2,album));
		menu.add(menu1);
		this.setJMenuBar(menu);
		
	}
	
	
	
}
