package view.dashboard;

import controller.history.Application_Controller;
import net.miginfocom.swing.MigLayout;
import org.sig.fw.layout.component.SButton;
import org.sig.fw.layout.component.SLabel;
import org.sig.fw.layout.component.STable;
import org.sig.fw.layout.pane.SPanel;
import org.sig.fw.layout.pane.SScrollPane;

import javax.swing.*;
import javax.swing.border.MatteBorder;
import java.awt.*;
import java.awt.event.*;

public abstract class Dashboard_Content extends SPanel
{
	private static final long serialVersionUID = 6635163272318604419L;
	
	protected STable jt_table;
	protected SScrollPane jsp_table;
	protected SLabel jl_title;
	protected SButton jb_option;
	protected SButton jb_update;
	
	public Dashboard_Content(Application_Controller ctrl_application, String title, boolean left_button_active)
	{
		super();
		addComponentListener(new ComponentAdapter()
		{
			@Override
			public void componentResized(ComponentEvent arg0)
			{
				updateModelSize();
			}
		});
	    setBorder(new MatteBorder(1, 1, 1, 1, (Color) new Color(0, 0, 0)));
	    setLayout(new MigLayout("fill, insets 0, gap -1px -1px", "[][][456px,grow][]", "[][428px,grow]"));
	    
	    jb_option = new SButton("");
	    jb_option.addActionListener(new ActionListener() {
	    	public void actionPerformed(ActionEvent e) {
	    		openOptionMenu();
	    	}
	    });
	    jb_option.setIcon(new ImageIcon(Dashboard_Content.class.getResource("/javax/swing/plaf/metal/icons/sortDown.png")));
	    if(!left_button_active) {
	    	jb_option.setVisible(false);
	    }
	    add(jb_option, "cell 1 0,growy");
	    
	    jl_title = new SLabel(title, SLabel.TYPE_TITLE_BOLD);
	    add(jl_title, "cell 2 0,alignx center");
	    
	    jb_update = new SButton("", "Actualiser");
	    jb_update.addActionListener(new ActionListener() 
	    {
	    	public void actionPerformed(ActionEvent arg0) 
	    	{
	    		new Thread(new Runnable() 
	    		{
	    			@Override
	    			public void run()
	    			{
	    	    		setModel();
	    			}
	    		}).start();
	    	}
	    });
	    jb_update.setIcon(new ImageIcon(Dashboard_View.class.getResource("/com/sun/javafx/scene/web/skin/Redo_16x16_JFX.png")));
	    add(jb_update, "cell 3 0");
	    
	    jt_table = new STable() 
	    {
			private static final long serialVersionUID = -2353027366654397757L;
			
			@Override
			public void rightClick(Object object, MouseEvent arg0) 
			{
				openMenu(object, arg0);
			}
			
			@Override
			public void doubleLeftClick(Object object) 
			{
				openView(object);
			}
		};
	    jt_table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
	    jt_table.setAutoResizeMode(STable.AUTO_RESIZE_OFF);
	    jt_table.setAutoCreateRowSorter(true);
	    jsp_table = new SScrollPane();
	    jsp_table.setViewportView(jt_table);
	    jsp_table.setHorizontalScrollBarPolicy(SScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
	    jsp_table.setVerticalScrollBarPolicy(SScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
	    add(jsp_table, "cell 1 1 3 1,grow");

	    jsp_table.setViewportView(jt_table);
	    
	    SwingUtilities.invokeLater(new Runnable() 
	    {
			@Override
			public void run() 
			{
				 //setModel();
			}
		});
	}
	
	public abstract void setModel();
	public abstract void updateModelSize();
	public abstract void openView(Object arg0);
	public abstract void openMenu(Object arg0, MouseEvent arg1);
	public abstract void openOptionMenu();
}
