package org.wikiwebserver.core;

import java.awt.BorderLayout;
import java.awt.Color;
import java.io.File;
import java.text.DateFormat;
import java.text.NumberFormat;
import java.util.Date;
import java.util.Map;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;

public class WikiMapViewer extends JPanel {

    private static final long serialVersionUID = 1L;

    public static void main(String[] args) {
        final WikiMap map = new WikiMap("root", new File(WareHouse.MAP_ROOT));  
        
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                public void run() {
                    JFrame frame = new JFrame("WikiMap Viewer");
                    
                    WikiMapViewer viewer = new WikiMapViewer(map);
                    frame.getContentPane().add(new JScrollPane(viewer));
                    frame.pack();
                    frame.setVisible(true);
                }
            });
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    
    
    private WikiMap map;
    
    public WikiMapViewer(WikiMap map) {
        this.map = map;
        setLayout(new BorderLayout());
        add(getWikiMapPropertiesPanel(), BorderLayout.CENTER);
        
        JPanel s = null;
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            JPanel labelPanel = new JPanel(new BorderLayout());
            JLabel label = new JLabel(entry.getKey());
            label.setBorder(new EmptyBorder(3, 3, 3, 3));
            label.setForeground(Color.blue);
            labelPanel.add(label, BorderLayout.NORTH);
            if (entry.getValue() instanceof WikiMap) {
                s = stack(s, labelPanel, new WikiMapViewer((WikiMap)entry.getValue()));
            }
            else {
                JPanel valuePanel = new JPanel(new BorderLayout());
                JLabel value = new JLabel(entry.getValue().toString());
                valuePanel.add(value, BorderLayout.CENTER);
                s = stack(s, labelPanel, valuePanel);
            }
        }

        if (s != null) {
            add(s, BorderLayout.SOUTH);
        }
        
        setBorder(new LineBorder(Color.blue));
    }
    
    public JPanel getWikiMapPropertiesPanel() {
        
        JPanel propsPanel = new JPanel();
        propsPanel.setBorder(new LineBorder(Color.black));
        propsPanel.setLayout(new BorderLayout());
        
        DateFormat df = DateFormat.getInstance();
        NumberFormat nf = NumberFormat.getInstance();
        
        JPanel s = stringStack(null, "Name", map.getName());
        if (map.size() > -1) {
	        s = stringStack(s, "Persist path", map.getPersistPath());
	        s = stringStack(s, "Last accessed", df.format(new Date(map.getLastAccessTime())));
	        s = stringStack(s, "Last modified", df.format(new Date(map.getLastModifiedTime())));
	        s = stringStack(s, "Maximum size", nf.format(map.getMaximumSize()));
	        s = stringStack(s, "Accessor class", map.getAccessorClassName());
        }
        
        return s;
    }
    
    public JPanel stringStack(JPanel panel, String name, String value) {
        JLabel nameLabel = new JLabel(name);
        nameLabel.setBorder(new EmptyBorder(3, 3, 3, 3));
        JLabel valueLabel = new JLabel(value);
        valueLabel.setBorder(new EmptyBorder(3, 3, 3, 3));
        return stack(panel, nameLabel, valueLabel);
    }
    
    public JPanel stack(JPanel panel, JComponent label, JComponent value) {
        JPanel itemPanel = new JPanel(new BorderLayout());        
        if (panel != null) {
            itemPanel.add(panel, BorderLayout.NORTH);
        }
        itemPanel.add(label, BorderLayout.WEST);
        itemPanel.add(value, BorderLayout.CENTER);
        
        JPanel s = new JPanel(new BorderLayout());        
        s.add(itemPanel, BorderLayout.NORTH);
        return s;
    }
}
