<?php

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

namespace Apps\Reward;

use Core\Core\Core;
use Core\Core\Request;
use Core\Core\Response;
use Apps\Base\Base;

use Apps\Reward\Module\Admin\AdminController;
use Apps\Reward\Module\Member\MemberController;


class Reward extends Base {

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

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

    /**
     * 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,
                "Reward",
                $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, "Reward", "Reward");
    }

    /***
     * Save a reward
     */
    function SaveReward(){

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

      if ($result === true) {
          return Response::Success($adminController->GetTabReward());
      } else {
          return Response::Error();
      }
    }

    /***
     * Save a gift
     */
    function SaveGift(){

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

      if ($result === true) {
          return Response::Success($adminController->GetTabGifts());
      } else {
          return Response::Error();
      }
    }
    
    /***
     * Valide a response User
     */
    function ValidResponseUser(){

      $adminController = new AdminController($this->Core);
      $result = $adminController->ValidResponseUser(Request::GetPost("ResponseUserId"));

      if ($result === true) {
          return Response::Success($adminController->GetTabRewardUser());
      } else {
          return Response::Error();
      } 
    }


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

      /*
      * Save User Response
      */
      function SaveUserResponse(){
        $memberController = new MemberController($this->Core);
        $result = $memberController->SaveUserResponse(Request::GetPosts());
  
        if ($result === true) {
            return Response::Success([]);
        } else {
            return Response::Error();
        }
      }

      /***
       * Demande de récompense
       */
      function ClaimThis(){
         $memberController = new MemberController($this->Core);
        $result = $memberController->ClaimThis(Request::GetPost("rewardId"));
  
        if ($result === true) {
            return Response::Success([]);
        } else {
            return Response::Error();
        } 
      }

      

    /**
     * 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\Reward\Widget\AdminDashBoard\AdminDashBoard($this->Core);
      break;
      }

      return $widget->Render();
      } */

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