<?php
/**
 * Cart controlls the actions for the shop
 */
class CartController{
    private $model;

    function __construct(){
        $this->model = new CartModel();
    }

    /**
     * get count of items in the cart
     * @return json number of items in the cart
     */
    public function getCartCount() {
        echo json_encode($this->model->getCartCount(), JSON_NUMERIC_CHECK);
    }

    /**
     * get items in cart
     * @return json with array list of items in the cart
     */
    public function getCartItems() {
        echo json_encode($this->model->getCartItems(), JSON_NUMERIC_CHECK);
    }

    /**
     * update the quantity in the cart for an item
     * @return json update message
     */
    public function updateQuantity() {
        $idPackage = isset($_REQUEST['idPackage']) ? $_REQUEST['idPackage'] : 0;
        $idPrice = isset($_REQUEST['idPrice']) ? $_REQUEST['idPrice'] : 0;
        $idCustomerInstance = isset($_REQUEST['idCustomerInstance']) ? $_REQUEST['idCustomerInstance'] : 0;
        $quantity = isset($_REQUEST['quantity']) ? $_REQUEST['quantity'] : 1;
        echo json_encode($this->model->updateQuantity($idPackage, $idCustomerInstance, $idPrice, $quantity));
    }

    /**
     * remove items from carts
     * @return json update message
     */
    public function removeFromCart(){
        $idCart = isset($_REQUEST['idCart']) ? $_REQUEST['idCart'] : 0;
        echo json_encode($this->model->removeFromCart($idCart));
    }

    /**
     * upload a questionaire form the cart page
     * @return json upload message
     */
    public function uploadOrderDocument(){
        $file = isset($_FILES['file']) ? $_FILES['file'] : [];
        $idDocumentType = isset($_REQUEST['idDocumentType']) ? $_REQUEST['idDocumentType'] : 0;
        $idPackage = isset($_REQUEST['idPackage']) ? $_REQUEST['idPackage'] : 0;
        echo json_encode($this->model->uploadOrderDocument($file, $idDocumentType, $idPackage));
    }

    /**
     * get document sfor packages in cart
     * @return json Array of documents
     */
    public function getCartDocuments(){
        $packages = isset($_REQUEST['packages']) ? $_REQUEST['packages'] : [];
        echo json_encode($this->model->getCartDocuments($packages), JSON_NUMERIC_CHECK);
    }

    public function setBidForCart(){
        $idBid = isset($_REQUEST['idBid']) ? $_REQUEST['idBid'] : 0;
        $idCart = isset($_REQUEST['idCart']) ? $_REQUEST['idCart'] : 0;

        echo json_encode($this->model->setBidForCart($idBid, $idCart), JSON_NUMERIC_CHECK);
    }

    /**
    * get details of the customer logged in
    * @return json array with neccessary details
    */
   public function getCustomerDetails() {
       echo json_encode($this->model->getCustomerDetails(), JSON_NUMERIC_CHECK);
   }

   /**
   * get all the countries
   * @return json array with all countries
   */
  public function getCountries() {
      echo json_encode($this->model->getCountries());
  }

  /**
   * save order delivery and billing addresses
   * @return json Array messages
   */
  public function saveOrderDetails(){
      $orderDetails = isset($_REQUEST['orderDetails']) ? $_REQUEST['orderDetails'] : [];
      $cartItems = isset($_REQUEST['cartItems']) ? $_REQUEST['cartItems'] : [];
      echo json_encode($this->model->saveOrderDetails($orderDetails, $cartItems), JSON_NUMERIC_CHECK);
  }

  /**
   * get document sfor packages in cart
   * @return json Array of documents
   */
  public function placeOrder(){
      $cartItems = isset($_REQUEST['cartItems']) ? $_REQUEST['cartItems'] : [];
      $orderDetails = isset($_REQUEST['orderDetails']) ? $_REQUEST['orderDetails'] : [];
      echo json_encode($this->model->placeOrder($cartItems, $orderDetails), JSON_NUMERIC_CHECK);
  }

}
