package org.kne.cloud.network.klalb;

import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.ConnectException;
import java.net.UnknownHostException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.Timer;
import java.util.TimerTask;

import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import org.kne.cloud.network.MultipurposeSocketAddress;
import org.kne.cloud.network.SocketBridge;
import org.kne.cloud.network.SocketListener;
import org.kne.cloud.network.ThreadTool;
import org.kne.ui.XFrame;
import org.kne.ui.YScrollPane;
import javax.swing.JProgressBar;

public class KLALBStateGUI extends XFrame {
	private BufferedImage bi;

	private TimerTask tsk;

	private SystemTray st;

	private TrayIcon ti;
	public KLALBStateGUI(KLALBController kc,String title) throws IOException {
		setResizable(false);
		try {
			bi = ImageIO.read(KLALBStateGUI.class.getResourceAsStream("/assets/KNEL.png"));
		} catch (IOException e) {
			e.printStackTrace();
		}
		if(bi!=null)
		setIconImage(bi);
		setTitleColor(new Color(0,0,0,80));
	getContentPane().setBackground(new Color(0,0,0,80));
		getTitlelabel().setForeground(Color.WHITE);

	

		YScrollPane ysp = new YScrollPane(660);
		ysp.setOpaque(false);
		getContentPane().add(ysp);
		JPanel wv = ysp.getView();
		
		/*JProgressBar progressBar = new JProgressBar();
		progressBar.setIndeterminate(true);
		progressBar.setForeground(Color.YELLOW);
		progressBar.setBorder(null);
		progressBar.setPreferredSize(new Dimension(100, 4));
		ysp.add(progressBar, BorderLayout.NORTH);*/
		wv.addComponentListener(new ComponentListener() {
			
			@Override
			public void componentShown(ComponentEvent e) {
				// TODO 自动生成的方法存根
				
			}
			
			@Override
			public void componentResized(ComponentEvent e) {
				// TODO 自动生成的方法存根
				
			}
			
			@Override
			public void componentMoved(ComponentEvent e) {
				repaint();
			}
			
			@Override
			public void componentHidden(ComponentEvent e) {
				// TODO 自动生成的方法存根
				
			}
		});
		setTitle(title);
		setSize(680, 430);
		setLocationRelativeTo(null);

		//setVisible(true);

		if (SystemTray.isSupported()) {
			st = SystemTray.getSystemTray();
			ti = new TrayIcon(bi);
			ti.setImageAutoSize(true);
			PopupMenu jpm = new PopupMenu();
			MenuItem mix = new MenuItem("open");
			mix.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent e) {
					setVisible(true);
				}
			});
			jpm.add(mix);
			MenuItem mi = new MenuItem("exit");
			mi.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent e) {
					close();
				}
			});
			jpm.add(mi);
			ti.setPopupMenu(jpm);
			try {
				st.add(ti);
			} catch (AWTException e1) {
				e1.printStackTrace();
			}
			ti.addActionListener((x) -> {
				setVisible(true);
			});
		}

		repaint();
		
		/*progressBar.setIndeterminate(false);
		progressBar.setValue(100);
		progressBar.setForeground(Color.GREEN);*/
		Timer t=	new Timer("状态刷新线程",true);
		tsk=new TimerTask() {
			
			@Override
			public void run() {
				synchronized (kc.getLines()) {
			loop:for (Iterator<KLALBRemoteLine> iterator = kc.getLines().iterator(); iterator.hasNext();) {
				KLALBRemoteLine ent =  iterator.next();
				Component[] count=ysp.getView().getComponents();
				for (int i = 0; i < count.length; i++) {
					Component tp=count[i];
					if(tp instanceof TPanel) {
						if(((TPanel) tp).getTunnel().equals(ent)) {
							continue loop;
						}
					}
				}
				TPanel tp=new TPanel(ent);
				ysp.getView().add(tp);
			}
			Component[] count=ysp.getView().getComponents();
			for (int i = 0; i < count.length; i++) {
				Component tp=count[i];
				if(tp instanceof TPanel) {
					if(kc.getLines().contains(((TPanel) tp).getTunnel())) {
					((TPanel) tp).updateTraffic();
					}else {
						ysp.getView().remove(tp);
					} 
				}
			}
					
				}
			repaint();
			}
		};
		t.schedule(tsk, 500, 500);
	}

	public void close() {
		setVisible(false);
		if(tsk!=null) {
			tsk.cancel();
		}
		st.remove(ti);
	}

	public static void main(String[] args) throws IOException, InterruptedException {//,new MultipurposeSocketAddress("frp-mom.top:22226"),new MultipurposeSocketAddress("0.0.0.0:26468")
		KLALBStateGUI kcg=new KLALBStateGUI(new KLALBController(),"KNE云网络负载均衡客户端 V2.0");
		Thread.sleep(10000);
		kcg.close();
	}
}
