<?php

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

namespace Apps\Notify\Module\Admin;

use Core\Controller\AdministratorController;
use Core\View\View;
use Core\View\ElementView;
use Core\Control\Icone\EditIcone;
use Core\Control\Libelle\Libelle;
use Core\Control\TabStrip\TabStrip;
use Core\Control\EntityGrid\EntityGrid;
use Core\Control\EntityGrid\EntityColumn;
use Core\Control\EntityGrid\EntityIconColumn;
use Core\Control\EntityGrid\EntityFunctionColumn;
use Core\Entity\Entity\Argument;
use Core\Control\Button\Button;

use Apps\Notify\Widget\NotifyParametersForm\NotifyParametersForm;
use Apps\Notify\Entity\NotifyParameters;

use Apps\Notify\Widget\NotifyTemplateForm\NotifyTemplateForm;
use Apps\Notify\Entity\NotifyTemplate;

use Apps\Notify\Widget\NotifyPushForm\NotifyPushForm;
use Apps\Notify\Entity\NotifyPush;

use Apps\Notify\Helper\MessageHelper;

/*
 * 
 */
 class AdminController extends AdministratorController
 {
    /**
     * Constructeur
     */
    function _construct($core="")
    {
          $this->Core = $core;
    }

    /**
     * Create
     */
    function Create()
    {
    }

    /**
     * Init
     */
    function Init()
    {
    }

    /**
     * Render
     */
    function Show($all=true)
    {
         return $this->Index();
    }
    
    /*
    * Get the home page
    */
   function Index()
   {
    $view = new View(__DIR__."/View/index.tpl", $this->Core);
      
    $tabNotify = new TabStrip("tabNotify", "Notify");
    $tabNotify->AddTab($this->Core->GetCode("Notify.Parameters"), $this->GetTabParameters());
    $tabNotify->AddTab($this->Core->GetCode("Notify.Extensions"), $this->GetTabExtension());
    $tabNotify->AddTab($this->Core->GetCode("Notify.Template"), $this->GetTabTemplate());
    $tabNotify->AddTab($this->Core->GetCode("Notify.Push"), $this->GetTabPush());
    $tabNotify->AddTab($this->Core->GetCode("Notify.Notify"), $this->GetTabNotify());

    $view->AddElement(new ElementView("tabNotify", $tabNotify->Render()));

    return $view->Render();
   }

   /**
    * Parameters of notify 
    * for sender / Sender, Reply to ...
    */
   function GetTabParameters(){
       
    $view = new View(__DIR__ . "/View/tabParameter.tpl", $this->Core);
       

    $gdParameters = new EntityGrid("gdParameters", $this->Core);
    $gdParameters->Entity = "Apps\Notify\Entity\NotifyParameters";
    $gdParameters->App = "Notify";
    $gdParameters->Action = "GetTabParameters";

    $btnAdd = new Button(BUTTON, "btnAddParameters");
    $btnAdd->Value = $this->Core->GetCode("Notify.AddParameters");

    $gdParameters->AddButton($btnAdd);
    $gdParameters->AddColumn(new EntityColumn("Name", "Name"));
    $gdParameters->AddColumn(new EntityColumn("Description", "Description"));
    
    $gdParameters->AddColumn(new EntityIconColumn("Action", 
                                            array(array("EditIcone", "Notify.EditParameter", "Notify.EditParameter"),
                                                    array("DeleteIcone", "Notify.DeleteParameter", "Notify.DeleteParameter"),
                                            )    
                    ));

    $view->AddElement(new ElementView("gdParameter", $gdParameters->Render()));

    return $view->Render();
   }

   /***
    * Save the parameters
    */
   function SaveParameter($data){

      $parameterForm = new NotifyParametersForm($this->Core, $data);

      if($parameterForm->Validate($data)){

         $notifyParameter = new NotifyParameters($this->Core);

         if($data["Id"]){
            $notifyParameter->GetById($data["Id"]);
         }

         $parameterForm->Populate($notifyParameter);
         $notifyParameter->Save();
         
         return true;
      } 

      return $parameterForm->errors;
   }

   /***
    * Delete a parameter
    */
   function DeleteParameter($parameterId){
      
      $notifyParameter = new NotifyParameters($this->Core);
      $notifyParameter->GetById($parameterId);
      $notifyParameter->Delete();

      return true;
   }

   /***
     * Extension de l'application
     */
    function GetTabExtension(){
        $view = new View(__DIR__ . "/View/extension.tpl", $this->Core);
         
        $plugins = \Apps\EeApp\Helper\AppHelper::GetPluginApp($this->Core, "Notify");
        $view->AddElement(new ElementView("Plugins", $plugins));
 
        return $view->Render();
     }
    
     /***
      * Template of email
      */
     function GetTabTemplate(){
        $gdTemplate = new EntityGrid("gdTemplate", $this->Core);
        $gdTemplate->Entity = "Apps\Notify\Entity\NotifyTemplate";
        $gdTemplate->App = "Mooc";
        $gdTemplate->Action = "GetTabTemplate";

        $btnAdd = new Button(BUTTON, "btnAddTemplate");
        $btnAdd->Value = $this->Core->GetCode("Notify.AddTemplate");
        $gdTemplate->AddButton($btnAdd);

        $gdTemplate->AddColumn(new EntityColumn("Code", "Code"));
        $gdTemplate->AddColumn(new EntityColumn("Description", "Description"));
        $gdTemplate->AddColumn(new EntityColumn("Title", "Title"));
        

        $gdTemplate->AddColumn(new EntityIconColumn("Action", 
         array(array("EditIcone", "Notify.EditTemplate", "Notify.EditTemplate"),
               array("ShareIcone", "Notify.TestTemplate", "Notify.TestTemplate"),
               array("DeleteIcone", "Notify.DeleteTemplate", "Notify.DeleteTemplate"),
         )    
       ));

        return $gdTemplate->Render();
   }

   /***
    * Send Notify to user
    */
   function GetTabPush(){

      $gdPush = new EntityGrid("gdPush", $this->Core);
      $gdPush->Entity = "Apps\Notify\Entity\NotifyPush";
      $gdPush->App = "Notify";
      $gdPush->Action = "GetTabPush";

      $btnAdd = new Button(BUTTON, "btnAddPush");
      $btnAdd->Value = $this->Core->GetCode("Notify.AddPush");
      $gdPush->AddButton($btnAdd);

      $gdPush->AddColumn(new EntityColumn("Name", "Name"));
      $gdPush->AddColumn(new EntityColumn("Description", "Description"));
      $gdPush->AddColumn(new EntityColumn("DateSend", "DateSend"));
      $gdPush->AddColumn(new EntityColumn("HourSend", "HourSend"));

      $gdPush->AddColumn(new EntityIconColumn("Action", 
       array(array("EditIcone", "Notify.EditPush", "Notify.EditPush"),
             array("ShareIcone", "Notify.TestPush", "Notify.TestPush"),
             array("DeleteIcone", "Notify.DeletePush", "Notify.DeletePush"),
       )    
     ));

      return $gdPush->Render();


      $view = new View(__DIR__ . "/View/push.tpl", $this->Core);

      return $view->render();
   }  

     /**
    * Parameters of notify 
    * for sender / Sender, Reply to ...
    */
   function GetTabNotify(){

    $gdNotify = new EntityGrid("gdNotify", $this->Core);
    $gdNotify->Entity = "Apps\Notify\Entity\NotifyNotify";
    $gdNotify->App = "Mooc";
    $gdNotify->Action = "GetTabParameters";

    $gdNotify->AddColumn(new EntityColumn("DateCreate", "DateCreate"));
    $gdNotify->AddColumn(new EntityColumn("UserId", "UserId"));
    $gdNotify->AddColumn(new EntityColumn("Code", "Code"));
    $gdNotify->AddColumn(new EntityColumn("DestinataireId", "DestinataireId"));
    $gdNotify->AddColumn(new EntityColumn("AppName", "AppName"));
    $gdNotify->AddColumn(new EntityColumn("EntityName", "EntityName"));
    $gdNotify->AddColumn(new EntityColumn("EntityId", "EntityId"));
    
    return $gdNotify->Render();
   }

   /***
    * Save a template
    */
   function SaveTemplate($data){

      $templateForm = new NotifyTemplateForm($this->Core, $data);

      if($templateForm->Validate($data)){

         $notifyTemplate = new NotifyTemplate($this->Core);

         if($data["Id"]){
            $notifyTemplate->GetById($data["Id"]);
         }

         $templateForm->Populate($notifyTemplate);
         $notifyTemplate->Save();
         
         return true;
      } 

      return $templateForm->errors;
   }

   /***
    * Delete a template
    */
   function DeleteTemplate($templateId){
      $notifyTemplate = new NotifyTemplate($this->Core);
      $notifyTemplate->GetById($templateId);
      $notifyTemplate->Delete();
      
      return true;
   }

   /***
    * Save a push
    */
    function SavePush($data){

      $pushForm = new NotifyPushForm($this->Core, $data);

      if($pushForm->Validate($data)){

         $notifyPush = new NotifyPush($this->Core);

         if($data["Id"]){
            $notifyPush->GetById($data["Id"]);
         }

         $pushForm->Populate($notifyPush);
         $notifyPush->Save();
         
         return true;
      } 

      return $pushForm->errors;
   }
   
    /*
     * Envoi un email de test
     */
   function SendEmailTest($data){
       $email = $data["testEmail"];
       $message = $data["testMessage"];
       
       MessageHelper::SendMail($this->Core, $email, "test message", $message);
   }

  
   
   /****
    * Test a template
    */
   function TestTemplate($data){
       
      $notifyTemplate = new NotifyTemplate($this->Core);
      $notifyTemplate->GetById($data["TemplateId"]);
      
      MessageHelper::SendMail($this->Core, $data["Email"], $notifyTemplate->Title->Value, $notifyTemplate->Content->Value);
   }

   /***
    * Send a push notity to a mobile
    */
     /**
     * Envoi de la notification via le serveru Google
     * @param type $registrationId
     */
    function Push($registrationId, $title, $message) {
  
      $notification = array('title' => $title, 'body' => $message, 'sound' => 'default', 'badge' => '1');
      $arrayToSend = array('to' => $registrationId, 'notification' => $notification, 'priority' => 'high');

      // $server_key = 'AAAAO85vcwk:APA91bFRLrZHCYivhxUmjp-OGDpW5saC3rPJnwi1gqXwJQnwgWWU_-TtEmo230H_osVGOr7od3KtEwqAlIFC1jnQ5ZXXlh0-5WoC_557PY1IV0mu2yjBsrNNcjoyzbAoZaGwCLsv72ul';
      $server_key = 'AAAArf8dV6Y:APA91bF4UFVivE9LxfLcdGJfEhFmXHdsXZF3JY95Y_Chu5pm_w1o4p6dK6acNmGkQHcHiQt4IhvfCbS2NZIrctHHap-G4euKOxEBDsvPKejR1a_SVg8SK7NuL1l4B3uoIHvLRHQDQDCc';
      
      $headers = array(
          'Content-Type:application/json',
          'Authorization:key=' . $server_key
      );

      $url = 'https://fcm.googleapis.com/fcm/send';
      //CURL request to route notification to FCM connection server (provided by Google)
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_POST, true);
      curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
      curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($arrayToSend));
      $result = curl_exec($ch);

      if ($result === FALSE) {
          die('Oops! FCM Send Error: ' . curl_error($ch));
      }
      curl_close($ch);
  }


  
          /*action*/
 }?>