package org.kne.cloud.network.klalb.ui;

import java.awt.Color;
import java.awt.Component;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import javax.swing.border.LineBorder;

import org.kne.cloud.klalb.uitool.XDefaultListModel;
import org.kne.ui.XFrame;
import javax.swing.JScrollPane;
import java.awt.BorderLayout;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class NetworkInterfaceSelector extends XFrame{
	private JList list;
	public NetworkInterfaceSelector(XDefaultListModel<NetworkInterface> model) {
		this(model,null);
	}

	public NetworkInterfaceSelector(XDefaultListModel<NetworkInterface> model,Component relate) {
		// setResizable(false);
		setIconImage(UIEnv.getIcon());
		// setTitleColor(new Color(255, 255, 255, 250));
		setTitleColor(UIEnv.getDefaultTitleColor());
		getContentPane().setBackground(UIEnv.getDefaultBackgroundColor());
		setTitle(UIEnv.getRsb().getString("selectinterface"));
		JScrollPane scrollPane = new JScrollPane();
		getContentPane().add(scrollPane, BorderLayout.CENTER);
		XDefaultListModel<NetworkInterface> nif=new XDefaultListModel<>();
		list = new JList<NetworkInterface>(nif);
		scrollPane.setViewportView(list);
		
		JPanel panel = new JPanel();
		getContentPane().add(panel, BorderLayout.SOUTH);
		
		JButton cancel = new JButton(UIEnv.getRsb().getString("cancel"));
		cancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setVisible(false);
			}
		});
		panel.add(cancel);
		
		JButton ok = new JButton(UIEnv.getRsb().getString("ok"));
		ok.addActionListener(new ActionListener() {
			@SuppressWarnings("unchecked")
			public void actionPerformed(ActionEvent e) {
				model.addAll(list.getSelectedValuesList());
				setVisible(false);
			}
		});
		panel.add(ok);
		getTitlelabel().setFont(UIEnv.getFont().deriveFont(17.0f));
		getTitlepanel().setBorder(new LineBorder(Color.LIGHT_GRAY));
		getTitlelabel().setForeground(Color.WHITE);
		// getContentPane().setBackground(new Color(0,0,0,0));
		setSize(1280/2, 720/2);
		setLocationRelativeTo(relate);
		
		Enumeration<NetworkInterface> eu;
		try {
			eu = NetworkInterface.getNetworkInterfaces();
		
		while (eu.hasMoreElements()) {
			NetworkInterface networkInterface = (NetworkInterface) eu.nextElement();
			if(networkInterface.isUp())
		nif.addElement(networkInterface);
		}
		} catch (SocketException e) {
			e.printStackTrace();
		}
		
		setVisible(true);
	}


}
