package ui.view;

import javax.swing.JSplitPane;
import javax.swing.JPanel;
import javax.swing.BoxLayout;
import javax.swing.JButton;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JToggleButton;
import java.awt.Component;

public class FlexSubData extends JSplitPane {
	public static final String HIDE = "Hide Objects";
	public static final String SHOW = "Show Objects";
	private JPanel subnetInfo;
	private JPanel objectInfo;
	private JPanel currentObj;
	private JButton btnShowObjects;
	private FlexiblePane listener;
	private boolean shown; // 0 = Hide, 1= Show
	public FlexSubData(FlexibleData fD) {
		setDividerSize(0);
		setAlignmentY(Component.CENTER_ALIGNMENT);
		setAlignmentX(Component.CENTER_ALIGNMENT);
		setOrientation(JSplitPane.VERTICAL_SPLIT);
		
		//subnetInfo = new FlexibleData("test" , 0, 0);
		subnetInfo = fD;
		subnetInfo.setMinimumSize(new Dimension(600, 100));
		setLeftComponent(subnetInfo);
		subnetInfo.setLayout(null);
		shown = true;
		btnShowObjects = new JButton(SHOW);
		btnShowObjects.setBounds(135, 13, 99, 23);
		btnShowObjects.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				if(shown){
					objectInfo.removeAll();
					objectInfo.add(currentObj);
					objectInfo.revalidate();
					objectInfo.updateUI();
					listener.revalidate();
					btnShowObjects.setText(HIDE);
					shown = false;
				}else{
					objectInfo.removeAll();
					objectInfo.revalidate();
					objectInfo.updateUI();
					listener.revalidate();
					btnShowObjects.setText(SHOW);
					shown = true;
				}
			}
		});
		subnetInfo.add(btnShowObjects);

		objectInfo = new JPanel();
		setRightComponent(objectInfo);
		objectInfo.setLayout(new BoxLayout(objectInfo, BoxLayout.X_AXIS));
	}
	
	public void setObjects(JPanel obj){
		objectInfo.removeAll();
		currentObj = obj;
	}
	
	public void setListener(FlexiblePane fP){
		listener = fP;
	}
	
	public JPanel getSubInfo(){
		return subnetInfo;
	}
	
	public void repaint(){
		super.repaint();
	}
	
	public void showAction(){
		objectInfo.removeAll();
		objectInfo.add(currentObj);
		objectInfo.revalidate();
		objectInfo.updateUI();
		listener.revalidate();
		shown = false;
		btnShowObjects.setText(HIDE);
	}
	
	public void hideAction(){
		objectInfo.removeAll();
		objectInfo.revalidate();
		objectInfo.updateUI();
		listener.revalidate();
		shown = true;
		btnShowObjects.setText(SHOW);
	}
}
