package surface;

import hu.swankey.ammo.common.yggdrasil.basics.BooleanField;
import hu.swankey.ammo.common.yggdrasil.basics.ComplexYObject;
import hu.swankey.ammo.common.yggdrasil.basics.IntegerField;
import hu.swankey.ammo.common.yggdrasil.basics.Reference;
import hu.swankey.ammo.common.yggdrasil.basics.StringField;
import hu.swankey.ammo.common.yggdrasil.basics.SimpleObject;
import hu.swankey.ammo.common.yggdrasil.basics.YContainer;
import hu.swankey.ammo.common.yggdrasil.basics.YObject;
import hu.swankey.ammo.common.yggdrasil.definition.YClassDefinition;
import hu.swankey.ammo.common.yggdrasil.definition.ComplexClassDefinition;
import hu.swankey.ammo.common.yggdrasil.definition.YMethodDefinition;
import hu.swankey.ammo.common.yggdrasil.definition.YPackageDefinition;
import hu.swankey.ammo.common.yggdrasil.ext.Command;
import hu.swankey.ammo.common.yggdrasil.ext.Root;
import hu.swankey.ammo.common.yggdrasil.ext.Universe;
import hu.swankey.ammo.common.yggdrasil.ext.World;

import java.awt.Color;
import java.awt.Component;

import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTree;
import javax.swing.plaf.metal.MetalLookAndFeel;
import javax.swing.tree.DefaultTreeCellRenderer;

/**
 *
 * @author solusa
 */
public class YggdrasilCellRenderer extends DefaultTreeCellRenderer {

	private static final long serialVersionUID = 1L;
	
	private JPanel panel_renderer = new JPanel();
	private JLabel label_name = new JLabel();
	private JLabel label_value = new JLabel();
	
	private static final String VALUE_PREFIX = ": ";
	
    private ImageIcon iconClass = new ImageIcon(getClass().getResource("/icons/class.png"));
    private ImageIcon iconComplexClass = new ImageIcon(getClass().getResource("/icons/complex_class.png"));
    
    private ImageIcon iconComplex = new ImageIcon(getClass().getResource("/icons/complex.png"));
    

    private ImageIcon iconStr = new ImageIcon(getClass().getResource("/icons/field_str.png"));
    private ImageIcon iconInt = new ImageIcon(getClass().getResource("/icons/field_int.png"));
    private ImageIcon iconBool = new ImageIcon(getClass().getResource("/icons/field_bool.png"));
    private ImageIcon iconLink = new ImageIcon(getClass().getResource("/icons/field_link.png"));    
    
	private ImageIcon iconContainer = new ImageIcon(getClass().getResource("/icons/box.png"));
    
    //private ImageIcon iconUnremovableField = new ImageIcon(getClass().getResource("/icons/brick_link.png"));    
    
    private ImageIcon iconFunction = new ImageIcon(getClass().getResource("/icons/cog.png"));
    private ImageIcon iconUniverse = new ImageIcon(getClass().getResource("/icons/universe.png"));
    private ImageIcon iconRoot = new ImageIcon(getClass().getResource("/icons/yggdrasil.png"));
    private ImageIcon iconUnknownObject = new ImageIcon(getClass().getResource("/icons/exclamation.png"));


    private ImageIcon iconPackage = new ImageIcon(getClass().getResource("/icons/box.png"));
    private ImageIcon iconWorld = new ImageIcon(getClass().getResource("/icons/world.png"));
    private ImageIcon iconCommand = new ImageIcon(getClass().getResource("/icons/cog.png"));
    
    //private static final int NAME_LENGHT_MINIMUM = 5;
    
    private static final Color colorPublic =  new Color(0x000000);
    private static final Color colorOwner =  new Color(0x0000cc);
    private static final Color colorPrivate =  new Color(0xaaaaaa);
//    private static final Color colorSelected =  new Color(0x000000);
    
    //private static final Color COLOR_SELECTED_BACKGROUND = 
    
    public YggdrasilCellRenderer(){
    	label_value.setForeground(Color.GRAY);
    	
    	panel_renderer.setLayout( new BoxLayout(panel_renderer, BoxLayout.X_AXIS) );
    	panel_renderer.add(label_name);
    	panel_renderer.add(label_value);
    }
    
    
    
    private ImageIcon getIconFor(YObject value) {


        if (value instanceof ComplexClassDefinition)
            return iconComplexClass;        
        
        if (value instanceof YClassDefinition)
            return iconClass;

        if (value instanceof Reference)
            return iconLink;

        
        if (value instanceof BooleanField)
            return iconBool;
        
        if (value instanceof IntegerField)
            return iconInt;
        
        if (value instanceof StringField)
            return iconStr;
        
        

        if (value instanceof YMethodDefinition)
            return iconFunction;

        if (value instanceof YPackageDefinition)
            return iconPackage;
        
        if (value instanceof Root)
            return iconRoot;
        
        if (value instanceof World)
            return iconWorld;
        
        if (value instanceof Universe)
            return iconUniverse;
        
        if (value instanceof Command)
            return iconCommand;
        
        if (value instanceof ComplexYObject)
            return iconComplex;
        
        if (value instanceof YContainer)
            return iconContainer;
        
//        if (value instanceof Character)
//            return iconCharacter;              

//        if (value instanceof Field) {
//            if ( ((Field)value).isRemovable() )
//                return iconField;
//            else
//                return iconUnremovableField;
//        }



        return iconUnknownObject;
    }

    
    private Color getColorFor(YObject element){
    	
    	switch (element.getAccessLevel()) {
    	
    	case YObject.ACCESS_LEVEL_PRIVATE: return colorPrivate;
    	case YObject.ACCESS_LEVEL_PROTECTED: return colorOwner;
    	case YObject.ACCESS_LEVEL_PUBLIC: return colorPublic;
    	default:
    		throw new RuntimeException("Unknown access-level: " + element.getAccessLevel());
    	}
    	
    }
    
    
    


    @Override
    public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {

    	YObject element = (YObject)value;
    	
    	label_name.setText(element.getName());
    	label_name.setIcon(getIconFor(element));
    	
    	if (element instanceof SimpleObject) {
    		label_value.setText( VALUE_PREFIX + ((SimpleObject)element).getValue().toString() );
    		label_value.setVisible(true);
    	} else
    		label_value.setVisible(false);
    	        
        if (sel)
        	panel_renderer.setBackground( MetalLookAndFeel.getCurrentTheme().getPrimaryControl() );
        else
        	panel_renderer.setBackground(null);
        
        
        setForeground(getColorFor(element));

        if (!tree.isEnabled()) {
            setEnabled(false);
        } else {
            setEnabled(true);
        }
        setComponentOrientation(tree.getComponentOrientation());
        selected = sel;
        
        panel_renderer.revalidate();
        
        return panel_renderer;
    }



}