

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import api.TMDBAPI;
import api.TMDBAPICallBack;
import api.TMDBFilm;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;
import java.awt.GridLayout;

public class MaJFrame extends JFrame implements TMDBAPICallBack {

	private JPanel contentPane;
	private JPanelImage panel_im;
	private JButton btnNewButton;
	private JTextField textField;
	private TMDBAPI api=null;
	private JTextField textField_1;
	private JPanel panel_1;
	private JPanelImage panel_im_2;
	
	
	/**
	 * 
	 * Create the frame.
	 */
	public MaJFrame() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 500, 500);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		JPanel panel = new JPanel();
		contentPane.add(panel, BorderLayout.NORTH);
		
		btnNewButton = new JButton("get Films");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				clicBouton();
			}
		});
		
		textField = new JTextField();
		textField.setText("550");
		panel.add(textField);
		textField.setColumns(5);
		
		textField_1 = new JTextField();
		textField_1.setText("650");
		panel.add(textField_1);
		textField_1.setColumns(5);
		panel.add(btnNewButton);
		
		panel_1 = new JPanel();
		contentPane.add(panel_1, BorderLayout.CENTER);
		panel_1.setLayout(new GridLayout(0, 2, 0, 0));
		
		panel_im = new JPanelImage();
		panel_1.add(panel_im);
		
		panel_im_2 = new JPanelImage();
		panel_1.add(panel_im_2);
		
		api = new TMDBAPI(this);
	}

	protected void clicBouton() {
		int idFilm=550;
		if (textField.getText().length()>1) idFilm = Integer.parseInt(textField.getText());
		if (api!=null) api.getFilm(idFilm,1);
		
		idFilm=650;
		if (textField_1.getText().length()>1) idFilm = Integer.parseInt(textField_1.getText());
		if (api!=null) api.getFilm(idFilm,2);
	}

	@Override
	public void retourAPI(TMDBFilm theMovie,int numCB) {
		if (theMovie!=null) {
			if (numCB==1) {
				panel_im.setImage(theMovie.getPoster_image());
			}
			if (numCB==2) {
				panel_im_2.setImage(theMovie.getPoster_image());
			}
			System.out.println("Affichage : "+numCB+" Film : "+theMovie);
		}
		this.repaint();
	}

}
