<?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);
        
        return $annonce->ToArray(); 
    }
    
    /***
     * 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");
        }    
    }
    
}
    
