package agenda;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.ArrayList;

public class Database {
  private Connection connect = null;
  private PreparedStatement preparedStatement = null;
  
  //database values
  private String host = "jdbc:mysql://jancokock.me:3306/festivalagenda";
  private String user = "projectgroepa6";
  private String pass = "tzHnYRbpt7r3z4jt";
  
  //get agenda values
  private Agenda agenda = new Agenda();
  private ArrayList<Act> acts = (ArrayList<Act>) agenda.getActs();
  

  public void writeDatabase() throws Exception {
    try {
      Class.forName("com.mysql.jdbc.Driver");
      connect = DriverManager.getConnection(host,user,pass);

      //first check if agenda already exists in DB, otherwise delete everything
       
	//insert stage names
      for(Act act : acts){
    	  String name = act.getName();
    	  String genre = act.getGenre();
    	  String beginTime = act.getActTime().getBeginTimeString();
    	  String endTime = act.getActTime().getEndTimeString();
    	  int popularity = act.getPopularity();
    	    
    	  System.out.println(name);
    	  System.out.println(genre);
    	  System.out.println(beginTime);
    	  System.out.println(endTime);
    	  System.out.println(popularity);
    	  //insert data into database
    	  preparedStatement = connect.prepareStatement("insert into  festivalagenda.Act (agenda_view_id, begintijd, eindtijd, naam, genre, populariteit) values (0, ?, ?, ?, ?, ?)");
    	  preparedStatement.setString(1, beginTime);
    	  preparedStatement.setString(2, endTime);
          preparedStatement.setString(3, name);
          preparedStatement.setString(4, genre);
          preparedStatement.setInt(5, popularity);
          preparedStatement.executeUpdate();
      }
            
    } catch (Exception e) {
      throw e;
    } finally {
    	if (preparedStatement != null) {
            preparedStatement.close();
          }

          if (connect != null) {
            connect.close();
          }
    }

  }
} 