<?php

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

namespace Apps\Annonce\Module\Api;

use Core\Controller\Controller;

use Core\View\View;
use Core\View\ElementView;
use Apps\Annonce\Entity\AnnonceCategory;
use Apps\Annonce\Entity\AnnonceAnnonce;


/*
 * 
 */

class ApiController extends Controller {

    /***
     * Get All Categorie
     */
    function GetCategories(){
        
        $categorie = new AnnonceCategory($this->Core);
        $categories =  $categorie->GetAll();
        
        return $categorie->ToAllArray($categories);
    }
    
    /***
     * Get the categorie and the annonce
     */
    function GetCategorie($categoryId){
        
          $categorie = new AnnonceCategory($this->Core);
          $categorie->GetById($categoryId);
          
          $annonce = new AnnonceAnnonce($this->Core);
          $annonces = $annonce->Find("CategoryId=" . $categoryId);
          
          
          return array("category" => $categorie->ToArray(), "annonces" =>  $annonce->ToAllArray($annonces) );
    }
    
    /***
     * Get detail of a annonce
     */
    function GetAnnonce($annonceId){
        $annonce = new AnnonceAnnonce($this->Core);
        $annonce->GetById($annonceId);
        
        $annocneArray = $annonce->ToArray();
        $annocneArray["Caracteristics"] = $annonce->GetCaracteristics();
        
        return $annocneArray; 
    }
    
    /***
     * Listes des annonces
     */
    function GetListAnnonce(){
        
        $caracteristicplugin = \Apps\EeApp\Helper\AppHelper::GetPluginType($this->Core, "Annonce" , "Caracteristic");
        
        if($caracteristicplugin != null){
            $filters = $caracteristicplugin->GetFilters("Annonce");
        } else{
          $filters = [];  
        }   

        $annonce = new AnnonceAnnonce($this->Core);
        $annonces =  $annonce->Find("1 = 1");
        $annonceArray = [];
        
        foreach($annonces as $annone){
            $dataAnnonce = $annone->ToArray();
            $dataAnnonce["Caracteristics"] = $annone->GetCaracteristicEntity();
            $annonceArray[] = $dataAnnonce;
        }
        
        return ["filters"  =>$filters ,"annonces" =>  $annonceArray];
        
        //$view->AddElement(new ElementView("Filters", $filtersCaracteristics ? $filtersCaracteristics : "" )); 
       
        //$view->AddElement(new ElementView("Annonces", $this->GetList())); 

        $alertPlugin = \Apps\EeApp\Helper\AppHelper::GetPluginType($this->Core, "Annonce" , "Alert");
        
        if($alertPlugin != null){
            $alertForm = $alertPlugin->RenderAlertForm("Annonce");
        }    
    }
    
    public function GetFilters(){
        $caracteristicplugin = \Apps\EeApp\Helper\AppHelper::GetPluginType($this->Core, "Annonce" , "Caracteristic");
        
        if($caracteristicplugin != null){
            $filters = $caracteristicplugin->GetFilters("Annonce");
        } else{
          $filters = [];  
        }
        
        return $filters;
    }
    
    /**
     * Création d'une alerte
     * @param type $data
     * 
     */
    public function CreateAlert($data){
        
        $alert = new \Plugins\AlertPlugin\Entity\AlertPluginAlert($this->Core);
      
        if($data["EntityId"]){
            $alert->EntityId->Value = $data["EntityId"];
            $alert->EntityName->Value ="Annonce";
            
        } else {
            $alert->EntityName->Value ="AnnonceFilter";
        }
        
        $alert->Email->Value = $data["Email"];
        $alert->FirstName->Value = $data["FirstName"];
        $alert->Name->Value = $data["Name"];
        $alert->DateCreated->Value = \Core\Utility\Date\Date::now();
        $alert->Data->Value = json_encode(base64_decode($data["Data64"]));
 
        $alert->Save();
    }
    
    
    public function GetTrackingForm($data){
        
        $stat = new \Plugins\ClickTrackPlugin\Entity\ClickTrackPluginStat($this->Core);
        $stat->DateCreated->Value = \Core\Utility\Date\Date::Now();
        $stat->EntityName->Value = "Annonce";    
        $stat->EntityId->Value = $data["EntityId"];    
        $stat->StateType->Value = $data["Action"];  
        $stat->Save();
        
        switch($data["Action"]){
            case "phone":
                return $this->Core->GetCode("ClickTrackPlugin.PhoneDescription");
            break; 
            default :
                return $data["Action"];
                break;
        }
    }
    
    /****
     * Message sur une annocne
     */
    public function SendContactForm($data){
          $message = new \Plugins\ClickTrackPlugin\Entity\ClickTrackPluginMessage($this->Core);
        $message->DateCreated->Value =  \Core\Utility\Date\Date::Now();
        $message->EntityName->Value = "Annonce";    
        $message->EntityId->Value = $data["EntityId"];    
      
        $message->FirstName->Value = $data["FirstName"];    
        $message->Name->Value = $data["Name"];    
        $message->Phone->Value = $data["Phone"];    
        $message->Email->Value = $data["Email"];    
        $message->Message->Value = $data["Message"];    
      
        $message->Save();
    }
    
}
    
