package jnet.client.gui;

import jnet.client.Client;
import jnet.client.gui.dialog.AddMapDialog;
import jnet.client.network.NettyClient;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Collections;
import javax.swing.ImageIcon;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeSelectionModel;
import jnet.lib.LogFile;
import jnet.lib.MapComparator;
import jnet.lib.Message;
import jnet.lib.object.Map;

public class MapTree extends JTree {

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

    private static JTree tree;

    public MapTree() {

        tree = this;

        // 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());

        //this.setComponentPopupMenu(getPopUpMenu());
        MouseListener ml = new MouseAdapter() {

            @Override
            public void mousePressed(MouseEvent e) {
                DefaultMutableTreeNode node = (DefaultMutableTreeNode) getLastSelectedPathComponent();
                if (SwingUtilities.isRightMouseButton(e)) {
                    // right click
                    if (node != null) {
                        Object obj = node.getUserObject();

                        getContextMenuMap(e, (Map) obj);

                    }

                } else if (SwingUtilities.isLeftMouseButton(e)) {
                    // left click
                    if (!node.isRoot()) {
                        if (e.getClickCount() == 1) { // jednoduchy klik

                        } else if (e.getClickCount() == 2) { // dvojklik
                            Object obj = node.getUserObject();
                            Window.setMapView((Map) obj);
                        }
                    }
                }

            }

        };

        addMouseListener(ml);

    }

    private static void getContextMenuMap(MouseEvent e, Map m) {
        JMenuItem mapAdd = new JMenuItem("Přidat mapu", new ImageIcon("img/add.png"));
        mapAdd.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new AddMapDialog().setVisible(true);
            }
        });

        JMenuItem mapDelete = new JMenuItem("Smazat mapu", new ImageIcon("img/trash.png"));
        mapDelete.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int option = JOptionPane.showConfirmDialog(
                        null,
                        "Smazat mapu " + m.getName() + " a všechny její objekty ?",
                        "Potvrzení akce",
                        JOptionPane.YES_NO_OPTION
                );

                if (option == JOptionPane.YES_OPTION) {
                    NettyClient.send(Message.MAP_REMOVE, m.getId());
                }
            }
        });

        JPopupMenu menu = new JPopupMenu();
        if (Client.user.isAddMap()) {
            menu.add(mapAdd);
        }
        if (Client.user.isRemoveMap()) {
            menu.add(mapDelete);
        }
        menu.show(e.getComponent(), e.getX(), e.getY());
    }

    private static void expandAllNodes(JTree tree, int startingIndex, int rowCount) {
        for (int i = startingIndex; i < rowCount; ++i) {
            tree.expandRow(i);
        }

        if (tree.getRowCount() != rowCount) {
            expandAllNodes(tree, rowCount, tree.getRowCount());
        }
    }

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

}
