package uy.edu.udelar.forrest.forrestloganalyser.model;
import java.io.File;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Iterator;
import java.util.List;
import java.util.Observable;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

import uy.edu.udelar.forrest.forrestloganalyser.exceptions.DataBaseConnectionException;
import uy.edu.udelar.forrest.forrestloganalyser.exceptions.MainModelCreateContextException;

/**
 *
 * @author pmarrero
 */
public class MainModel extends Observable {

    private static MyLogger myLogger;
    
    private static DataBase dataBase;
    
    private static String fileConfig;

    private static int idPartido;
    
    private static String locatario;
    
    private static String visitante;
        
    private static int cantFinPartido;
    
	public static int getCantFinPartido() {
		return cantFinPartido;
	}

	public static void setCantFinPartido(int cantFinPartido) {
		MainModel.cantFinPartido = cantFinPartido;
	}

	/** Creates a new instance of MainModel */
    public MainModel() {
    }
    
    public static DataBase getDataBase(){
        return dataBase;
    }
    
    public static MyLogger getLogger(){
        return myLogger;
    }

    public static String getLocatario() {
		return locatario;
	}

	public static void setLocatario(String locatario) {
		MainModel.locatario = locatario;
	}

	public static String getVisitante() {
		return visitante;
	}

	public static void setVisitante(String visitante) {
		MainModel.visitante = visitante;
	}

	public static int getIdPartido() {
		return idPartido;
	}

	public static void setIdPartido() {
        Statement stmt;
        ResultSet rs;
        Connection connection = MainModel.getDataBase().getConnection();
        try{
            stmt = connection.createStatement();
            rs = stmt.executeQuery("select max(id_partido) from partidos");
        	if (rs.next()){
        		idPartido = rs.getInt(1) + 1;
        	}else{
        		idPartido = 1;
        	}
        }catch (Exception e){
        	idPartido = 1;
        }
	}

    public static void createInitialContext(String configFile) throws DataBaseConnectionException, MainModelCreateContextException {
        fileConfig=configFile;
        
        SAXBuilder builder = new SAXBuilder();
        try {
            Document doc = builder.build(new File(fileConfig));
            Element rootElement = doc.getRootElement().getChild("mainModel");
            List childElemts = rootElement.getChildren();
            Iterator childElementsIterator = childElemts.iterator();
            while (childElementsIterator.hasNext()){
                Element childElement = (Element) childElementsIterator.next();
                if (childElement.getName()=="archLogger"){
                    myLogger = new MyLogger();
                    myLogger.setLogConfigFileName(System.getProperty("user.dir")+childElement.getText());
                    myLogger.openLogger();
                }else if (childElement.getName()=="dataBase"){
                    String url=childElement.getChild("url").getText();
                    String driver=childElement.getChild("driver").getText();
                    String user=childElement.getChild("user").getText();
                    String password=childElement.getChild("password").getText();
                    dataBase=new DataBase(url, driver,user,password);
                }
            }
        } catch (DataBaseConnectionException e) {
            throw e;
        } catch (Exception e) {
            throw new MainModelCreateContextException("MainModel::createInitialContext - Error al parsear el XML: "+ e.getMessage());
        }
    }
}
