package ui.view;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.util.ArrayList;
import java.util.LinkedList;

import javax.swing.BoxLayout;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import classes.AbstractCanvasObject;
import classes.HolonObject;
import classes.SubNet;
import interfaces.ObjectListener;
import ui.controller.Control;

public class FlexiblePane extends JScrollPane implements ObjectListener {
	private JPanel flexPanel;
	private Control controller;
	
	public FlexiblePane(Control controller) {
		this.controller = controller;
		flexPanel = new JPanel();
		flexPanel.setLayout(new BoxLayout(flexPanel, BoxLayout.Y_AXIS));
		setViewportView(flexPanel);
		class ResizeListener extends ComponentAdapter{
			public void componentResized(ComponentEvent e){
				flexPanel.requestFocusInWindow();
			}
		}
		this.addComponentListener(new ResizeListener());
	}
	
	public void recalculate(){
		LinkedList<Color> netColors = controller.getModel().getSubNetColors();
		flexPanel.removeAll();
		ArrayList<Float> maxVals = calcMax(controller.getSimManager().getSubNets());
		float gridProd = maxVals.get(0);
		float gridCons = maxVals.get(1);
		FlexibleData maingrid = new FlexibleData("Main Grid", gridProd, gridCons, gridProd, gridCons);
		maingrid.getColorPanel().setVisible(false);
		maingrid.getNamelbl().setFont(new Font("Tahoma", Font.BOLD, 11));
		flexPanel.add(maingrid);
		int counter = 0;
		int colorCounter = 0;
		ArrayList<SubNet> subnets = controller.getSimManager().getSubNets(); 
		for(SubNet sn: subnets){
			if(netColors.size() < subnets.size()){
				netColors.add(new Color((int)(Math.random()*255), (int)(Math.random()*255), (int)(Math.random()*255)));
			}
			JPanel objects = new JPanel();
			objects.setLayout(new BoxLayout(objects, BoxLayout.Y_AXIS));
			int index = flexPanel.getComponentCount();
			float subProd = 0;
			float subCons = 0;
			Color subColor = netColors.get(colorCounter);
			for(HolonObject hl: sn.getObjects()){
				System.out.println("Delete Me");
				FlexibleData tmp = new FlexibleData(hl.getName()+" "+hl.getId(),
						666.f, 666.f, gridProd, gridCons);
				tmp.getColorPanel().setBackground(subColor);
				objects.add(tmp);
			}
			FlexibleData subnet = new FlexibleData("Subnet "+ counter, subProd, subCons, gridProd, gridCons);
			subnet.getNamelbl().setFont(new Font("Tahoma", Font.BOLD, 11));
			subnet.getColorPanel().setBackground(subColor);
			FlexSubData wholeSubnet = new FlexSubData(subnet);
			wholeSubnet.setObjects(objects);
			wholeSubnet.setListener(this);
			flexPanel.add(wholeSubnet, index);
			counter++;
			colorCounter++;
		}
		flexPanel.revalidate();
		flexPanel.repaint();
		flexPanel.updateUI();
	}
	
	public JPanel getPanel(){
		return flexPanel;
	}
	
	public ArrayList<Float> calcMax(ArrayList<SubNet> sn){
		ArrayList<Float> result = new ArrayList<Float>();
		float prod = 0;
		float cons = 0;
		if(sn != null){
			for(SubNet s: sn){
				for(HolonObject h: s.getObjects()){
					prod += 666.f;
					cons += 666.f;
				}
			}
			result.add(prod);
			result.add(cons);
			return result;
		}else{
			result.add(prod);
			result.add(cons);
			return result;
		}
	}

	@Override
	public void onChange(ArrayList<AbstractCanvasObject> objects) {
		recalculate();
	}

}
