package controller.history;

import controller.Check_Maintenance_Controller;
import controller.authentification.Authentification_Controller;
import controller.client.Add_Client_Controller;
import controller.client.Client_Controller;
import controller.client.Search_Client_Controller;
import controller.commande.Add_Commande_Controller;
import controller.commande.Commande_Controller;
import controller.commande.Search_Commande_Controller;
import controller.commande.Search_Commande_Specifique_Controller;
import controller.definition.*;
import controller.detail.Detail_Dossier_Controller;
import controller.export.Export_Factures_Controller;
import controller.export.Export_Month_Facture_Controller;
import controller.facture.Add_Facture_Controller;
import controller.facture.Facture_Controller;
import controller.facture.Search_Facture_Controller;
import controller.facture.Search_Facture_Specifique_Controller;
import controller.note.Export_Notes_Controller;
import controller.note.Notes_Controller;
import controller.payment.Add_Payment_Controller;
import controller.payment.Search_Payment_Controller;
import controller.relance.Add_Relance_Controller;
import main.Main;
import model.Site_Without_Commande;
import org.sig.fw.database.model.DBModel;
import org.sig.fw.db.bddc.model.Construction_Site;
import org.sig.fw.db.bddc.model.Dossier;
import org.sig.fw.db.corrida.model.Commande;
import org.sig.fw.db.corrida.model.Facture;
import org.sig.fw.db.corrida.model.Payment;
import org.sig.fw.db.corrida.model.Relance;
import org.sig.fw.layout.pane.SOptionPane;
import org.sig.fw.log.implement.LogInterface;
import org.sig.fw.log.model.Definition;
import org.sig.fw.sig.db.dao.Software_DBAO;
import org.sig.fw.sig.model.Account;
import org.sig.fw.sig.model.Software;
import org.sig.fw.utility.date.SDate;
import view.Application_view;
import view.LogView;
import view.client.Add_Client_View;
import view.client.Client_View;
import view.client.Search_Client_View;
import view.commande.Add_Commande_View;
import view.commande.Commande_View;
import view.commande.Search_Commande_Specifique_View;
import view.commande.Search_Commande_View;
import view.definition.*;
import view.detail.Detail_Dossier_View;
import view.export.Export_Month_Facture_View;
import view.facture.Add_Facture_View;
import view.facture.Facture_View;
import view.facture.Search_Facture_Specifique_View;
import view.facture.Search_Facture_View;
import view.payment.Search_Payment_View;

import javax.swing.*;

public class Application_Controller implements LogInterface
{
	public static int ADD_COMMANDE_VIEW = 1;
	public static int ADD_FACTURE_VIEW = 2;
	public static int ADD_PAYMENT_VIEW = 3;
	public static int DETAIL_VIEW = 4;
	public static int SEARCH_COMMANDE_VIEW = 5;
	public static int SEARCH_FACTURE_VIEW = 6;
	public static int SEARCH_PAYMENT_VIEW = 7;
	public static int STATE_VIEW = 8;
	public static int REFERENCE_VIEW = 9;
	public static int COMMANDE_REFERENCE_VIEW = 10;
	public static int DEFINITION_VIEW = 11;
	public static int PAYMENT_MODE_VIEW = 12;
	public static int ADD_RELANCE_VIEW = 13;
	public static int HISTORY_VIEW = 14;
	public static int ADD_CLIENT = 15;
	public static int CLIENT = 16;
	public static int SEARCH_CLIENT_VIEW = 17;
	public static int COMMANDE_VIEW = 18;
	public static int FACTURE_VIEW = 19;
	public static int SEARCH_COMMANDE_SPECIFIC_VIEW = 20;
	public static int EXPORT_FACTURE_MONTH = 21;
	public static int SEARCH_FACTURE_SPECIFIC_VIEW = 22;
	public static int EXPORT_FACTURES = 23;
	public static int NOTES_VIEW = 24;
	public static int EXPORT_NOTES = 25;

	private Application_view vw_application;
		
	private Account mdl_connected_account = null;

	private Check_Maintenance_Controller ctrl_check_maintenance;
	
	public Application_Controller()
	{
		super();
		this.vw_application = new Application_view(this);
		this.ctrl_check_maintenance = new Check_Maintenance_Controller(this);
		SwingUtilities.invokeLater(new Runnable()
		{
			@Override
			public void run() 
			{
				init();				
			}
		});
		
	}

	public void init()
	{
		if(!this.version_compare())
		{
			return;
		}
		if(this.mdl_connected_account == null)
		{
			new Authentification_Controller(this);
		}
	}

	private boolean version_compare() {
		Software software = new Software_DBAO("soft").getWithName(Main.APP_NAME);
		if(software.getMaintenanceDate() != null && software.getMaintenanceDate().compareTo(new SDate()) < 0 && software.isMaintenance()) {
			this.vw_application.maintenance();
			return false;
		}
		
		String version = Main.APP_VERSION.split("\\.")[0]+"."+Main.APP_VERSION.split("\\.")[1];
		
		if(!software.getVersion().equals(version)) {
			this.vw_application.differents_version(true);
			return false;
		}
		
		if(!software.getPatch().equals(Main.APP_VERSION.split("\\.")[2])) {
			this.vw_application.differents_version(false);
		}
		
		return true;
	}
	
	public void setConnectedAccount(Account account) {
		this.mdl_connected_account = account;
		this.vw_application.setApplicationTitle(this.mdl_connected_account.getCompleteName());
		this.vw_application.configureDashboard(true);
		this.ctrl_check_maintenance.startCheckThread();
	}
	
	public void deconnection()
	{
		this.vw_application.removeAllFrame();
		this.vw_application.setApplicationTitle("");
		this.mdl_connected_account = null;
		this.vw_application.configureDashboard(false);
		this.ctrl_check_maintenance.stopThread();
		this.init();
	}
	
	public void closeApplication()
	{
		this.ctrl_check_maintenance.stopThread();
		this.vw_application.dispose();
	}
	
	public Application_view getView()
	{
		return this.vw_application;
	}
	
	public Account getConnectedAccount()
	{
		return this.mdl_connected_account;
	}

	public void activeUpdate(String update, int id1, int id2)
	{
		this.vw_application.updateDashboard();
		
		JInternalFrame[] frames = this.vw_application.getAllFrame();

		for(JInternalFrame jif : frames)
		{
			if(update.equals("ADD_FACTURE"))
			{
				if(jif instanceof Detail_Dossier_View)
				{
					((Detail_Dossier_View)jif).refreshTree(id1, id2);
				}
				else if(jif instanceof Add_Commande_View)
				{
					((Add_Commande_View)jif).setReadOnly(id1, id2);
				}
			}
			else if(update.equals("ADD_COMMANDE"))
			{
				if(jif instanceof Detail_Dossier_View)
				{
					((Detail_Dossier_View)jif).refreshTree(id1, id2);
				}
				else if(jif instanceof Add_Facture_View)
				{
					((Add_Facture_View)jif).refresh(id1, id2);
				}
			}
			else if(update.equals("ADD_PAYMENT"))
			{
				if(jif instanceof Detail_Dossier_View)
				{
					((Detail_Dossier_View)jif).refreshTree(id1, id2);
				}
			}
			else if(update.equals("ADD_RELANCE"))
			{
				if(jif instanceof Detail_Dossier_View)
				{
					((Detail_Dossier_View)jif).refreshTree(id1, id2);
				}
			}
			else if(update.equals("STATE"))
			{
				if(jif instanceof Detail_Dossier_View)
				{
					((Detail_Dossier_View)jif).updateState();
				}
			}
			else if(update.equals("REFERENCE"))
			{
				if(jif instanceof Add_Commande_View)
				{
					((Add_Commande_View)jif).updateReference();
				}
			}
			else if(update.equals("PAYMENT_MODE"))
			{
				if(jif instanceof Add_Commande_View)
				{
					((Add_Commande_View)jif).updatePaymentMode();
				}
				else if(jif instanceof Add_Facture_View)
				{
					((Add_Facture_View)jif).updatePaymentMode();
				}
			}
			else if(update.equals("DEFINITION"))
			{
				if(jif instanceof Add_Facture_View)
				{
					((Add_Facture_View)jif).updateFactureCategory();
				}
			}
			else if(update.equals("COMMANDE_REFERENCE"))
			{
				if(jif instanceof Add_Commande_View)
				{
					((Add_Commande_View)jif).updateCommandeReference();
				}
			}
			else if(update.equals("CLIENT"))
			{
				if(jif instanceof Add_Commande_View)
				{
					((Add_Commande_View)jif).updateClient();
				}
				else if(jif instanceof Add_Facture_View)
				{
					((Add_Facture_View)jif).updateClient();
				}
			}
		}
	}
	
	/*
	 * Permet d'ajouter une vue à l'application

	 * Pour ADD_COMMANDE_VIEW : Site_Without_Commande || Dossier+ConstructionSite || Commande
	 * Pour ADD_FACTURE_VIEW : Commande || Facture
	 * Pour ADD_PAYMENT_VIEW : Facture || Payment
	 * Pour DETAIL_VIEW : Dossier+ConstructionSite
	 * Pour SEARCH_XXX_VIEW : null
	 * @param type : Voir les indices STATIC de la Class Controller
	 * @param obj... : Object 1, 2, 3
	 */
	public boolean openView(int type, Object... obj)
	{
		if(type == ADD_COMMANDE_VIEW)
		{
			if(getView().verifyInternalFrame(Add_Commande_View.class))
    		{
				if(obj[0] instanceof Site_Without_Commande)
				{
					getView().addView(new Add_Commande_Controller(this, (Site_Without_Commande) obj[0]).getView());
					return true;
				}
				else if(obj[0] instanceof Dossier && obj[1] instanceof Construction_Site)
				{
					getView().addView(new Add_Commande_Controller(this, (Dossier) obj[0], (Construction_Site) obj[1]).getView());
					return true;
				}
				else if(obj[0] instanceof Commande)
				{
					getView().addView(new Add_Commande_Controller(this, (Commande) obj[0]).getView());
					return true;
				}
    		}
    		else
    		{
    			SOptionPane.showErrorMessage("Ouverture de la vue Commande", "Un formulaire de Commande est déjà ouvert.");
    		}
		}
		else if(type == COMMANDE_VIEW)
		{
			if(getView().verifyInternalFrame(Commande_View.class))
    		{
				if(obj[0] instanceof Commande)
				{
					getView().addView(new Commande_Controller(this, (Commande) obj[0]).getView());
					return true;
				}
    		}
    		else
    		{
    			SOptionPane.showErrorMessage("Ouverture de la vue Commande", "Une vue Commande est déjà ouverte.");
    		}
		}
		else if(type == HISTORY_VIEW)
		{
			if(obj[0] instanceof Dossier && obj[1] instanceof Construction_Site)
			{
				new History_Controller(this, (Dossier) obj[0], (Construction_Site) obj[1]);
				return true;
			}
		}
		else if(type == ADD_FACTURE_VIEW)
		{
			if(getView().verifyInternalFrame(Add_Facture_View.class))
    		{
				if(obj[0] instanceof Commande)
				{
					getView().addView(new Add_Facture_Controller(this, (Commande) obj[0]).getView());
					return true;
				}
				else if(obj[0] instanceof Facture)
				{
					getView().addView(new Add_Facture_Controller(this, (Facture) obj[0]).getView());
					return true;
				}
    		}
    		else
    		{
    			SOptionPane.showErrorMessage("Ouverture de la vue Facture", "Un formulaire de Facture est déjà ouvert.");
    		}
		}
		else if(type == FACTURE_VIEW)
		{
			if(getView().verifyInternalFrame(Facture_View.class))
    		{
				if(obj[0] instanceof Facture)
				{
					getView().addView(new Facture_Controller(this, (Facture) obj[0]).getView());
					return true;
				}
    		}
    		else
    		{
    			SOptionPane.showErrorMessage("Ouverture de la vue Facture", "Une vue Facture est déjà ouverte.");
    		}
		}
		else if(type == DETAIL_VIEW)
		{
			if(getView().verifyInternalFrame(Detail_Dossier_View.class))
    		{
				getView().addView(new Detail_Dossier_Controller(this, (Dossier) obj[0], (Construction_Site) obj[1], obj.length > 2 ? (DBModel) obj[2] : null).getView());
				return true;
    		}
    		else
    		{
    			SOptionPane.showErrorMessage("Ouverture de la vue Détail", "Une vue Détail est déjà ouverte.");
    		}
		}
		else if(type == ADD_PAYMENT_VIEW)
		{
			if(obj[0] instanceof Payment)
			{
				new Add_Payment_Controller(this, (Payment) obj[0]);
				return true;
			}
			else if(obj[0] instanceof Facture)
			{
				new Add_Payment_Controller(this, (Facture) obj[0]);
				return true;
			}
		}
		else if(type == ADD_RELANCE_VIEW)
		{
			if(obj[0] instanceof Relance)
			{
				new Add_Relance_Controller(this, (Relance) obj[0]);
				return true;
			}
			
		}
		else if(type == ADD_CLIENT)
		{
			if(getView().verifyInternalFrame(Add_Client_View.class))
    		{
				if(obj[0] == null)
					getView().addView(new Add_Client_Controller(this).getView());
				else
					getView().addView(new Add_Client_Controller(this, (org.sig.fw.db.corrida.model.Client) obj[0]).getView());
				return true;
    		}
    		else
    		{
    			SOptionPane.showErrorMessage("Ouverture de la vue Ajouter un Client", "Une vue Ajout Client est déjà ouverte.");
    		}	
		}
		else if(type == CLIENT)
		{
			if(getView().verifyInternalFrame(Client_View.class))
    		{
    			getView().addView(new Client_Controller(this, (org.sig.fw.db.corrida.model.Client) obj[0]).getView());
    			return true;
    		}
    		else
    		{
    			SOptionPane.showErrorMessage("Ouverture de la vue Client", "Une vue Client est déjà ouverte.");
    		}	
		}
		else if(type == SEARCH_COMMANDE_VIEW)
		{
			if(getView().verifyInternalFrame(Search_Commande_View.class))
    		{
    			getView().addView(new Search_Commande_Controller(this).getView());
    			return true;
    		}
    		else
    		{
    			SOptionPane.showErrorMessage("Ouverture de la vue Recherche Commande", "Une vue Recherche Commande est déjà ouverte.");
    		}	
		}
		else if(type == SEARCH_COMMANDE_SPECIFIC_VIEW)
		{
			if(getView().verifyInternalFrame(Search_Commande_Specifique_View.class))
    		{
    			getView().addView(new Search_Commande_Specifique_Controller(this).getView());
    			return true;
    		}
    		else
    		{
    			SOptionPane.showErrorMessage("Ouverture de la vue Recherche Commande Spécifique", "Une vue Recherche Commande Spécifique est déjà ouverte.");
    		}	
		}
		else if(type == SEARCH_FACTURE_VIEW)
		{
			if(getView().verifyInternalFrame(Search_Facture_View.class))
    		{
    			getView().addView(new Search_Facture_Controller(this).getView());
    			return true;
    		}
    		else
    		{
    			SOptionPane.showErrorMessage("Ouverture de la vue Recherche Facture", "Une vue Recherche Facture est déjà ouverte.");
    		}	
		}
		else if(type == SEARCH_PAYMENT_VIEW)
		{
			if(getView().verifyInternalFrame(Search_Payment_View.class))
    		{
    			getView().addView(new Search_Payment_Controller(this).getView());
    			return true;
    		}
    		else
    		{
    			SOptionPane.showErrorMessage("Ouverture de la vue Recherche Paiement", "Une vue Recherche Paiement est déjà ouverte.");
    		}	
		}
		else if(type == STATE_VIEW)
		{
			if(getView().verifyInternalFrame(State_View.class))
    		{
    			getView().addView(new State_Controller(this).getView());
    			return true;
    		}
    		else
    		{
    			SOptionPane.showErrorMessage("Ouverture de la vue Définition des états", "Une vue Définition des états est déjà ouverte.");
    		}
		}
		else if(type == REFERENCE_VIEW)
		{
			if(getView().verifyInternalFrame(Reference_View.class))
    		{
    			getView().addView(new Reference_Controller(this).getView());
    			return true;
    		}
    		else
    		{
    			SOptionPane.showErrorMessage("Ouverture de la vue Définition des références", "Une vue Définition des références est déjà ouverte.");
    		}
		}
		else if(type == COMMANDE_REFERENCE_VIEW)
		{
			if(getView().verifyInternalFrame(Commande_Reference_View.class))
    		{
    			getView().addView(new Commande_Reference_Controller(this).getView());
    			return true;
    		}
    		else
    		{
    			SOptionPane.showErrorMessage("Ouverture de la vue Définition des références de commande", "Une vue Définition des références de commande est déjà ouverte.");
    		}
		}
		else if(type == DEFINITION_VIEW)
		{
			if(getView().verifyInternalFrame(Definition_View.class))
    		{
    			getView().addView(new Definition_Controller(this, (String) obj[0], (String) obj[1]).getView());
    			return true;
    		}
    		else
    		{
    			SOptionPane.showErrorMessage("Ouverture de la vue Définition", "Une vue Définition est déjà ouverte.");
    		}
		}
		else if(type == PAYMENT_MODE_VIEW)
		{
			if(getView().verifyInternalFrame(Payment_Mode_View.class))
    		{
    			getView().addView(new Payment_Mode_Controller(this).getView());
    			return true;
    		}
    		else
    		{
    			SOptionPane.showErrorMessage("Ouverture de la vue Définition des Mode de Paiement", "Une vue Définition des Mode de Paiement est déjà ouverte.");
    		}
		}
		else if(type == SEARCH_CLIENT_VIEW)
		{
			if(getView().verifyInternalFrame(Search_Client_View.class))
    		{
    			getView().addView(new Search_Client_Controller(this).getView());
    			return true;
    		}
    		else
    		{
    			SOptionPane.showErrorMessage("Ouverture de la vue Recherche Client", "Une vue Recherche Client est déjà ouverte.");
    		}
		}
		else if(type == EXPORT_FACTURE_MONTH)
		{
			if(getView().verifyInternalFrame(Export_Month_Facture_View.class))
    		{
    			getView().addView(new Export_Month_Facture_Controller(this).getView());
    			return true;
    		}
    		else
    		{
    			SOptionPane.showErrorMessage("Ouverture de la vue Export des Factures par mois", "Une vue Export des Factures par mois est déjà ouverte.");
    		}
		} else if(type == SEARCH_FACTURE_SPECIFIC_VIEW) {
			if(getView().verifyInternalFrame(Search_Facture_Specifique_View.class)) {
    			getView().addView(new Search_Facture_Specifique_Controller(this).getView());
    			return true;
    		} else {
    			SOptionPane.showErrorMessage("Ouverture de la vue Recherche Facture Spécifique", "Une vue Recherche Facture Spécifique est déjà ouverte.");
    		}	
		} else if(type == EXPORT_FACTURES) {
			if(getView().verifyInternalFrame(Export_Factures_Controller.class)) {
    			getView().addView(new Export_Factures_Controller(this).getView());
    			return true;
    		} else {
    			SOptionPane.showErrorMessage("Ouverture de la vue Export des Factures", "Une vue Export des Factures est déjà ouverte.");
    		}
		} else if(type == NOTES_VIEW) {
    		getView().addView(new Notes_Controller(this, (Dossier) obj[0]).getView());
		} else if(type == EXPORT_NOTES) {
    		getView().addView(new Export_Notes_Controller(this).getView());
		}
		return false;
	}

	public boolean openViewRelance(int type, Object... obj)
	{
		if(obj[0] instanceof Relance)
		{
			new Add_Relance_Controller(this, (Relance) obj[0]);
			return true;
		} else {
			return false;
		}
	}

	@Override
	public void fireCrash(int arg0) {
		new LogView(arg0).setVisible(true);
		if(arg0 == Definition.LEVEL_FATAL) {
			System.exit(-1);
		}
	}

}
