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

import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;

import javax.swing.SwingConstants;
import javax.swing.border.LineBorder;

import org.kne.cloud.network.MultiProtocolSocketAddress;
import org.kne.cloud.network.ipv6.IPv6AddressGroup;
import org.kne.cloud.network.klalb.CONST;
import org.kne.cloud.network.klalb.KLALBController;
import org.kne.cloud.network.klalb.KLALBRemoteLink;
import org.kne.cloud.network.klalb.KLALBUtils;
import org.kne.cloud.network.monitor.LinkStatus;

import java.awt.Font;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import java.util.Objects;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JPopupMenu;
import javax.swing.JMenuItem;

public class TPanel2 extends JPanel {
	private JLabel tname;
	private JLabel targup;
	private KLALBRemoteLink tunnel;
	private LineMonitorGUI mdg;
	private JMenuItem mntmNewMenuItem2;
	private JMenuItem mntmNewMenuItem3;
	private static volatile Image imgup,imgdown,imgeth;
	@Override
	public int hashCode() {
		return Objects.hash(tunnel);
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		TPanel2 other = (TPanel2) obj;
		return Objects.equals(tunnel, other.tunnel);
	}

	public JLabel getTname() {
		return tname;
	}

	public JLabel getTarg() {
		return targup;
	}

	/**
	 * @wbp.parser.constructor
	 */
	public TPanel2(KLALBRemoteLink t,KLALBController kc) {
		this.tunnel = t;
		//setOpaque(false);
		setBackground(Color.WHITE);
		setLayout(new BorderLayout(0, 0));

		tname = new JLabel();
			tname.setText(t.getName());
		tname.setFont(new Font("宋体", Font.BOLD, 14));

		targup = new JLabel();
		try {
			targup.setIcon(new ImageIcon(getImgUp() ));
		} catch (IOException e) {
			e.printStackTrace();
		}
		targup.setFont(new Font("宋体", Font.PLAIN, 13));
		targup.setHorizontalAlignment(SwingConstants.TRAILING);
		targup.setForeground(Color.BLACK);
		targup.setHorizontalTextPosition(SwingConstants.LEFT);

		setBorder(new LineBorder(Color.LIGHT_GRAY));
		setSize(CONST.itemwidth, CONST.linepanelheight);
		setPreferredSize(getSize());
		
		JPanel panel = new JPanel();
		panel.setOpaque(false);
		add(panel, BorderLayout.CENTER);
		panel.setLayout(new BorderLayout(0, 0));
		panel.add(tname);
		
		vaddr = new JLabel(UIEnv.getRsb().getString("unknown"));
		panel.add(vaddr, BorderLayout.SOUTH);
		vaddr.setForeground(Color.DARK_GRAY);
		
		panel_1 = new JPanel();
		panel_1.setOpaque(false);
		add(panel_1, BorderLayout.EAST);
		panel_1.setLayout(new BorderLayout(0, 0));
		panel_1.add(targup, BorderLayout.NORTH);
		
		targdown = new JLabel();
		try {
			targdown.setIcon(new ImageIcon(getImgDown()));
		} catch (IOException e) {
			e.printStackTrace();
		}
		targdown.setHorizontalTextPosition(SwingConstants.LEFT);
		targdown.setFont(new Font("宋体", Font.PLAIN, 13));
		targdown.setHorizontalAlignment(SwingConstants.TRAILING);
		targdown.setForeground(Color.BLACK);
		panel_1.add(targdown, BorderLayout.SOUTH);
		

		panel_2 = new JPanel();
		panel_2.setOpaque(false);
		panel_2.setPreferredSize(new Dimension(50, 45));
		panel_2.setSize(panel_2.getPreferredSize());
		add(panel_2, BorderLayout.WEST);
		panel_2.setLayout(null);
		
		lblNewLabel = new JLabel();
		lblNewLabel.setBounds(0, 0, 50, 40);
		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		lblNewLabel.setVerticalAlignment(SwingConstants.CENTER);
		panel_2.add(lblNewLabel);
		try {
			double scalex=1.0;
			lblNewLabel.setIcon(new ImageIcon(getImgEth().getScaledInstance((int)(getImgEth().getWidth(null)*scalex), (int)(getImgEth().getHeight(null)*scalex), Image.SCALE_FAST)));
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		
		lblNewLabel_1 = new JLabel();
		lblNewLabel_1.setOpaque(true);
		lblNewLabel_1.setBackground(Color.LIGHT_GRAY);
		lblNewLabel_1.setBounds(8, 7, 7, 4);
		panel_2.add(lblNewLabel_1);
		
		lblNewLabel_2 = new JLabel();
		lblNewLabel_2.setOpaque(true);
		lblNewLabel_2.setBackground(Color.LIGHT_GRAY);
		lblNewLabel_2.setBounds(37, 7, 7, 4);
		panel_2.add(lblNewLabel_2);
		
		
		popupMenu = new JPopupMenu();
		
		mntmNewMenuItem = new JMenuItem(UIEnv.getRsb().getString("viewlinemonitor"));
		popupMenu.add(mntmNewMenuItem);
		mntmNewMenuItem.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {

				mdg.setVisible(true);
			}
		});
		
		JMenuItem mntmNewMenuItemz = new JMenuItem(UIEnv.getRsb().getString("copylineaddress"));
		popupMenu.add(mntmNewMenuItemz);
		mntmNewMenuItemz.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(tname.getText()), null);
			}
		});
		
		JMenuItem mntmNewMenuItemx = new JMenuItem(UIEnv.getRsb().getString("copyvirtualaddress"));
		popupMenu.add(mntmNewMenuItemx);
		mntmNewMenuItemx.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {

				Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(vaddr.getText()), null);
			}
		});

	
		
		mntmNewMenuItem3 = new JMenuItem(UIEnv.getRsb().getString("tryreconnect"));
		popupMenu.add(mntmNewMenuItem3);
		mntmNewMenuItem3.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				tunnel.reconnectImmediately();
				
			}
		});
		mntmNewMenuItem3.setForeground(Color.GREEN);
		mntmNewMenuItem2 = new JMenuItem(UIEnv.getRsb().getString("forcedisconnect"));
		popupMenu.add(mntmNewMenuItem2);
		mntmNewMenuItem2.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				tunnel.dislink();
			}
		});
		mntmNewMenuItem2.setForeground(Color.RED);
		
		JMenuItem mi=new JMenuItem(UIEnv.getRsb().getString("removeline"));
		popupMenu.add(mi);
		mi.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				MultiProtocolSocketAddress mtar=tunnel.getSocketAddress();
				if(mtar!=null) {
				kc.removeRemoteLines(mtar);
				}else {
					tunnel.close();
				}
			}
		});	
		mi.setForeground(Color.RED);
		
		mdg=new LineMonitorGUI(t);
		addMouseListener(new MouseListener() {
			
			@Override
			public void mouseReleased(MouseEvent e) {
				if(e.getButton()==MouseEvent.BUTTON3)
				popupMenu.show(TPanel2.this,e.getX(),e.getY());
			}
			
			@Override
			public void mousePressed(MouseEvent e) {
				// TODO 自动生成的方法存根
				
			}
			
			@Override
			public void mouseExited(MouseEvent e) {
				// TODO 自动生成的方法存根
				
			}
			
			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO 自动生成的方法存根
				
			}
			
			@Override
			public void mouseClicked(MouseEvent e) {
				switch(e.getButton()) {
				case MouseEvent.BUTTON1:
					if(e.getClickCount()==2) {
						mdg.setVisible(true);
					}
					break;
				case MouseEvent.BUTTON3:
						
				}
				
			}
		});
	}

	private static Image getImgEth() throws IOException {
		if(imgeth==null) {
			imgeth=ImageIO.read(TPanel2.class.getResourceAsStream("/assets/ethnet.png"));
		}
		return imgeth;
	}
	
	private static Image getImgUp() throws IOException {
		if(imgup==null) {
			imgup=ImageIO.read(TPanel2.class.getResourceAsStream("/assets/KLALBupload.png")).getScaledInstance(20, 20,Image.SCALE_SMOOTH);
		}
		return imgup;
	}
	
	private static Image getImgDown() throws IOException {
		if(imgdown==null) {
			imgdown=ImageIO.read(TPanel2.class.getResourceAsStream("/assets/KLALBdownload.png")).getScaledInstance(20, 20,Image.SCALE_SMOOTH);
		}
		return imgdown;
	}
	
	private JLabel vaddr;
	private JPanel panel_1;
	private JLabel targdown;
	private JPanel panel_2;
	private JPopupMenu popupMenu;
	private JMenuItem mntmNewMenuItem;
	private JLabel lblNewLabel;
	private JLabel lblNewLabel_1;
	private JLabel lblNewLabel_2;


	private void updateText() {
		targup.setText( KLALBUtils. convertIBUint(tunnel.getMonitor().getOutSpeed()) + "/s "+KLALBUtils. convertUintDefalut(tunnel.getMonitor().getOutPPS()) + "PPS "+ KLALBUtils.convertUintNanoDefalut(tunnel.getMonitor().getOutDelay()) + "s");
		targdown.setText(KLALBUtils. convertIBUint(tunnel.getMonitor().getInSpeed()) + "/s "+KLALBUtils. convertUintDefalut(tunnel.getMonitor().getInPPS()) + "PPS "  + KLALBUtils.convertUintNanoDefalut( tunnel.getMonitor().getInDelay()) + "s");
		
     if(tunnel.getMonitor().getOutSpeed()>4096||tunnel.getMonitor().getInSpeed()>4096) {
    	 lblNewLabel_2.setBackground(Color.ORANGE);
		}else {
			lblNewLabel_2.setBackground(Color.LIGHT_GRAY);
		}
	}

	public KLALBRemoteLink getTunnel() {
		return tunnel;
	}


	public void updateTraffic() {
		switch (tunnel.getState()) {
		case LinkStatus.DOWN:
			tname.setForeground(Color.RED);
			lblNewLabel_1.setBackground(Color.LIGHT_GRAY);
			break;
		case LinkStatus.UNSTABLE:
			tname.setForeground(Color.YELLOW);
			lblNewLabel_1.setBackground(Color.LIGHT_GRAY);
			break;
		case LinkStatus.UP:
			tname.setForeground(Color.GREEN);
			lblNewLabel_1.setBackground(Color.GREEN);
		
			break;
		default:
			break;
		}
		if(tunnel.getState()!=LinkStatus.DOWN) {
			
			
			IPv6AddressGroup unvar=tunnel.getRemoteVaddr();
			if(unvar==null) {
				this.vaddr.setText("unknown");
			}else {
			this.vaddr.setText(unvar.getAddress().toString());
			}
			
				
				updateText();
		}
		//mdg.updateTraffic();
	}

	public LineMonitorGUI getMdg() {
		return mdg;
	}

}
