package de.hfu.anybeam.desktop;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.UIManager;

import de.hfu.anybeam.networkCore.Client;
import de.hfu.anybeam.networkCore.DeviceType;
import de.hfu.anybeam.networkCore.EncryptionType;
import de.hfu.anybeam.networkCore.NetworkEnvironmentListener;
import de.hfu.anybeam.networkCore.NetworkEnvironmentSettings;

public class Main extends JFrame implements NetworkEnvironmentListener, ActionListener {

	private static final long serialVersionUID = 7918950691568540541L;

	public static void main(String[] args) {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Exception e) {
			e.printStackTrace();
		}
		new Main();
	}

	private JList list = new JList();
	private JButton search = new JButton("Search");
	
	public Main() {
		super("NetworkCore Test");
		EncryptionType type = EncryptionType.AES256;
		String pass = "anybeamRockt1137";
		byte[] key =  type.getSecretKeyFromPassword(pass);
		NetworkEnvironmentSettings set = new NetworkEnvironmentSettings("MacBook Pro", DeviceType.TYPE_LAPTOP, type, 1338, 1337, key);
		
		
		this.setLayout(new BorderLayout());
		try {
			NetworkEnvironmentManager.addNetworkEnvironmentListener(this);
			NetworkEnvironmentManager.getNetworkEnvironment().startClientSearch(356, TimeUnit.DAYS, 3, TimeUnit.SECONDS);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		this.setSize(new Dimension(400, 300));
		this.setResizable(false);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		
		this.add(new JScrollPane(list), BorderLayout.CENTER);
		this.add(search, BorderLayout.SOUTH);
		search.addActionListener(this);
		this.setVisible(true);
		
		Runtime.getRuntime().addShutdownHook(new Thread() {
			@Override
			public void run() {
				try {
					NetworkEnvironmentManager.getNetworkEnvironment().dispose();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	
	public void updateTable() {
		DefaultListModel model = new DefaultListModel();

		List<Client> clients = new ArrayList<Client>();
		try {
			clients = NetworkEnvironmentManager.getNetworkEnvironment().getClientList();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		for(int i=0; i<clients.size(); i++) {
			model.add(i, clients.get(i).getName());
		}
		
		this.list.setModel(model);


	}


	@Override
	public void clientFound(Client c) {
		updateTable();
		
	}


	@Override
	public void clientUpdated(Client c) {
		updateTable();
		
	}


	@Override
	public void clientLost(Client c) {
		updateTable();
		
	}


	@Override
	public void clientListCleared() {
		updateTable();
		
	}


	@Override
	public void clientSearchStarted() {
		this.search.setEnabled(false);
		this.search.setText("Searching...");
		
	}


	@Override
	public void clientSearchDone() {
		this.search.setText("Search");
		this.search.setEnabled(true);

	}


	@Override
	public void actionPerformed(ActionEvent e) {
		try {
			NetworkEnvironmentManager.getNetworkEnvironment().startClientSearch();
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}		
	}
}
