<?php

/**
 * Class GetFlightSensorInfo
 * Get flight sensor info
 */
class GetFlightSensorInfo 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];

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

/**
 * Class InsertFlightSensor
 * Insert new flight sensor
 */
class InsertFlightSensor 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];

      // Gets "post" variables from data that is in the format of a query string, could use the _POST variable for this
      // but then it wouldnt work for PUT requests so might as well be consistent across requests
      parse_str(file_get_contents("php://input"), $post_vars);

      if($this->has_keys($post_vars, array('sensor_name', 'priority', 'lines')) == false) {
         // Error, Not enough keys
         $this->error('Incorrect number of parameters sent to the api.');
      } else {
         // Validation success
         $database_insert_success = $this->db->insert_flight_sensor($flight_id, $post_vars['sensor_name'], $post_vars['priority'], $post_vars['lines'], 'ARSF');
         if($database_insert_success) {
            // Success
            $this->output['success'] = true;
         } else {
            // Database Error
            $this->error('Failed to insert into the database.');
         }
      }
   }
}

/**
 * Class DeleteFlightSensor
 * Delete flight sensor
 */
class DeleteFlightSensor 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];

      $database_delete_success = $this->db->delete_flight_sensor($flight_sensor_id);
      if($database_delete_success) {
         // Success
         $this->output['success'] = true;
      } else {
         // Database Error
         $this->error('Failed to delete from the database.');
      }
   }
}

/**
 * Class UpdateFlightSensor
 * Update flight sensor information
 */
class UpdateFlightSensor 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('sensor_priority', 'sensor_priority_offset', 'sensor_flightlines', 'sensor_del_check_time')) == false) {
         // Error, Not enough keys
         $this->error('Incorrect number of parameters sent to the api.');
      } else {
         // Validation success
         $parameter_array = array($flight_sensor_id, $post_vars['sensor_priority'], $post_vars['sensor_priority_offset'], $post_vars['sensor_flightlines'], $post_vars['sensor_del_check_time']);

         $database_update_success = $this->db->update_flight_sensor($parameter_array);
         if($database_update_success) {
            // Success
            $this->output['success'] = true;
         } else {
            // Database Error
            $this->error('Failed to insert into the database.');
         }
      }
   }
}

/**
 * Class GetProgressFlightSensorInfo
 * Get progress details for flight sensor
 */
class GetProgressFlightSensorInfo 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];

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