package ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.border.EmptyBorder;
import javax.swing.filechooser.FileNameExtensionFilter;

import backend.*;

public class Window extends JFrame {
	
	private static final long serialVersionUID = 3127616044375965020L;
	private ImageJPanel topPanel;
	private JPanel prefsPanel;
	private JPanel playlistPane;
	private JScrollPane jsp;
	private JButton syncBtn;
	private JTextField itunesLibLocation;
	private JTextField mobileLibLocation;
	private JRadioButton syncAll;
	private JRadioButton syncSelected;
	private List<JCheckBox> playlists;
	private JLabel statusMsg;
	private JProgressBar progress;
	private JPanel errorPanelMobi;
	private JPanel errorPanelItunes;
	private JLabel diskSpaceLabel;
	private JLabel errorLabel;
	
	private LibSync backend;
	
	private static final ImageIcon WARNING_ICON = new ImageIcon("warning.png");
	private static final ImageIcon ERROR_ICON = new ImageIcon("error.png");
	private static final Dimension WINDOW_SIZE = new Dimension(500,450);
	private static final Color PREFSPANEL_BG = new Color(250,250,250);

	public Window() {
		super("Unnamed iTunes Library Synctool");
		this.playlists = Collections.synchronizedList(new ArrayList<JCheckBox>());
		this.setPreferredSize(WINDOW_SIZE);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		this.setupBackEnd();
		
		this.setIconImage(new ImageIcon("ilibsynclogo.png").getImage());
		this.setContentPane(new JPanel());
		this.getContentPane().setLayout(new BorderLayout());
		this.setupBottomPanel();
		this.setupPrefsPanel();
		//this.setupMenu();
		
		this.analysisReady(this.backend.analysisReady(),!this.backend.analysisNotSuccessful());
		
		this.pack();
		this.setLocationRelativeTo(null);
		this.setVisible(true);
		
	}
	
	private void setupBackEnd() {
		try {
			if (this.backend == null) {
				this.backend = new LibSync();
			}
			//System.out.println("Setting analysis listener");
			this.backend.setAnalysisReadyListener(new ActionListener() {
				public void actionPerformed(ActionEvent ae) {
					//System.out.println("Invoking later...");
					SwingUtilities.invokeLater(new Runnable() {
						public void run() {
							//System.out.println("Analysis complete!");
							Window.this.analysisReady(Window.this.backend.analysisReady(),
									!Window.this.backend.analysisNotSuccessful());
							Window.this.pack();
							Window.this.validate();
						}
					});
				}
			});
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this, "Error: \n"+e.getLocalizedMessage(), "Error", JOptionPane.ERROR_MESSAGE);
		}
	}
	
	private void setupBottomPanel() {
		this.topPanel = new ImageJPanel();
		//java.net.URL imgURL = getClass().getResource("/topgradient.png");
		//this.topPanel.setBgImage(Toolkit.getDefaultToolkit().createImage(imgURL));
		this.topPanel.setBgImage(new ImageIcon("topgradient.png").getImage());
		this.syncBtn = new JButton("Sync");
		this.syncBtn.addActionListener(new ActionListener() {
			private Timer timer;
			
			public void actionPerformed(ActionEvent ae) {
				
				new Thread() {
					public void run() {
						try {
							Window.this.backend.syncMobile();
							Window.this.backend.reanalyze();
							Window.this.updateProgressBarAndStatus();
							Window.this.updateDiskSpaceData(true);
						} catch (IOException e) {
							JOptionPane.showMessageDialog(Window.this, e.getLocalizedMessage(), "Error", JOptionPane.ERROR_MESSAGE);
						}
					}
				}.start();
				ActionListener al = new ActionListener() {

					public void actionPerformed(ActionEvent arg0) {
						Window.this.updateProgressBarAndStatus();
						if (Window.this.backend.getAnalysisState() == LibSync.LIBSTATE_SYNC_OK) {
							timer.stop();
						}
					}
					
				};
				this.timer = new Timer(100,al);
				this.timer.start();
			}
		});
		//this.syncBtn.setPreferredSize(new Dimension(60,60));
		//this.syncBtn.setMargin(new Insets(1,1,1,1));
		this.syncBtn.setText("Sync");
		this.syncBtn.setAlignmentX(RIGHT_ALIGNMENT);
		
		JPanel diskSpacePanel = new JPanel();
		diskSpacePanel.setOpaque(false);
		diskSpacePanel.setPreferredSize(new Dimension(150,60));
		diskSpacePanel.setBorder(new EmptyBorder(10,5,10,5));
		
		this.diskSpaceLabel = new JLabel();
		this.diskSpaceLabel.setText("<html>Bla<p>Bla</html>");
		diskSpacePanel.add(this.diskSpaceLabel);
		
		// progress bar and status msg
		JPanel progresspanel = new JPanel();
		progresspanel.setOpaque(false);
		progresspanel.setPreferredSize(new Dimension(250,60));
		progresspanel.setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		
		this.topPanel.add(diskSpacePanel,c);
		c.gridx +=2;
		
		if (this.progress == null) {
			this.progress = new JProgressBar();
		}
		this.statusMsg = new JLabel();
		if (this.backend != null) {
			this.updateProgressBarAndStatus();
		}
		this.statusMsg.setText("Analyzing files...");
		//c.gridwidth = 3;
		
		//progresspanel.add(new JSeparator(),c);
		//c.gridy += 2;
		c.gridx = 0;
		c.gridwidth = 1;
		progresspanel.add(this.progress,c);
		c.gridy+=2;
		//c.gridy++;
		progresspanel.add(this.statusMsg,c);
		//c.gridy++;
		
		this.topPanel.add(progresspanel);
		this.topPanel.add(this.syncBtn);
		
		this.getContentPane().add(this.topPanel,BorderLayout.SOUTH);
	}
	
	private void setupPrefsPanel() {
		this.prefsPanel = new JPanel(new GridBagLayout());
		this.prefsPanel.setBackground(Window.PREFSPANEL_BG);
		
		// focus listener for textfields
		FocusListener f = new FocusAdapter() {
			public void focusLost(FocusEvent fe) {
				if (!itunesLibLocation.getText().equals( 
					Window.this.backend.getSetting(Settings.ITUNES_LIBRARY)) ||
					!mobileLibLocation.getText().equals(
							Window.this.backend.getSetting(Settings.MOBILE_LIBRARY_DIR))) {
				Window.this.fireLibraryLocationDataChanged(itunesLibLocation.getText(),
						mobileLibLocation.getText()); 
				}
			}
		};
		
		// contents
		GridBagConstraints c = new GridBagConstraints();
		
		c.gridy += 20;
		
		c.gridy -= 20;
		
		c.anchor = GridBagConstraints.NORTHWEST;
		JLabel itllDesc = new JLabel();
		c.gridwidth = 2;
		c.gridy++;
		itllDesc.setText("iTunes library Location");
		this.prefsPanel.add(itllDesc, c);
		c.gridy++;
		this.itunesLibLocation = new JTextField();
		this.itunesLibLocation.setText(this.backend.getSetting(Settings.ITUNES_LIBRARY));
		this.itunesLibLocation.setPreferredSize(new Dimension(300,25));
		this.itunesLibLocation.addFocusListener(f);
		this.prefsPanel.add(this.itunesLibLocation, c);
		
		c.gridx+=3;
		c.gridwidth = 1;
		JButton browselib = new JButton();
		browselib.setText("Browse");
		browselib.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				JFileChooser filechooser = new JFileChooser(Window.this.backend.getSetting(Settings.ITUNES_LIBRARY));
				filechooser.setFileFilter(new FileNameExtensionFilter("iTunes library XML file (*.xml)","xml"));
				filechooser.showDialog(Window.this, "Select");
				if (filechooser.getSelectedFile() != null) {
					Window.this.backend.setSetting(Settings.ITUNES_LIBRARY,filechooser.getSelectedFile().getAbsolutePath());
					Window.this.itunesLibLocation.setText(Window.this.backend.getSetting(Settings.ITUNES_LIBRARY));
					Window.this.fireLibraryLocationDataChanged(itunesLibLocation.getText(),
							mobileLibLocation.getText()); 
				}
			}
		});
		this.prefsPanel.add(browselib,c);
		
		if (this.errorPanelItunes == null) {
			this.errorPanelItunes = new JPanel();
		}
		this.errorPanelItunes.setPreferredSize(new Dimension(25,25));
		this.errorPanelItunes.setBackground(Window.PREFSPANEL_BG);
		
		c.gridwidth = 1;
		c.gridx++;
		this.prefsPanel.add(this.errorPanelItunes,c);
		
		c.gridwidth = 2;
		c.gridx-=4;
		
		c.gridy++;
		JSeparator sep = new JSeparator();
		this.prefsPanel.add(sep, c);
		
		c.gridy++;
		JLabel mllDesc = new JLabel();
		mllDesc.setText("Mobile library directory");
		this.prefsPanel.add(mllDesc, c);
		c.gridy++;
		this.mobileLibLocation = new JTextField();
		this.mobileLibLocation.setText(this.backend.getSetting(Settings.MOBILE_LIBRARY_DIR));
		this.mobileLibLocation.setPreferredSize(new Dimension(300,25));
		this.mobileLibLocation.addFocusListener(f);
		this.prefsPanel.add(this.mobileLibLocation, c);
		
		c.gridx+=3;
		c.gridwidth = 1;
		JButton browsemobilib = new JButton();
		browsemobilib.setText("Browse");
		
		browsemobilib.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				JFileChooser filechooser = new JFileChooser(Window.this.backend.getSetting(Settings.MOBILE_LIBRARY_DIR));
				filechooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
				filechooser.showDialog(Window.this, "Select");
				if (filechooser.getSelectedFile() != null)
					Window.this.backend.setSetting(Settings.MOBILE_LIBRARY_DIR,filechooser.getSelectedFile().getAbsolutePath());
				Window.this.mobileLibLocation.setText(Window.this.backend.getSetting(Settings.MOBILE_LIBRARY_DIR));
				Window.this.fireLibraryLocationDataChanged(itunesLibLocation.getText(),
						mobileLibLocation.getText()); 
			}
		});
		
		this.prefsPanel.add(browsemobilib,c);
		
		if (this.errorPanelMobi == null) {
			this.errorPanelMobi = new JPanel();
		}
		this.errorPanelMobi.setPreferredSize(new Dimension(25,25));
		this.errorPanelMobi.setBackground(Window.PREFSPANEL_BG);
		
		c.gridwidth = 1;
		c.gridx++;
		this.prefsPanel.add(this.errorPanelMobi,c);
		
		c.gridwidth = 2;
		c.gridx-=4;
		
		c.gridy++;
		JSeparator sep2 = new JSeparator();
		this.prefsPanel.add(sep2, c);
		
		c.gridy++;
		Insets i = c.insets;
		c.insets = new Insets(10,5,10,5);
		if (this.errorLabel == null) {
			this.errorLabel = new JLabel();
		}
		this.errorLabel.setText(" ");
		this.prefsPanel.add(this.errorLabel, c);
		c.insets = i;
		
		c.gridy++;
		JSeparator sep3 = new JSeparator();
		this.prefsPanel.add(sep3, c);
		
		c.gridwidth = 1;
		c.gridy++;
		JLabel syncOpt = new JLabel();
		syncOpt.setText("Sync options");
		this.prefsPanel.add(syncOpt, c);
		c.gridy++;
		
		
		ButtonGroup grp = new ButtonGroup();  
		this.syncAll = new JRadioButton();
		this.syncSelected = new JRadioButton();
		this.syncAll.setText("Sync entire library");
		this.syncSelected.setText("Sync only selected playlists:");
		
		ActionListener al = new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				boolean wholelib = arg0.getSource().equals(Window.this.syncAll);
				Window.this.backend.setWholeLibSync(wholelib);
				Window.this.setPlaylistsEnabled(!wholelib);
				Window.this.updateProgressBarAndStatus();
			}
			
		};
		
		this.syncAll.addActionListener(al);
		this.syncSelected.addActionListener(al);
		
		c.gridy++;
		this.prefsPanel.add(this.syncAll, c);
		c.gridy++;
		this.prefsPanel.add(this.syncSelected, c);
		grp.add(this.syncAll);
		grp.add(this.syncSelected);
		
		this.syncSelected.setBackground(Window.PREFSPANEL_BG);
		this.syncAll.setBackground(Window.PREFSPANEL_BG);
		
		if (this.backend.getSetting(Settings.SYNC_ENTIRE_LIBRARY).equals("yes")) {
			this.syncAll.setSelected(true);
			this.setPlaylistsEnabled(false);
		} else {
			this.syncSelected.setSelected(true);
		}
		
		if (jsp == null) {
			playlistPane = new JPanel();
			
			jsp = new JScrollPane(playlistPane, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
					JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
			jsp.setPreferredSize(new Dimension(180,130));
		}
		
		c.gridx++;
		c.gridx++;
		c.gridwidth = 2;
		this.prefsPanel.add(jsp,c);	
		
		
		this.loadPlaylists();
		
		this.getContentPane().add(this.prefsPanel, BorderLayout.CENTER);
	}
	
	private void loadPlaylists() {
		
		if (jsp == null) {
			playlistPane = new JPanel();
			
			jsp = new JScrollPane(playlistPane, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
					JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
			jsp.setPreferredSize(new Dimension(180,130));
		}
		
		if (this.playlistPane != null) {
			this.jsp.remove(this.playlistPane);
		}
		this.playlistPane = new JPanel();
		this.jsp.setViewportView(this.playlistPane);
		
		this.playlists.removeAll(this.playlists);
		
		playlistPane.setLayout(new GridBagLayout());
		GridBagConstraints c2 = new GridBagConstraints();
		playlistPane.setAlignmentX(LEFT_ALIGNMENT);
		c2.anchor = GridBagConstraints.NORTHWEST;
		c2.fill = GridBagConstraints.VERTICAL;
		
		ActionListener al = new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				JCheckBox chk = (JCheckBox)arg0.getSource();
				Window.this.backend.setPlaylistEnabled(LibSync.ITUNES_LIB, chk.getText(), chk.isSelected());
				Window.this.updateDiskSpaceData();
			}
			
		};
		
		FocusAdapter fa = new FocusAdapter() {
			public void focusGained(FocusEvent f) {
				Window.this.playlistPane.scrollRectToVisible(((JCheckBox)f.getSource()).getBounds());
			}
		};
		
		for (String playlist : this.backend.getPlaylists()) {
			JCheckBox playlistChkBox = new JCheckBox();
			Dimension d = new Dimension(160,playlistChkBox.getPreferredSize().height);
			playlistChkBox.setPreferredSize(d);
			playlistChkBox.addActionListener(al);
			playlistChkBox.addFocusListener(fa);
			playlistChkBox.setText(playlist);
			boolean selected = this.backend.getSetting(Settings.SYNC_PLAYLIST_PREFIX+playlist) != null && 
				this.backend.getSetting(Settings.SYNC_PLAYLIST_PREFIX+playlist).equals("yes");
			playlistChkBox.setSelected(selected);
			playlistPane.add(playlistChkBox, c2);
			this.playlists.add(playlistChkBox);
			c2.gridy += 2;
		}
		
		if (this.backend.getSetting(Settings.SYNC_ENTIRE_LIBRARY).equals("yes") ||
				this.backend.getAnalysisState() == LibSync.LIBSTATE_SYNCING) {
			this.setPlaylistsEnabled(false);
		}
		
		this.pack();
	}
	
	private void setPlaylistsEnabled(boolean flag) {
			for (JCheckBox chk : this.playlists) {
				//System.out.println(chk.getText()+": "+flag);
				//System.out.println(chk.getText());
				chk.setEnabled(flag);
				if (this.backend != null && 
						this.backend.getSetting(
								Settings.SYNC_PLAYLIST_PREFIX + chk.getText()) != null) {
					if (!this.backend.getSetting(
							Settings.SYNC_PLAYLIST_PREFIX + chk.getText()).equals("yes")) {
						chk.setSelected(false);
					}
				}
				//System.out.println(chk.isEnabled());
			}
			
	}
	
	private void fireLibraryLocationDataChanged(String lib1, String lib2) {
		Window.this.backend.setSetting(Settings.ITUNES_LIBRARY,lib1);
		Window.this.backend.setSetting(Settings.MOBILE_LIBRARY_DIR,lib2);
		Window.this.updateProgressBarAndStatus();
		try {
			//System.out.println("Reanalyzing");
			Window.this.backend.reanalyze();
			Window.this.setupBackEnd();
		} catch (FileNotFoundException e) {
			// shouldnt happen ever
			JOptionPane.showMessageDialog(Window.this,e.getLocalizedMessage(),"File not found",JOptionPane.ERROR_MESSAGE);
		}
		Runnable r = new Runnable() {
			public void run() {
				Window.this.updateProgressBarAndStatus();
			}
		};
		SwingUtilities.invokeLater(r);
		
	}
	
	private void updateProgressBarAndStatus() {
		if (this.progress == null) {
			this.progress = new JProgressBar();
		}
		this.progress.setMaximum(100);
		this.progress.setMinimum(0);
		this.progress.setValue(this.backend.getProgress());
		
		if (this.statusMsg == null) {
			this.setupBottomPanel();
		}
		switch (this.backend.getAnalysisState()) {
			case LibSync.LIBSTATE_SYNCING:
				this.setSyncBtnsEnabled(false);
				int[] amounts = this.backend.getProgressAmounts();
				if (amounts[2] == amounts[3]) { // already copying
					this.statusMsg.setText("Copying file "+amounts[0] + " of " + amounts[1]);
				} else {
					this.statusMsg.setText("Deleting file "+amounts[2] + " of " + amounts[3]);
				}
				break;
			case LibSync.LIBSTATE_SYNC_OK:
				this.statusMsg.setText("Sync OK");
				this.setSyncBtnsEnabled(true);
				break;
			case LibSync.LIBSTATE_ANALYZING:
				this.statusMsg.setText("Analyzing...");
				this.showErrorSign(LibSync.MOBILE_LIB, null, "");
				this.showErrorSign(LibSync.ITUNES_LIB, null, "");
				this.setSyncBtnsEnabled(false);
				break;
			case LibSync.LIBSTATE_BOTH_LIBS_OK:
				this.statusMsg.setText("Ready to sync");
				this.showErrorSign(LibSync.MOBILE_LIB, null, "");
				this.showErrorSign(LibSync.ITUNES_LIB, null, "");
				this.setSyncBtnsEnabled(true);
				break;
			case LibSync.LIBSTATE_ITUNES_LIB_OK_NO_MOBILE_LIB:
				this.statusMsg.setText("Ready to sync");
				this.showErrorSign(LibSync.MOBILE_LIB, WARNING_ICON, "No mobile library found - creating a new one");
				this.showErrorSign(LibSync.ITUNES_LIB, null, "");
				this.setSyncBtnsEnabled(true);
				break;
			case LibSync.LIBSTATE_NO_ITUNES_LIB_MOBILE_LIB_OK:
				this.statusMsg.setText("iTunes library not found");
				this.showErrorSign(LibSync.ITUNES_LIB, ERROR_ICON, "No iTunes library found - impossible to sync");
				this.showErrorSign(LibSync.MOBILE_LIB, null, "");
				this.setSyncBtnsEnabled(false);
				break;
			case LibSync.LIBSTATE_NO_ITUNES_LIB_NO_MOBILE_LIB:
				this.statusMsg.setText("iTunes library not found");
				this.showErrorSign(LibSync.ITUNES_LIB, ERROR_ICON, "No iTunes library found - impossible to sync");
				this.showErrorSign(LibSync.MOBILE_LIB, WARNING_ICON, "No mobile library found - creating new");
				this.setSyncBtnsEnabled(false);
				break;
			default:
				break;
		}
		this.updateDiskSpaceData();
		this.pack();
	}
	
	private void updateDiskSpaceData(boolean zeroneeded) {
		long spaceNeeded = this.backend.getTotalNeededDiskSpace()/1024/1024;
		if (zeroneeded) {
			spaceNeeded = 0;
		}
		long spaceAvail = this.backend.getFreeSpaceOnDevice()/1024/1024;
		this.diskSpaceLabel.setText("<html>Space needed: "+spaceNeeded+" MB<p>Available:"+spaceAvail+" MB</html>");
	}
	
	private void updateDiskSpaceData() {
		this.updateDiskSpaceData(false);
	}
	
	private synchronized void analysisReady(boolean ready, boolean success) {
		if (ready) {
			Debug.println("Analysis ready - success: " + success);
		}
		if (success) {
			if (ready) {
				this.loadPlaylists();
			}
			
			if (this.syncAll != null)
				this.syncAll.setEnabled(ready);
			if (this.syncSelected != null)
				this.syncSelected.setEnabled(ready);
			if (this.syncBtn != null)
				this.syncBtn.setEnabled(ready);
			if (!this.backend.getSetting(Settings.SYNC_ENTIRE_LIBRARY).equals("yes")) {
				for (JCheckBox chk : this.playlists) {
					chk.setEnabled(ready);
				}
			}
		}
		
		this.updateProgressBarAndStatus();
	}
	
	private void setSyncBtnsEnabled(boolean flag) {
			if (flag) {
				this.loadPlaylists();
			}
			
			if (this.prefsPanel == null) {
				this.prefsPanel = new JPanel();
			}
			if (this.backend.getAnalysisState() == LibSync.LIBSTATE_SYNCING) {
				for (Component c : this.prefsPanel.getComponents()) {
					c.setEnabled(false);
				}
			} else {
				for (Component c : this.prefsPanel.getComponents()) {
					c.setEnabled(true);
				}
			}
			
			if (this.syncAll != null)
				this.syncAll.setEnabled(flag);
			if (this.syncSelected != null)
				this.syncSelected.setEnabled(flag);
			if (this.syncBtn != null)
				this.syncBtn.setEnabled(flag);
			
			this.setPlaylistsEnabled(flag && !this.backend.getSetting(Settings.SYNC_ENTIRE_LIBRARY).equals("yes"));
	}
	
	private void showErrorSign(int library,ImageIcon sign, String tooltip) {
		
		
		this.setErrorPanelMsg(tooltip);
		if (library == LibSync.ITUNES_LIB) {
			if (this.errorPanelItunes == null) {
				this.errorPanelItunes = new JPanel();
			}
			if (sign != null) {
				JLabel jl = new JLabel(sign);
				jl.setToolTipText(tooltip);
				this.errorPanelItunes.add(jl);
			} else {
				if (this.errorPanelItunes.getComponentCount() > 0) {
					this.errorPanelItunes.getComponent(0).setVisible(false);
				}
				this.errorPanelItunes.removeAll();
			}
		} else if (library == LibSync.MOBILE_LIB) {
			if (this.errorPanelMobi == null) {
				this.errorPanelMobi = new JPanel();
			}
			if (sign != null) {
				JLabel jl = new JLabel(sign);
				jl.setToolTipText(tooltip);
				this.errorPanelMobi.add(jl);
			} else {
				if (this.errorPanelMobi.getComponentCount() > 0) {
					this.errorPanelMobi.getComponent(0).setVisible(false);
				}
				this.errorPanelMobi.removeAll();
			}
		} else throw new IllegalArgumentException("Library ID not correct!");
		
		this.pack();
	}
	
	private void setErrorPanelMsg(String msg) {
//		if (this.errorLabel == null) {
//			this.errorLabel = new JLabel();
//		}
//		if ("".equals(msg)) {
//			msg = " ";
//		}
//		System.out.println("ABC");
//		this.errorLabel.setText(msg);
	}
	
	private class ImageJPanel extends JPanel {
		
		private static final long serialVersionUID = 5897025565559999935L;
		private Image img;
		
		public void setBgImage(Image im) {
			this.img = im;
		}
		
		public void paintComponent(Graphics g) {
			int height = this.getSize().height;
			int width = this.getSize().width;
			
			g.drawImage(img,0,0, width, height, null);
		}
	}
	
	public static void main(String[] args) {
		new Window();
	}
	
}
