package IHM;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Image;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.JButton;
import javax.swing.ScrollPaneConstants;

import org.eclipse.emf.ecore.EObject;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.IOException;

import code.StaticMap;
import code.LoadSaveModel;

/**
 * Maps Viewer Tool
 *  
 * @author Paola Vallejo
 *
 */
public class MapsViewer extends JFrame {
	private JTextField textFieldModel;
	private JLabel lblMap;
	private JTextField txtScale = new JTextField();
	private JTextField txtZoom = new JTextField();

	private StaticMap ObjStaticMaps=new StaticMap();

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

	/**
	 * Create the frame.
	 */
	public MapsViewer() {
		setTitle("Maps Viewer");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(200, 200, 610, 550);

		JPanel panel = new JPanel();
		getContentPane().add(panel, BorderLayout.NORTH);

		JLabel lblModel = new JLabel("Model: ");
		panel.add(lblModel);

		textFieldModel = new JTextField();
		panel.add(textFieldModel);
		textFieldModel.setColumns(20);
		textFieldModel.setText("C:/ModifProject/MapViewer/model/BrestCity.mapview.xmi");

		JButton btnOpen = new JButton("Open");
		btnOpen.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {			
				JFileChooser choix = new JFileChooser();
				int retour=choix.showOpenDialog(null);
				if(retour==JFileChooser.APPROVE_OPTION){				   
					textFieldModel.setText(choix.getSelectedFile().getAbsolutePath());			  
				}else {

				}	
			}
		});
		
		panel.add(btnOpen);
		
		JLabel lblScale = new JLabel("Scale:");
		panel.add(lblScale);
		
		txtScale = new JTextField();
		panel.add(txtScale);
		txtScale.setColumns(2);
		txtScale.setText("1");
		
		JLabel lblZoom = new JLabel("Zoom:");
		panel.add(lblZoom);		
		txtZoom = new JTextField();
		panel.add(txtZoom);
		txtZoom.setColumns(2);
		txtZoom.setText("12");

		JButton btnShow = new JButton("ShowMap");
		btnShow.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(textFieldModel.getText().isEmpty()){
					JOptionPane.showMessageDialog(null, "Select a model");
				}else{
					try {
						createMap();

					} catch (NumberFormatException e) {
						e.printStackTrace();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		});
		panel.add(btnShow);

		JScrollPane scrollPaneMap = new JScrollPane();
		scrollPaneMap.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		scrollPaneMap.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
		getContentPane().add(scrollPaneMap, BorderLayout.CENTER);

		lblMap = new JLabel("Map");
		lblMap.setHorizontalAlignment(SwingConstants.CENTER);
		scrollPaneMap.setViewportView(lblMap);
	}

	/**
	 * Creates a map
	 * 
	 * @throws NumberFormatException
	 * @throws IOException
	 */
	private void createMap() throws NumberFormatException, IOException{
		lblMap.setText("");
		LoadSaveModel load = new LoadSaveModel();
		EObject inputModel = load.loadModel(textFieldModel.getText(), "metamodel/mapview.ecore");
		int zoom = Integer.parseInt(txtZoom.getText());
		if (zoom < 1){
			zoom = 1;
		}else if(zoom > 15){
			zoom = 15;
		}
		
		int scale = Integer.parseInt(txtScale.getText());
		if(scale < 1){
			scale = 1;
		}else if(scale >2){
			scale = 2;
		}
		
		Image mapImage=ObjStaticMaps.getStaticMap(inputModel,
				zoom ,new Dimension(600,600),
				scale);
		if(mapImage!=null){
			ImageIcon imgIcon=new ImageIcon(mapImage);
			Icon iconImage=(Icon)imgIcon;
			lblMap.setIcon(iconImage);
		}
	}

}
