<?php

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

namespace Apps\Annonce\Entity;

use Core\Entity\Entity\Entity;
use Core\Entity\Entity\Property;
use Core\Entity\Entity\EntityProperty;

use Core\Utility\Format\Format;


class AnnonceAnnonce extends Entity {

     //Entité liée
    protected $Category;
    protected $User;
    
    //Constructeur
    function __construct($core) {
        //Version
        $this->Version = "2.0.0.0";

        //Nom de la table 
        $this->Core = $core;
        $this->TableName = "AnnonceAnnonce";
        $this->Alias = "AnnonceAnnonce";

        $this->Title = new Property("Title", "Title", TEXTBOX, false, $this->Alias);
        $this->Code = new Property("Code", "Code", TEXTBOX, false, $this->Alias);
        $this->Description = new Property("Description", "Description", TEXTAREA, false, $this->Alias);
        $this->CategoryId = new Property("CategoryId", "CategoryId", NUMERICBOX, false, $this->Alias);
        $this->UserId = new Property("UserId", "UserId", NUMERICBOX, false, $this->Alias);
        $this->Status = new Property("Status", "Status", NUMERICBOX, false, $this->Alias);

        $this->Category = new EntityProperty("Apps\Annonce\Entity\AnnonceCategory", "CategoryId"); 
        $this->User = new EntityProperty("Core\Entity\User\User", "UserId"); 
     
        //Image directory    
        $this->DirectoryImage = "Data/Apps/Annonce/Annonce/";
        
        //Partage entre application 
        $this->AddSharedProperty();

        //Creation de l entité 
        $this->Create();
    }
    
    /***
     * GetThumbImage 
     */
    function GetThumbImage(){
        
        if(file_exists($this->DirectoryImage . $this->IdEntite ."/thumb.png")){
            $view = "<img src='".$this->DirectoryImage . $this->IdEntite ."/thumb.png' />";
             return  $view; 
        } 
        
        return "";
    }
    
    /***
     * Get Image Path
     */
    function GetImagePath(){
        
        if(file_exists($this->DirectoryImage . $this->IdEntite ."/full.png")){
             return "/" . $this->DirectoryImage . $this->IdEntite ."/full.png" ;
        } 
        
        return $this->Core->GetPath("/images/nophoto.png");
    }
    
    function GetImagesPath(){
        return implode(";" , $this->GetImages());
    }

    /***
     * Get className for Render images
     */
    function GetClassRenderImage(){

        if(count($this->GetImages()) > 0){
            return "col-md-4";
        } else {
            return "";
        }
    }
    
      /*
     * Get A small Description
     */
    function GetSmallDescription()
    {
        return Format::Tronquer($this->Description->Value, 200);
    }
    
    
    function GetNumberImage(){
       return count($this->GetImages());
    }
    
    /***Caracteristic */
    function GetCaracteristics(){
     
        $caracteristicPlugin = \Apps\EeApp\Helper\AppHelper::GetPluginType($this->Core, "Annonce", "Caracteristic");
        
        if($caracteristicPlugin){
            return  $caracteristicPlugin->RenderDetailFront($this->IdEntite, "Annonce");
        }
    }
    
    function GetCaracteristicEntity(){
        $caracteristicPlugin = \Apps\EeApp\Helper\AppHelper::GetPluginType($this->Core, "Annonce", "Caracteristic");
        
        if($caracteristicPlugin){
            return  $caracteristicPlugin->GetByAppAndEntity($this->IdEntite, "Annonce");
        }
    }
    
    /***
     * Stat de clik
     */
    function GetClickTrackStat(){
        
        $trackPlugin = \Apps\EeApp\Helper\AppHelper::GetPluginType($this->Core, "Annonce", "ClickTrack");

        if($trackPlugin != null ){

              $stats = $trackPlugin->GetStatsByEntity("Annonce", $this->IdEntite);
              
              $view = "<div>";
              foreach($stats as $stat){
                  
                  switch($stat["StateType"]){
                      case "phone":
                           $picto = "<i class='fa fa-phone' title='".$this->Core->GetCode("Annonce.ClickForPhone")."' ></i>";
                          break;
                      case "contact":
                           $picto = "<i class='fa fa-envelope' title='".$this->Core->GetCode("Annonce.ClickForContact")."' ></i>";
                          break;
                      case "alert":
                           $picto = "<i class='fa fa-bell' title='".$this->Core->GetCode("Annonce.ClickForCreateAlert")."'></i>";
                          break;
                  }
                  $view .= "<span style='margin-right:5px;margin-left:5px' >".$picto." ".$stat["nbElement"]. "</span>";
                  
              }
              $view .= "</div>";
        
            return $view;      
        }
    }
    
    /***
     * Get Message of a annonce
     */
    function GetClickTrackMessage(){
        
         $trackPlugin = \Apps\EeApp\Helper\AppHelper::GetPluginType($this->Core, "Annonce", "ClickTrack");

        if($trackPlugin != null ){
            $messages = $trackPlugin->GetMessagesByEntity("Annonce", $this->IdEntite);
        
            foreach($messages as $message){
                $view .="<div style='width:300px;border-bottom:1px solid grey;'>";
            
                $view .= "<b><i class='fa fa-user'></i>:".$message->FirstName->Value . " " . $message->Name->Value ."</b>";
                $view .= "<br/><i class='fa fa-envelope'></i> : ".$message->Email->Value . " - <i class='fa fa-phone'></i>" . $message->Phone->Value ;
                $view .= "<br/><p>".$message->Message->Value. "</p>";
                $view .= "</div>";
            }
            
            return $view; 
        }
    }

  /***
     * Get Message of a annonce
     */
    function GetAlert(){
        
        $alertPlugin = \Apps\EeApp\Helper\AppHelper::GetPluginType($this->Core, "Annonce", "Alert");

       if($alertPlugin != null ){
        $alertes = $alertPlugin->GetAlertByEntity("Annonce", $this->IdEntite);
        
        foreach($alertes as $message){
            $view .="<div style='width:300px;border-bottom:1px solid grey;'>";
        
            $view .= "<b><i class='fa fa-user'></i>:".$message->FirstName->Value . " " . $message->Name->Value ."</b>";
            $view .= "<br/><i class='fa fa-envelope'></i> : ".$message->Email->Value;
            $view .= "</div>";
        }
        
        return $view;
  
        }
    } 
}

?>