<?php

/*
 * PuzzleApp
 * Webemyos
 * Jérôme Oliva
 * GNU Licence
 */

namespace Apps\Booking;

use Core\Core\Core;
use Apps\Base\Base;
use Core\Core\Request;
use Core\Core\Response;
use Apps\Booking\Module\Front\FrontController;
use Apps\Booking\Module\Admin\AdminController;
use Apps\Booking\Helper\ReservationHelper;

class Booking extends Base {

    /**
     * Author and version
     * */
    public $Author = 'Webemyos';
    public $Version = '1.0.0';

    /**
     * Constructeur
     * */
    function __construct() {
        $this->Core = Core::getInstance();
        parent::__construct($this->Core, "Booking");
    }

    /**
     * Set public route
     */
    function GetRoute($routes = "") {
        $this->Route->SetPublic(array());

        return $this->Route;
    }

    /*     * *
     * Execute action after install
     */

    function PostInstall() {
        \Apps\EeApp\Helper\AppHelper::UpdateAppParams($this->Core,
                "Booking",
                $this->Version,
                "", // Add Description of the app
                1, // Set 1 if the App have a AdminWidgetDashBoard
                1  // Set 1 if the App have a Member Module
        );
    }

    /**
     * Start Admin
     */
    function Run() {
        echo parent::RunApp($this->Core, "Booking", "Booking");
    }

    /***
     * INdex front page
     */
    function Index(){
      $frontController = new FrontController($this->Core);
      return $frontController->Index();
    }

     /**
     * Sauvegarde la caractéristique
     */
    function SaveCaracteristique() {

      $adminController = new AdminController($this->Core);

      if ($adminController->SaveCaracteristique($this->Core, Request::GetPosts())) {
          $adminController = new AdminController($this->Core);
          return Response::Success($adminController->GetTabCaracteristique());
      } else {
          return Response::Error();
      }
    }

    /***
     * Sauvegare des horaires d'ouverture
     */
    function SaveOpeningTime(){
      $adminController = new AdminController($this->Core);

      if ($adminController->SaveOpeningTime($this->Core, Request::GetPosts())) {
          return Response::Success([]);
      } else {
          return Response::Error();
      }
    }

    /*     * *
     * Run member Controller
     * 
     * /
      /*function RunMember(){
      $memberController = new MemberController($this->Core);
      return $memberController->Index();
      } */

    /**
     * Get The siteMap 
     */
    /* public function GetSiteMap(){
      return SitemapHelper::GetSiteMap($this->Core);
      } */

    /*     * *
     * Get the widget
     */
     public function GetWidget($type = "", $params = "") {

      switch ($type) {
      case "AdminDashboard" :
        $widget = new \Apps\Booking\Widget\AdminDashBoard\AdminDashBoard($this->Core);
      break;
      case "BookingReservation" :
        $widget = new \Apps\Booking\Widget\BookingReservationForm\BookingReservationForm($this->Core);
        break;
      }

      return $widget->Render($params);
      } 

      /***
       * Save a reservation
       */
      public function SaveReservation(){
          $reservation = ReservationHelper::SaveReservation($this->Core, Request::GetPosts());

          if($reservation === true){
            return Response::Success(array());  
          } else {
            return Response::Error(array("Message" => $reservation));
          }
      }

    /*     * *
     * Get Forum Addable widget
     */
     public function GetListWidget(){
        return array(array("Name" => "BookingReservation",
        "Description" => $this->Core->GetCode("Booking.BookingReservationWidget")),
        );
      } 

      /***
       * Get List Email of Base App 
       */
      public function GetListEmail(){
        return array("addReservation");
    }
}
