<?php
/**
 * User: tec
 * Date: 05/09/14
 * Time: 10:53
 */

require_once('../classes/api.php');

/**
 * Class GetProjectInfo
 * Get project info
 */
class GetProjectInfo extends APIPage
{
   public function __construct()
   {
      // we want db connection
      parent::__construct(true);
   }

   public function render($regex_matches)
   {

      // Get flight id
      $flight_id = $regex_matches[1];

      // Call db to get rows
      $this->output = $this->db->getProject($flight_id);
   }
}

/**
 * Class UpdateProject
 * Update project info
 */
class UpdateProject extends APIPage
{
   public function __construct()
   {
      // we want db connection
      parent::__construct(true);
   }

   public function render($regex_matches)
   {

      // Get flight id
      $project_code = $regex_matches[1];

      // Gets "post" variables from data that is in the format of a query string
      parse_str(file_get_contents("php://input"), $post_vars);
      $needed_keys = array('project_summary', 'project_objective', 'project_eufarcode');


      if($this->has_keys($post_vars, $needed_keys) == false) {
         // Error, Not enough keys
         $this->error('Incorrect number of parameters sent to the api.');
      } else {
         // Validation success

         $parameter_array = array($project_code, $post_vars['project_summary'], $post_vars['project_objective'], $post_vars['project_eufarcode']);

         // Send to database
         $database_update_result = $this->db->update_project($parameter_array);
         if($database_update_result) {
            // Success
            $this->output['success'] = true;
         } else {
            // Database Error
            $this->error('Failed to update the database.');
         }
      }
   }
}

/**
 * Class DeleteProject
 * Delete project
 */
class DeleteProject extends APIPage
{
   public function __construct()
   {
      // we want db connection
      parent::__construct(true);
   }

   public function render($regex_matches)
   {

      // Get flight id
      $flight_id    = $regex_matches[1];
      $project_code = $regex_matches[2];

      $database_delete_result = $this->db->delete_project($flight_id, $project_code);
      if($database_delete_result) {
         // Success
         $this->output['success'] = true;
      } else {
         // Database Error
         $this->error('Failed to update the database.');
      }
   }
}

/**
 * Class InsertProject
 * Add new project to database
 */
class InsertProject extends APIPage
{
   public function __construct()
   {
      // we want db connection
      parent::__construct(true);
   }

   public function render($regex_matches)
   {
      // Gets "post" variables from data that is in the format of a query string
      $javascript_array = json_decode(file_get_contents("php://input"), true);

      if($this->has_keys($javascript_array, array('existing_project', 'existing_pi'))) {

         $use_existing_project = $javascript_array['existing_project'];
         $use_existing_pi      = $javascript_array['existing_pi'];

         $pi_id = null;

         if($use_existing_pi) {
            $pi_id = $javascript_array['pi_id'];
         } else {
            $pi_name    = $javascript_array['pi_name'];
            $pi_email   = $javascript_array['pi_email'];
            $pi_address = $javascript_array['pi_address'];

            $possible_id = $this->db->insert_pi($pi_name, $pi_email, $pi_address);
            if($possible_id > 0) {
               $pi_id = $possible_id;
            } else {
               $this->error('Unable to add PI to database.');
            }
         }

         if($pi_id != null) {
            if($use_existing_project) {
               $this->error('NOT IMPLEMENTED YET'); // TODO IMPLEMENT THIS
            } else {
               $project_code        = $javascript_array['project_code'];
               $project_eufar_code  = $javascript_array['project_eufar_code'];
               $project_summary     = $javascript_array['project_summary'];
               $project_description = $javascript_array['project_description'];
               $project_arguments   = array($project_code, $pi_id, $project_summary, $project_eufar_code, $project_description);

               if($this->db->insert_project($project_arguments)) {
                  $this->output['success'] = true;
               } else {
                  // Database Error
                  $this->error('Failed to insert project into the database.');
               }
            }
         } else {
            $this->error('Unable to add PI to database.');
         }
      } else {
         $this->error('Incorrect number of parameters sent to the api.');
      }
   }
}
