package jguard.client.gui;

import cucky.jGuard.lib.LogFile;
import cucky.jGuard.lib.MapComparator;
import cucky.jGuard.lib.Status;
import cucky.jGuard.lib.object.Map;
import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Collections;
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.*;
import jguard.client.Client;

public class MapTree extends JTree {

    static DefaultTreeModel model;
    static DefaultMutableTreeNode root = new DefaultMutableTreeNode("Mapy");

    public MapTree() {
        
        // neni videt root element
        this.setRootVisible(false);
        // povoli pouze jeden vybrany radek
        this.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
        
        model = new DefaultTreeModel(root);
        this.setModel(model);

        this.setCellRenderer(new MapTreeCellRenderer());

        MouseListener ml = new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                DefaultMutableTreeNode node = (DefaultMutableTreeNode) getLastSelectedPathComponent();
                if (!node.isRoot()) {
                    if (e.getClickCount() == 1) { // jednoduchy klik

                    } else if (e.getClickCount() == 2) { // dvojklik
                        Window.setMapView(node.getParent().getIndex(node));
                    }
                }
            }
     
        };

        addMouseListener(ml);

    }

    public static void add() { 
        root.removeAllChildren();
        Collections.sort(Client.map, new MapComparator());
        for (Map map : Client.map) {
            DefaultMutableTreeNode node = new DefaultMutableTreeNode(map);
            root.add(node);
        }
        try {
          model.reload();  
        } catch (Exception e) {
            LogFile.printErr("Map tree error: " +e);
        }
        
    }
    
    public static void clear(){
        root.removeAllChildren();
        model.reload();
    }
    
    
    public static void reload(){
        model.reload();
    }

    private 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;
                    switch (m.getMapStatus()) {
                        case Status.OK: // online
                            image = ImageIO.read(new File("img/flag_green.png"));
                            break;
                        case Status.WARNING: // warning
                            image = ImageIO.read(new File("img/flag_orange.png"));
                            break;
                        case Status.OFFLINE: // offline
                            image = ImageIO.read(new File("img/flag_red.png"));
                            break;
                        default: // other
                            image = ImageIO.read(new File("img/flag_gray.png"));
                    }
                    if (image != null) {
                        label.setIcon(new ImageIcon(image));
                    }
                    label.setText(m.getName());
                } catch (Exception ex) {
                    System.err.println("Error tree icon: " + ex.getMessage());
                }
            } else {
                label.setIcon(null);
                label.setText(String.valueOf(value));
            }
            
            // EmptyBorder(int top, int left, int bottom, int right)
            EmptyBorder border = new EmptyBorder(7, 25, 3, 3);
            label.setBorder(border);
            return label;
        }
    }

}
