package soa.jaxrslabs.booktrainclientwebservicerestexercice2;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.ws.rs.core.MediaType;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.GenericType;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;

public class BookTrainClientMain extends JFrame {

	private static final long serialVersionUID = -6298739485988198268L;

	private WebResource service;

	public BookTrainClientMain() {
		super("Client Réservation Train");

		this.initializeService();
		this.setLayout(new GridLayout(3, 1, 10, 10));
		JPanel panelTrains = new JPanel();
		panelTrains.setLayout(new GridLayout(1, 1));

		JButton getTrains = new JButton("GetTrains");
		panelTrains.add(getTrains);
		getTrains.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				callGetTrains();
			}
		});

		JPanel panelBookTrains = new JPanel();
		panelBookTrains.setLayout(new GridLayout(1, 1));

		JButton getBookTrains = new JButton("GetBookTrains");
		panelBookTrains.add(getBookTrains);
		getBookTrains.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				callGetBookTrains();
			}
		});
		JPanel createBookTrainFormPanel = new JPanel();
		createBookTrainFormPanel.setLayout(new GridLayout(1, 3));
		final JTextField numTrain = new JTextField();
		final JTextField numberPlaces = new JTextField();
		JButton createBookTrains = new JButton("CreateBookTrain");
		createBookTrains.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				createBookTrains(numTrain.getText(), numberPlaces.getText());
			}
		});
		createBookTrainFormPanel.add(createBookTrains);
		createBookTrainFormPanel.add(numTrain);
		createBookTrainFormPanel.add(numberPlaces);

		this.add(panelTrains);
		this.add(panelBookTrains);
		this.add(createBookTrainFormPanel);

		this.pack();
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setVisible(true);
	}

	private void initializeService() {
		ClientConfig clientConfig = new DefaultClientConfig();
		Client client = Client.create(clientConfig);
		service = client
				.resource("http://localhost:8083/BookTrainWebServiceRestExercice1/");
	}

	private void callGetTrains() {
		List<Train> listOfTrainsResult = service.path("trains")
				.accept(MediaType.TEXT_XML).get(new GenericType<List<Train>>() {
				});
		for (Train train : listOfTrainsResult) {
			System.out.println(train.getNumTrain() + " - "
					+ train.getVilleDepart() + " - " + train.getVilleArrivee()
					+ " - " + train.getHeureDepart());
		}
	}

	private void callGetBookTrains() {
		List<BookTrain> listOfBookTrainsResult = service
				.path("trains/booktrains").accept(MediaType.TEXT_XML)
				.get(new GenericType<List<BookTrain>>() {
				});
		for (BookTrain bookTrain : listOfBookTrainsResult) {
			System.out.println(bookTrain.getNumBook() + " - "
					+ bookTrain.getNumberPlaces() + " - "
					+ bookTrain.getCurrentTrain().getNumTrain());
		}
	}

	private void createBookTrains(String numTrain, String numberPlaces) {
		String numBookTrainResult = service.path("trains/booktrains")
				.queryParam("numTrain", numTrain)
				.queryParam("numberPlaces", numberPlaces).post(String.class);
		System.out.println(numBookTrainResult + " created");
	}

	public static void main(String[] args) {
		new BookTrainClientMain();
	}
}
