package view.dashboard;

import controller.history.Application_Controller;
import controller.payment.Add_Payment_Controller;
import controller.state.Search_State_Controller;
import model.Site_Without_Commande;
import model.Week;
import net.miginfocom.swing.MigLayout;
import org.sig.fw.database.model.DBModel;
import org.sig.fw.db.corrida.dao.Facture_DBAO;
import org.sig.fw.db.corrida.dao.History_DBAO;
import org.sig.fw.db.corrida.dao.Relance_DBAO;
import org.sig.fw.db.corrida.dao.State_DBAO;
import org.sig.fw.db.corrida.model.Commande;
import org.sig.fw.db.corrida.model.Facture;
import org.sig.fw.db.corrida.model.History;
import org.sig.fw.db.corrida.model.State;
import org.sig.fw.layout.menu.SMenuItem;
import org.sig.fw.layout.pane.SOptionPane;
import org.sig.fw.layout.pane.SPanel;
import org.sig.fw.utility.date.SDate;
import org.sig.fw.utility.model.Devise;
import renderer.dashboard.New_Commande_Renderer;
import renderer.table.Facture_Paiement_Wait_Renderer;
import table.dashboard.Commande_To_Facture_Table;
import table.dashboard.Facture_Paiement_Wait_Table;
import table.dashboard.New_Commande_Table;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;

public class Dashboard_View extends SPanel
{
	private static final long serialVersionUID = 1348967763418701898L;
	private Dashboard_Content dc_commande;
	private Dashboard_Content dc_commande_to_facture;
	private Dashboard_Content dc_facture_paiment_wait;
	private Dashboard_Content dc_relance_wait;

	public Dashboard_View(Application_Controller ctrl_application)
	{
		setOpaque(false);
	    setBounds(0, 0, 1920, 1022);
	    setLayout(new MigLayout("", "[][grow][][grow][]", "[][grow][][grow][]"));
	    
	    dc_commande = new Dashboard_Content(ctrl_application, "Commandes à Traiter", false) 
	    {
			private static final long serialVersionUID = 2313418478752165523L;

			@Override
			public void setModel() 
			{
				this.jb_update.setEnabled(false);
				this.jt_table.setModel(new New_Commande_Table());
				this.jt_table.setDefaultRenderer(String.class, new New_Commande_Renderer());
				this.jt_table.setDefaultRenderer(Week.class, new New_Commande_Renderer());
				updateModelSize();
				this.jl_title.setText("Commandes à Traiter (" + jt_table.getModel().getRowCount() + ")");
				this.jb_update.setEnabled(true);
			}
			
			@Override
			public void updateModelSize() 
			{
				int scroll_bar_width = this.jsp_table.getVerticalScrollBar().isVisible() ? this.jsp_table.getVerticalScrollBar().getWidth() : 0;
				this.jt_table.configureColumn(0, 0, 0, 0);
				this.jt_table.configureColumn(1, 0, 0, 0);	
				this.jt_table.configureColumn(2, 70, 70, 70);	
				this.jt_table.configureColumn(3, 70, 70, 70);	
				this.jt_table.configureColumn(4, (this.getWidth()-70-130-80-10-scroll_bar_width-70)/3, 100, 800);	
				this.jt_table.configureColumn(5, (this.getWidth()-70-130-80-10-scroll_bar_width-70)/3, 100, 800);	
				this.jt_table.configureColumn(6, (this.getWidth()-70-130-80-10-scroll_bar_width-70)/3, 100, 800);	
				this.jt_table.configureColumn(7, 130, 130, 130);							
				this.jt_table.configureColumn(8, 80, 80, 80);
			}
			
			@Override
			public void openView(Object arg0) 
			{
				if(((Site_Without_Commande) arg0).getConstructionSite() == null)
					SOptionPane.showErrorMessage("Ouverture de commande", "Impossible de créer une commande pour ce dossier : aucune tranche de renseigné !");
				else
					ctrl_application.openView(Application_Controller.ADD_COMMANDE_VIEW, (Site_Without_Commande) arg0, null);
			}

			@Override
			public void openMenu(Object arg0, MouseEvent arg1)
			{
				JPopupMenu popup = new JPopupMenu();
				SMenuItem item_open_detail = new SMenuItem("Detail du Dossier");
				item_open_detail.addActionListener(new ActionListener() 
				{
					public void actionPerformed(ActionEvent arg2)
					{
						if(((Site_Without_Commande) arg0).getConstructionSite() == null)
							SOptionPane.showErrorMessage("Ouverture du détail", "Impossible d'ouvrir le détail : aucune tranche de renseigné !");
						else
							ctrl_application.openView(Application_Controller.DETAIL_VIEW, ((Site_Without_Commande) arg0).getDossier(), ((Site_Without_Commande) arg0).getConstructionSite());
					}
				});
				popup.add(item_open_detail);

				popup.setVisible(true);
				popup.show(jt_table, arg1.getX(), arg1.getY());
			}

			@Override
			public void openOptionMenu() { }
		};
		add(dc_commande, "cell 1 1,grow");
		
		
		dc_commande_to_facture = new Dashboard_Content(ctrl_application, "Commandes en cours de Facturations", false) 
		{
			private static final long serialVersionUID = 2313418478752165523L;

			@Override
			public void setModel() 
			{
				this.jb_update.setEnabled(false);
				this.jt_table.setModel(new Commande_To_Facture_Table());
				updateModelSize();
				this.jl_title.setText("Commandes en cours de Facturations (" + jt_table.getModel().getRowCount() + ")");
				this.jb_update.setEnabled(true);
			}
			
			@Override
			public void updateModelSize() 
			{
				this.jt_table.configureColumn(0, 0, 0, 0);
				this.jt_table.configureColumn(1, 70, 70, 70);	
				this.jt_table.configureColumn(2, 100, 100, 100);	
				this.jt_table.configureColumn(3, (this.getWidth()-70-100-100-100-130-80-10-50)/3, 50, 800);	
				this.jt_table.configureColumn(4, (this.getWidth()-70-100-100-100-130-80-10-50)/3, 50, 800);	
				this.jt_table.configureColumn(5, (this.getWidth()-70-100-100-100-130-80-10-50)/3, 50, 800);	
				this.jt_table.configureColumn(6, 100, 100, 100);	
				this.jt_table.configureColumn(7, 100, 100, 100);	
				this.jt_table.configureColumn(8, 130, 130, 130);	
				this.jt_table.configureColumn(9, 50, 50, 50);	
				this.jt_table.configureColumn(10, 80, 80, 80);
			}
			
			@Override
			public void openView(Object arg0) 
			{
				ctrl_application.openView(Application_Controller.ADD_FACTURE_VIEW, (Commande) arg0, null);
			}

			@Override
			public void openMenu(Object arg0, MouseEvent arg1) 
			{
				JPopupMenu popup = new JPopupMenu();
				SMenuItem item_open_commande = new SMenuItem("Ouvrir la Commande");
				item_open_commande.addActionListener(new ActionListener() 
				{
					public void actionPerformed(ActionEvent arg2)
					{
						ctrl_application.openView(Application_Controller.COMMANDE_VIEW, arg0, null);
					}
				});
				popup.add(item_open_commande);
				SMenuItem item_open_detail = new SMenuItem("Detail du Dossier");
				item_open_detail.addActionListener(new ActionListener() 
				{
					public void actionPerformed(ActionEvent arg2)
					{
						ctrl_application.openView(Application_Controller.DETAIL_VIEW, ((Commande) arg0).getDossier(), ((Commande) arg0).getConstructionSite(), arg0);
					}
				});
				popup.add(item_open_detail);

				popup.setVisible(true);
				popup.show(jt_table, arg1.getX(), arg1.getY());
			}

			@Override
			public void openOptionMenu() { }
		};
		add(dc_commande_to_facture, "cell 3 1,grow");
		
		dc_facture_paiment_wait = new Dashboard_Content(ctrl_application, "Factures en attente de Paiement(s)", false) 
		{
			private static final long serialVersionUID = 2313418478752165523L;

			@Override
			public void setModel() 
			{
				this.jb_update.setEnabled(false);
				this.jt_table.setModel(new Facture_Paiement_Wait_Table());
				this.jt_table.setDefaultRenderer(String.class, new Facture_Paiement_Wait_Renderer());
				this.jt_table.setDefaultRenderer(Devise.class, new Facture_Paiement_Wait_Renderer());
				this.jt_table.setDefaultRenderer(SDate.class, new Facture_Paiement_Wait_Renderer());
				updateModelSize();
				this.jl_title.setText("Factures en attente de Paiement(s) (" + jt_table.getModel().getRowCount() + ")");
				this.jb_update.setEnabled(true);
			}
			
			@Override
			public void updateModelSize() {
				this.jt_table.configureColumn(0, 0, 0, 0);
				this.jt_table.configureColumn(1, 70, 70, 70);
				this.jt_table.configureColumn(2, (this.getWidth()-70-100-80-10-100-100-80-50-330)/4, 50, 500);
				this.jt_table.configureColumn(3, (this.getWidth()-70-100-80-10-100-100-80-50-330)/4, 50, 500);
				this.jt_table.configureColumn(4, (this.getWidth()-70-100-80-10-100-100-80-50-330)/4, 50, 400);
				this.jt_table.configureColumn(5, (this.getWidth()-70-100-80-10-100-100-80-50-330)/4, 50, 400);
				this.jt_table.configureColumn(6, 100, 100, 100);
				this.jt_table.configureColumn(7, 100, 100, 100);
				this.jt_table.configureColumn(8, 80, 80, 80);
				this.jt_table.configureColumn(9, 90, 90, 90);
				this.jt_table.configureColumn(10, 70, 70, 70);
				this.jt_table.configureColumn(11, 70, 70, 70);
				this.jt_table.configureColumn(12, 90, 90, 90);
				this.jt_table.configureColumn(13, 150, 150, 150);
				this.jt_table.configureColumn(14, 90, 90, 90);
			}
			
			@Override
			public void openView(Object arg0) 
			{
				new Add_Payment_Controller(ctrl_application, (Facture) arg0);
				setModel();
			}

			@Override
			public void openMenu(Object arg0, MouseEvent arg1)
			{
				JPopupMenu popup = new JPopupMenu();
				SMenuItem item_open_facture = new SMenuItem("Ouvrir la Facture");
				item_open_facture.addActionListener(new ActionListener() 
				{
					public void actionPerformed(ActionEvent arg2)
					{
						ctrl_application.openView(Application_Controller.FACTURE_VIEW, arg0, null);
					}
				});
				popup.add(item_open_facture);
				SMenuItem item_open_detail = new SMenuItem("Detail du Dossier");
				item_open_detail.addActionListener(new ActionListener() 
				{
					public void actionPerformed(ActionEvent arg2)
					{
						ctrl_application.openView(Application_Controller.DETAIL_VIEW, ((Facture) arg0).getCommande().getDossier(), ((Facture) arg0).getCommande().getConstructionSite(), arg0);
					}
				});
				popup.add(item_open_detail);

				// 2024 - AJOUT D'UN SOUS MENU POUR REMPLACER DOUBLE CLIC DE LA FENETRE "RELANCE A EFFECTUER" APRES LA SUPPRESSION DE CELLE-CI
				SMenuItem item_add_relance = new SMenuItem("Ajout d'une relance");
				item_add_relance.addActionListener(new ActionListener()
				{
					public void actionPerformed(ActionEvent arg3)
					{
						DBModel model = ((Facture) arg0).getRelance();
						ctrl_application.openView(Application_Controller.ADD_RELANCE_VIEW, model, null);
					}
				});
				popup.add(item_add_relance);

				popup.addSeparator();

				SMenuItem item_change_state = new SMenuItem("Changer l'état de la Facture");
				item_change_state.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						Facture f = (Facture) arg0;
						
						State new_state = (State) new Search_State_Controller(ctrl_application).show(f.getState().getShortValue());

						if(new_state != null)
						{
							new Facture_DBAO("fact").updateState(f, new_state.getDbId());
							History h = new History(-1, History_DBAO.FACTURE, f, ctrl_application.getConnectedAccount(), new SDate(), "Passage de l'état " + f.getState() + " à l'état " + new_state);
							new History_DBAO("h").insert(h);
							if(new_state.getDbId() == State_DBAO.SOLDE)
								new Relance_DBAO("rel", -1).deleteWithModel(f);
						}
						updateModel();
					}
				});
				popup.add(item_change_state);
				
				popup.setVisible(true);
				popup.show(jt_table, arg1.getX(), arg1.getY());
			}

			@Override
			public void openOptionMenu() { }
		};
		add(dc_facture_paiment_wait, "cell 0 2, spanx, spany, grow");

//		dc_relance_wait = new Dashboard_Content(ctrl_application, "Relance à effectuer", true)
//		{
//			private static final long serialVersionUID = 2313418478752165523L;
//
//			@Override
//			public void setModel()
//			{
//				this.jb_update.setEnabled(false);
//				List<DBModel> models = new Relance_DBAO("rel", -1).getAll(true);
//				List<Relance> relance = new ArrayList<Relance>();
//				for(DBModel model : models) {
//					relance.add((Relance) model);
//				}
//				Collections.sort(relance);
//				this.jt_table.setModel(new Relance_To_Do_Table(relance));
//				this.jt_table.setDefaultRenderer(String.class, new Relance_To_Do_Renderer());
//				this.jt_table.setDefaultRenderer(Devise.class, new Relance_To_Do_Renderer());
//				this.jt_table.setDefaultRenderer(SDate.class, new Relance_To_Do_Renderer());
//				updateModelSize();
//				this.jl_title.setText("Relance à effectuer (" + jt_table.getModel().getRowCount() + ")");
//				this.jb_update.setEnabled(true);
//			}
//
//			@Override
//			public void updateModelSize()
//			{
//				this.jt_table.configureColumn(0, 0, 0, 0);
//				this.jt_table.configureColumn(1, 70, 70, 70);
//				this.jt_table.configureColumn(2, (this.getWidth()-70-95-100-80-9-80-50)/4, 50, 800);
//				this.jt_table.configureColumn(3, (this.getWidth()-70-95-100-80-9-80-50)/4, 50, 800);
//				this.jt_table.configureColumn(4, 95, 95, 95);
//				this.jt_table.configureColumn(5, 100, 100, 100);
//				this.jt_table.configureColumn(6, (this.getWidth()-70-95-100-80-9-80-50)/4, 50, 800);
//				this.jt_table.configureColumn(7, (this.getWidth()-70-95-100-80-9-80-50)/4, 50, 800);
//				this.jt_table.configureColumn(8, 80, 80, 80);
//				this.jt_table.configureColumn(9, 50, 50, 50);
//				this.jt_table.configureColumn(10, 80, 80, 80);
//			}
//
//			@Override
//			public void openView(Object arg0)
//			{
//				new Add_Relance_Controller(ctrl_application, (Relance) arg0);
//				setModel();
//			}
//
//			@Override
//			public void openMenu(Object arg0, MouseEvent arg1)
//			{
//				JPopupMenu popup = new JPopupMenu();
//				SMenuItem item_open_detail = new SMenuItem("Detail du Dossier");
//				item_open_detail.addActionListener(new ActionListener()
//				{
//					public void actionPerformed(ActionEvent arg2)
//					{
//						DBModel model = ((Relance)arg0).getObject();
//						if(model instanceof Facture)
//							ctrl_application.openView(Application_Controller.DETAIL_VIEW, ((Facture) model).getCommande().getDossier(), ((Facture) model).getCommande().getConstructionSite(), arg0);
//						else if(model instanceof Commande)
//							ctrl_application.openView(Application_Controller.DETAIL_VIEW, ((Commande) model).getDossier(), ((Commande) model).getConstructionSite(), arg0);
//					}
//				});
//				popup.add(item_open_detail);
//
//				popup.addSeparator();
//
//				SMenuItem item_change_state = new SMenuItem("Changer l'état de la Facture");
//				item_change_state.addActionListener(new ActionListener() {
//					@Override
//					public void actionPerformed(ActionEvent e) {
//						DBModel model = ((Relance)arg0).getObject();
//						Facture f = (Facture) model;
//
//						State new_state = (State) new Search_State_Controller(ctrl_application).show(f.getState().getShortValue());
//
//						if(new_state != null)
//						{
//							new Facture_DBAO("fact").updateState(f, new_state.getDbId());
//							History h = new History(-1, History_DBAO.FACTURE, f, ctrl_application.getConnectedAccount(), new SDate(), "Passage de l'état " + f.getState() + " à l'état " + new_state);
//							new History_DBAO("h").insert(h);
//							if(new_state.getDbId() == State_DBAO.SOLDE)
//								new Relance_DBAO("rel", -1).deleteWithModel(f);
//						}
//						updateModel();
//					}
//				});
//				popup.add(item_change_state);
//
//				popup.setVisible(true);
//				popup.show(jt_table, arg1.getX(), arg1.getY());
//			}
//
//			@Override
//			public void openOptionMenu() {
//				JPopupMenu popup = new JPopupMenu();
//
//				SMenuItem item_decal_auto = new SMenuItem("Reporter la sélection de 10 jours");
//				item_decal_auto.addActionListener(new ActionListener() {
//					@Override
//					public void actionPerformed(ActionEvent e) {
//						int[] selections = jt_table.getSelectedRows();
//						if(selections.length == 0) {
//							SOptionPane.showErrorMessage("Relances", "Aucune sélection...");
//							return;
//						}
//
//						List<Relance> relances = new ArrayList<Relance>();
//						int column = -1;
//		    			for(int i = 0; i < jt_table.getColumnCount(); i++)
//		    			{
//		    				if(jt_table.getColumnName(i).equals("Class"))
//		    					column = i;
//		    			}
//
//		    			for(int row : selections) {
//		    				relances.add((Relance) jt_table.getValueAt(row, column));
//		    			}
//
//		    			for(Relance relance : relances) {
//		    				Relance_History rh = new Relance_History(relance);
//		    				rh.setActionDate(new SDate());
//		    				rh.setAccount(ctrl_application.getConnectedAccount());
//		    				rh.setObservation("Report de 10 jours.");
//		    				rh.setDbId(new Relance_History_DBAO("rh").insert(rh));
//
//		    				History history = new History(
//		    						-1,
//		    						History_DBAO.RELANCE,
//		    						rh,
//		    						ctrl_application.getConnectedAccount(),
//		    						new SDate(),
//		    						"Report d'une Relance.");
//		    				new History_DBAO("h").insert(history);
//
//		    				SDate dt = relance.getEcheanceDate();
//		    				Calendar c = Calendar.getInstance();
//		    				c.setTime(dt.getDate());
//		    				c.add(Calendar.DATE, 10);
//		    				dt = new SDate(c.getTime());
//
//		    				relance.setEcheanceDate(dt);
//		    				new Relance_DBAO("rel", -1).update(relance);
//		    			}
//		    			setModel();
//					}
//				});
//				popup.add(item_decal_auto);
//
//				SMenuItem item_decal_manu = new SMenuItem("Reporter la sélection de X jours");
//				item_decal_manu.addActionListener(new ActionListener() {
//					@Override
//					public void actionPerformed(ActionEvent e) {
//						int[] selections = jt_table.getSelectedRows();
//						if(selections.length == 0) {
//							SOptionPane.showErrorMessage("Relances", "Aucune sélection...");
//							return;
//						}
//
//						List<Relance> relances = new ArrayList<Relance>();
//						int column = -1;
//		    			for(int i = 0; i < jt_table.getColumnCount(); i++)
//		    			{
//		    				if(jt_table.getColumnName(i).equals("Class"))
//		    					column = i;
//		    			}
//
//		    			for(int row : selections) {
//		    				relances.add((Relance) jt_table.getValueAt(row, column));
//		    			}
//
//		    			int decal = new Report_Relance_View().getValue();
//
//		    			if(decal == 0) {
//		    				return;
//		    			}
//
//		    			for(Relance relance : relances) {
//		    				Relance_History rh = new Relance_History(relance);
//		    				rh.setActionDate(new SDate());
//		    				rh.setAccount(ctrl_application.getConnectedAccount());
//		    				rh.setObservation("Report de " + decal + " jours.");
//		    				rh.setDbId(new Relance_History_DBAO("rh").insert(rh));
//
//		    				History history = new History(
//		    						-1,
//		    						History_DBAO.RELANCE,
//		    						rh,
//		    						ctrl_application.getConnectedAccount(),
//		    						new SDate(),
//		    						"Report d'une Relance.");
//		    				new History_DBAO("h").insert(history);
//
//		    				SDate dt = relance.getEcheanceDate();
//		    				Calendar c = Calendar.getInstance();
//		    				c.setTime(dt.getDate());
//		    				c.add(Calendar.DATE, decal);
//		    				dt = new SDate(c.getTime());
//
//		    				relance.setEcheanceDate(dt);
//		    				new Relance_DBAO("rel", -1).update(relance);
//		    			}
//		    			setModel();
//					}
//				});
//				popup.add(item_decal_manu);
//				popup.setVisible(true);
//
//				popup.show(this.jb_option, jb_option.getX(), jb_option.getY()+20);
//			}
//		};
//		dc_relance_wait.jt_table.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
//		add(dc_relance_wait, "cell 3 3,grow");
	}
	
	public void updateAll()
	{
		dc_commande.updateModelSize();
		dc_commande_to_facture.updateModelSize();
		dc_relance_wait.setModel();
		dc_facture_paiment_wait.updateModelSize();
	}
	
	public void updateModel()
	{
		new Thread(new Runnable() 
		{
			@Override
			public void run()
			{
				dc_commande.setModel();
			}
		}).start();
		new Thread(new Runnable() 
		{
			@Override
			public void run()
			{
				dc_commande_to_facture.setModel();
			}
		}).start();
		new Thread(new Runnable() 
		{
			@Override
			public void run()
			{
				dc_facture_paiment_wait.setModel();
			}
		}).start();
//		new Thread(new Runnable()
//		{
//			@Override
//			public void run()
//			{
//				dc_relance_wait.setModel();
//			}
//		}).start();
	}
}
