package jnet.client.gui;

import java.awt.Component;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JTree;
import javax.swing.border.EmptyBorder;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import jnet.lib.Status;
import jnet.lib.object.Map;

public class MapTreeCellRenderer extends DefaultTreeCellRenderer {

    private JLabel label;
    private BufferedImage image;

    public MapTreeCellRenderer() {
        label = new JLabel();

    }

    @Override
    public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
        label = (JLabel) super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
        Object o = ((DefaultMutableTreeNode) value).getUserObject();
        if (o instanceof Map) {
            try {
                Map m = (Map) o;
                String imagePath = "";
                switch (m.getStatus()) {
                    case Status.OK: // online
                        imagePath = "/img/flag_green.png";
                        break;
                    case Status.WARNING: // warning
                        imagePath = "/img/flag_orange.png";
                        break;
                    case Status.OFFLINE: // offline
                        imagePath = "/img/flag_red.png";
                        break;
                    default: // other
                        imagePath = "/img/flag_gray.png";
                }
                image = ImageIO.read(getClass().getResourceAsStream(imagePath));

                if (image != null) {
                    label.setIcon(new ImageIcon(image));
                }
                label.setText(m.getName());
            } catch (Exception ex) {
                System.err.println("Error tree icon: " + ex.getMessage());
            }
        }
        // EmptyBorder(int top, int left, int bottom, int right)
        EmptyBorder border = new EmptyBorder(7, 25, 3, 3);
        label.setBorder(border);
        return label;
    }

}
