package view;

import controller.history.Application_Controller;
import main.Main;
import org.sig.fw.layout.component.SLabel;
import org.sig.fw.layout.contener.SFrame;
import org.sig.fw.layout.pane.SOptionPane;
import view.dashboard.Dashboard_View;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class Application_view extends SFrame
{
	private static final long serialVersionUID = -5668960927971248404L;

	private Application_Controller ctrl_application;
	
	private JDesktopPane desktopPane;
	
	private Dashboard_View vw_dashboard;
	
	private SLabel maintenance_text;
	
	public Application_view(Application_Controller ctrl_application)
	{
		super(true);
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosed(WindowEvent e) 
			{
				removeAllFrame();
				ctrl_application.closeApplication();
				System.exit(0);
			}
		});
		this.ctrl_application = ctrl_application;
		
	    this.setVisible(true);
	    this.setDefaultCloseOperation(SFrame.DISPOSE_ON_CLOSE);
	    this.setApplicationTitle("");
	    getContentPane().setLayout(new BorderLayout(0, 0));
	    
	    desktopPane = new JDesktopPane();
	    getContentPane().add(desktopPane, BorderLayout.CENTER);
	    
	    JMenuBar menuBar = new Menu_View(ctrl_application);
	    setJMenuBar(menuBar);
	    
	    maintenance_text = new SLabel("", SLabel.TYPE_ERROR);
	    maintenance_text.setHorizontalAlignment(SwingConstants.CENTER);
	    getContentPane().add(maintenance_text, BorderLayout.NORTH);
	    
	    this.revalidate();
	    
	    addComponentListener(new ComponentListener() 
		{
		    public void componentResized(ComponentEvent e)
		    {
		    	if(vw_dashboard != null)
		    	{
			        int width = getSize().width-getInsets().left-getInsets().right;
			        int height = getSize().height-getInsets().top-getInsets().bottom - getJMenuBar().getHeight();
			        vw_dashboard.setBounds(new Rectangle(0,0,width, height));
			        vw_dashboard.validate();
		    	}
		    }

			@Override
			public void componentHidden(ComponentEvent arg0) { }

			@Override
			public void componentMoved(ComponentEvent arg0) { }

			@Override
			public void componentShown(ComponentEvent arg0) { }
		});
	}
	
	public void setMaintenanceText(String text)
	{
		maintenance_text.setText(text);
	}
	
	public void configureDashboard(boolean activate)
	{
		if(activate)
		{
			this.vw_dashboard = new Dashboard_View(ctrl_application);
			int width = getSize().width-getInsets().left-getInsets().right;
	        int height = getSize().height-getInsets().top-getInsets().bottom - getJMenuBar().getHeight();
	        vw_dashboard.setBounds(new Rectangle(0,0,width, height));
	        vw_dashboard.validate();
			this.desktopPane.add(this.vw_dashboard);
			this.updateDashboard();
		}
		else
		{
			this.desktopPane.remove(this.vw_dashboard);
			this.vw_dashboard = null;
		}
	}
	
	public void updateDashboard()
	{
		this.vw_dashboard.updateModel();
	}
	
	public void setApplicationTitle(String user_name)
	{
		String title = Main.APP_NAME + " - ";
		
		if(user_name.equals(""))
		{
			title += "V" + Main.APP_VERSION;
		}
		else
		{
			title += user_name + " - V" + Main.APP_VERSION;
		}
		
		if(Main.onDev)
		{
			title += " - ON DEV"; 
		}
		
		this.setTitle(title);
	}
	
	public void differents_version(boolean isNecessary)
	{
		if(isNecessary)
		{
			SOptionPane.showInformationMessage(
					"Mise à jour nécessaire",
					"Vous ne possédez pas la dernière version du logiciel. Une mise à jour est nécessaire pour continuer.");
			this.dispose();
		}
		else
		{
			SOptionPane.showInformationMessage( 
					"Mise à jour disponible",
					"Une mise à jour est disponible, mais n'est pas obligatoire.");
		}
	}
	
	public void maintenance()
	{
		int result = SOptionPane.showOkCancelMessage(
				"Maintenance en cours !",
				"Une maintenance est en cours... Veuillez réessayer plus tard.");
		
		if(result == SOptionPane.OK_OPTION)
			this.ctrl_application.init();
		else
			this.dispose();
	}
	
	public void addView(JInternalFrame v)
	{
		if(verifyInternalFrame(v))
		{
			v.setBounds(getWidth()/2-v.getWidth()/2, getHeight()/2-v.getHeight()/2, v.getWidth(), v.getHeight());
			this.desktopPane.add(v);
			v.moveToFront();
		}
	}
	
	public void removeAllFrame()
	{
		JInternalFrame[] vs = desktopPane.getAllFrames();
		for(JInternalFrame f : vs)
		{
			f.dispose();
			this.desktopPane.remove(f);
		}
		this.desktopPane.repaint();
		this.repaint();
	}
	
	public JInternalFrame[] getAllFrame()
	{
		return desktopPane.getAllFrames();
	}
	
	public void reconfigureView()
	{
		JInternalFrame[] vs = desktopPane.getAllFrames();
		for(JInternalFrame v : vs)
		{
			if(v.getWidth() > getWidth()-50)
				v.setBounds(v.getX(), v.getY(), getWidth()-50, v.getHeight());
			if(v.getHeight() > getHeight()-50)
				v.setBounds(v.getX(), v.getY(), v.getWidth(), getHeight()-150);
			v.setBounds(getWidth()/2-v.getWidth()/2, getHeight()/2-v.getHeight()/2, v.getWidth(), v.getHeight());
			v.moveToFront();
		}
	}
	
	public boolean verifyInternalFrame(JInternalFrame v)
	{
		JInternalFrame[] vs = desktopPane.getAllFrames();
		for(JInternalFrame f : vs)
		{
			if(v.getClass().equals(f.getClass()))
			{
				f.setBounds(getWidth()/2-v.getWidth()/2, getHeight()/2-v.getHeight()/2, v.getWidth(), v.getHeight());
				f.moveToFront();
				return false;
			}
		}
		return true;
	}
	
	public boolean verifyInternalFrame(Class<?> v)
	{
		JInternalFrame[] vs = desktopPane.getAllFrames();
		for(JInternalFrame f : vs)
		{
			if(v.equals(f.getClass()))
			{
				return false;
			}
		}
		return true;
	}
}
