<?php namespace Blackpulp\MinistryPlatform\CoreTool;

use Blackpulp\MinistryPlatform\MinistryPlatformException;
use Blackpulp\MinistryPlatform\MinistryPlatform;
use Blackpulp\MinistryPlatform\User;

/**
 * This class handles the base setup for setting up a Core Tool.
 */
class Base {
  
  /**
   * Church's domain GUID
   * 
   * @var string
   */
  protected $domain;
  
  /**
   * Current user's User_GUID
   * 
   * @var string
   */
  protected $user_guid;

  /**
   * User object for the current user.
   * 
   * @var Blackpulp\MinistryPlatform\User
   */
  protected $user;

  /**
   * ID of the current page.
   * 
   * @var integer
   */
  protected $page_id;

  /**
   * Record ID.
   * 
   * If the tool is launched from an open record, this will be that record's ID. 
   * If launched from the grid, this value will always be -1.
   * 
   * @var integer
   */
  protected $record_id;

  /**
   * Brief description of the current record.
   * 
   * @var string
   */
  protected $record_description;

  /**
   * ID of the active selection.
   * 
   * @var int
   */
  protected $selection_id; // integer

  /**
   * Number of records present in the active selection.
   * 
   * @var int
   */
  protected $selection_count;

  /**
   * The current page of the grid view.
   *
   * This may be deprecated since MP 2.0 uses lazy loading and 
   * doesn't offer a paginated grid view.
   * 
   * @var int
   */
  protected $pagination;

  /**
   * Stores the sort order, if any
   * @var string
   */
  protected $sort_order;

  /**
   * Stores the grid's query string, if any
   * 
   * @var string
   */
  protected $query_string;

  /**
   * Store's the active view_id, if any
   * 
   * @var int
   */
  protected $view_id;

  /**
   * Array of the user's current selection
   * @var array
   */
  protected $selection = [];
  
  /**
  * Create the base coreTool object
  * 
  * Set all query params as properties, create an MP instance so hitting the MP API is quicker to do
  */
  
  public function __construct(MinistryPlatform $mp)
  {

    $this->domain = $_GET['dg'];
    $this->user_guid = $_GET['ug'];
    $this->page_id = $_GET['pageID'];
    $this->record_id = $_GET['recordID'];
    $this->record_description = $_GET['recordDescription'];
    $this->selection_id = $_GET['s'];
    $this->selection_count = $_GET['sc'];
    $this->pagination = $_GET['p'];
    $this->sort_order = !empty($_GET['o']) ? $_GET['o'] : "";
    $this->query_string = !empty($_GET['q']) ? $_GET['q'] : "";
    $this->view_id = !empty($_GET['v']) ? $_GET['v'] : 0;
    
    $this->mp = $mp;
    $this->user = $this->mp->authenticateGuid($this->user_guid);

    $this->selection = $this->setSelection();
  }


  /**
   * Gets the Array of the user's current selection.
   *
   * @return array
   */
  public function getSelection()
  {
      
    return $this->selection;

  }
  
  /**
   * Retrieves the current selection - if one exists
   */
  
  public function setSelection() {

    if(is_numeric($this->selection_count) && $this->selection_count > 0)
    {
      
      $sp = "api_Common_GetSelection";
      $params = [
        "UserID" => $this->user->getId()
        ,"PageID" => $this->page_id
        ,"SelectionID" => $this->selection_id
      ];

      $this->selection = $this->mp->storedProcedure($sp, $params);

    }

    return $this;
  }

  /**
   * Gets the User object for the current user.
   *
   * @return Blackpulp\MinistryPlatform\User
   */
  public function getUser()
  {
    return $this->user;
  }

  /**
   * Sets the User object for the current user.
   *
   * @param Blackpulp\MinistryPlatform\User $user the user
   *
   * @return self
   */
  public function setUser(Blackpulp\MinistryPlatform\User $user)
  {
    $this->user = $user;

    return $this;
  }

  /**
   * Gets the ID of the current page.
   *
   * @return integer
   */
  public function getPageId()
  {
    return $this->page_id;
  }

  /**
   * Gets the Record ID
   * If the tool is launched from an open record, this will be that record's ID
   * If launched from the grid, this value will always be -1.
   *
   * @return integer
   */
  public function getRecordId()
  {
    return $this->record_id;
  }

  /**
   * Gets the Brief description of the current record.
   *
   * @return string
   */
  public function getRecordDescription()
  {
    return $this->record_description;
  }

  /**
   * Gets the ID of the active selection.
   *
   * @return int
   */
  public function getSelectionId()
  {
    return $this->selection_id;
  }

  /**
   * Gets the Number of records present in the active selection.
   *
   * @return int
   */
  public function getSelectionCount()
  {
    return $this->selection_count;
  }

  /**
   * Gets the The current page of the grid view
   * This may be deprecated since MP 2.0 uses lazy loading and
   * doesn't offer a paginated grid view.
   *
   * @return int
   */
  public function getPagination()
  {
    return $this->pagination;
  }

  /**
   * Gets the Stores the sort order, if any.
   *
   * @return string
   */
  public function getSortOrder()
  {
    return $this->sort_order;
  }

  /**
   * Gets the Stores the grid's query string, if any.
   *
   * @return string
   */
  public function getQueryString()
  {
    return $this->query_string;
  }

  /**
   * Gets the Store's the active view_id, if any.
   *
   * @return int
   */
  public function getViewId()
  {
    return $this->view_id;
  }
}
