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

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

/**
 * Class GetSensorStatuses
 * Return sensor status for a given flight sensor ID
 */
class GetSensorStatuses extends APIPage
{
   public function __construct()
   {
      // we want db connection
      parent::__construct(true);
   }

   public function render($regex_matches)
   {

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

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

/**
 * Class DeleteSensorStatus
 * Delete specified sensor status
 */
class DeleteSensorStatus extends APIPage
{
   public function __construct()
   {
      // we want db connection
      parent::__construct(true);
   }

   public function render($regex_matches)
   {

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

      // Call db to delete row
      $database_delete_result = $this->db->delete_sensor_status($sensor_status_id);
      if($database_delete_result) {
         // Success
         $this->output['success'] = true;
      } else {
         // Database Error
         $this->error('Failed to insert into the database.');
      }
   }
}

/**
 * Class UpdateSensorStatus
 * Update sensor status
 */
class UpdateSensorStatus extends APIPage
{
   public function __construct()
   {
      // we want db connection
      parent::__construct(true);
   }

   public function render($regex_matches)
   {

      $result = true;

      $json_data = json_decode(file_get_contents("php://input"));

      foreach($json_data as $status_record) {
         //$parameter_array = array($status_record['id'], $status_record['username'], $status_record['datetime'], $status_record['notes'], $status_record['progress']);
         $parameter_array = array($status_record->id, $status_record->username, $status_record->datetime, $status_record->notes, $status_record->progress);

         $db_result = $this->db->update_sensor_status($parameter_array);
         if($db_result) {
            $result = $result & true;
         } else {
            $result = false;
         }
      }

      if($result) {
         // Success
         $this->output['success'] = true;
      } else {
         // Database Error
         $this->error('Failed to update statuses.');
      }
   }
}

/**
 * Class InsertSensorStatus
 * Insert sensor status
 */
class InsertSensorStatus extends APIPage
{
   public function __construct()
   {
      // we want db connection
      parent::__construct(true);
   }

   public function render($regex_matches)
   {

      // Get flight sensor id
      $flight_sensor_id = $regex_matches[1];

      parse_str(file_get_contents("php://input"), $post_vars);

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


         $parameter_array = array($flight_sensor_id, $post_vars['status_progress'], $_SESSION['user'], $post_vars['blocked_reason']);
         // Send to database
         $database_insert_result = $this->db->insert_sensor_status($parameter_array);
         if($database_insert_result) {
            // Success
            $this->output['success'] = true;
         } else {
            // Database Error
            $this->error('Failed to insert into the database.');
         }
      }
   }
}

