<?php

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

namespace Apps\Calendar;

use Core\Core\Core;
use Core\Core\Request;
use Core\Core\Response;
use Apps\Base\Base;
use Apps\Calendar\Module\Admin\AdminController;
use Apps\Calendar\Module\Member\MemberController;
use Apps\Calendar\Module\Front\FrontController;
use Apps\Calendar\Helper\EventHelper;
use Apps\Calendar\Entity\CalendarEvent;

class Calendar extends Base {

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

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

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

        return $this->Route;
    }

    /*     * *
     * Execute action after install
     */

    function PostInstall() {
        \Apps\EeApp\Helper\AppHelper::UpdateAppParams($this->Core,
                "Calendar",
                $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, "Calendar", "Calendar");
    }

    /*     * *
     * Index page
     */

    function Index() {
        $frontController = new FrontController($this->Core);
        return $frontController->Index();
    }

    /*     * *
     * Detail of a event
     */

    function Event($eventCode) {
        if($eventCode == ""){
            $frontController = new FrontController($this->Core);
            return $frontController->Index();
        } else {

            $frontController = new FrontController($this->Core);
            return $frontController->Event($eventCode);
        }
    }

    /*     * *
     * Sauvegare l'event
     */

    function SaveEvent() {
        $adminController = new AdminController($this->Core);
        $result = $adminController->SaveEvent(Request::GetPosts());

        if ($result === true) {
            return Response::Success(["eventGrid" => $adminController->GetTabEvent()]);
        } else {
            return Response::Error(["error" => $result]);
        }
    }

    /*     * *
     * Delete a event
     */

    function DeleteEvent() {
        $adminController = new AdminController($this->Core);
        $result = $adminController->DeleteEvent(Request::GetPost("EventId"));

        if ($result === true) {
            return Response::Success(["eventGrid" => $adminController->GetTabEvent()]);
        } else {
            return Response::Error(["error" => $result]);
        }
    }

    /*     * *
     * Add user to e event
     */

    function AddUserEvent() {
        $result = EventHelper::AddEventUser($this->Core, Request::GetPosts());

        if ($result === true) {
            return Response::Success([]);
        } else {
            return Response::Error(["error" => $result]);
        }
    }

    /*     * *
     * 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\Calendar\Widget\AdminDashBoard\AdminDashBoard($this->Core);
                break;
            case "LastEvent" :
                $widget = new \Apps\Calendar\Widget\LastEvent\LastEvent($this->Core);
                break;
        }

        return $widget->Render();
    }

    /*     * *
     * Get Forum Addable widget
     */

    public function GetListWidget() {
        return array(array("Name" => "LastEvent",
                "Description" => $this->Core->GetCode("Calendar.DescriptionLastEventWidget")),
        );
    }

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

    /***
     * Get the lase Event
     */
    public function GetEvents() {
        return Response::Success(EventHelper::GetEvents($this->Core));
    }
    
    /***
     * Get the Event
     */
    public function GetEvent() {
        return Response::Success(EventHelper::GetEvent($this->Core, Request::GetPost("eventId")));
    }
    
    /***
     * Save a reservation Mobile
     */
    public function SaveReservation($params, $userId){
        
        $result = EventHelper::AddEventUser($this->Core, Request::GetPosts(), $userId);

        if ($result === true) {
            return Response::Success([]);
        } else {
            return Response::Error(["error" => $result]);
        }
    }
    
    /****
     * Obtient les évenements des utilisateurs
     */
    public function GetUserEvents($params, $userId){
       $userEvent = new CalendarEvent($this->Core);
       $userEvent->join("CalendarEventUser ", "CalendarEventUser", "LEFT" ,   "CalendarEventUser.EventId = CalendarEvent.Id");
       $events = $userEvent->Find("CalendarEventUser.UserId=" .$userId);
       
       return Response::Success(["events" => $userEvent->ToAllArray($events)]);
    }
    
}
