<?php

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

namespace Apps\Forum\Helper;

use Core\Entity\Entity\Argument;
use Core\Utility\Date\Date;
use Apps\Forum\Entity\ForumReponse;
use Apps\Forum\Entity\ForumMessage;
use Apps\Forum\Widget\ReponseForm\ReponseForm;

class ReponseHelper
{   
    /**
     * Sauvegarde une réponse
     * @param object $core Core object
     * @param array $data Form data
     * @param int $sujetId Message ID to which the response is attached
     * @return boolean Success status
     */
    public static function SaveReponse($core, $data, $sujetId = null)
    {
        $reponseForm = new ReponseForm($core, $data);
        
        if($sujetId !== null) {
            $reponseForm->SetSujetId($sujetId);
        } elseif(isset($data['MessageId'])) {
            $reponseForm->SetSujetId($data['MessageId']);
        } else {
            return false; // Pas d'identifiant de sujet
        }

        if($reponseForm->Validate($data)) {
            $reponse = new ForumReponse($core);
            
            if(isset($data["Id"]) && $data["Id"] != "") {
                $reponse->GetById($data["Id"]);
            }

            $reponseForm->Populate($reponse);
            $reponse->Save();

            return true;
        }

        return false;
    }
    
    /**
     * Obtient les réponses d'un message
     * @param object $core Core object
     * @param int $messageId Message ID
     * @param boolean $toArray Convert to array or not
     * @return array Responses
     */
    public static function GetReponsesByMessage($core, $messageId, $toArray = false)
    { 
        $reponse = new ForumReponse($core);
        $reponse->AddArgument(new Argument("Apps\Forum\Entity\ForumReponse", "MessageId", EQUAL, $messageId));
        $reponse->AddOrder("DateCreated ASC");
        $reponses = $reponse->GetByArg();
        
        if($toArray == false) {
            return $reponses;
        } else {
            return $reponse->ToAllArray($reponses);
        }
    }
    
    /**
     * Supprime une réponse
     * @param object $core Core object
     * @param int $reponseId Response ID to delete
     * @return boolean Success status
     */
    public static function DeleteReponse($core, $reponseId)
    {
        $reponse = new ForumReponse($core);
        $reponse->GetById($reponseId);
        return $reponse->Delete();
    }
    
    /**
     * Met à jour une réponse
     * @param object $core Core object
     * @param array $data Form data
     * @return boolean Success status
     */
    public static function UpdateReponse($core, $data)
    {
        if(!isset($data['Id']) || $data['Id'] == "") {
            return false;
        }
        
        $reponseForm = new ReponseForm($core, $data);
        
        if(isset($data['MessageId'])) {
            $reponseForm->SetSujetId($data['MessageId']);
        }

        if($reponseForm->Validate($data)) {
            $reponse = new ForumReponse($core);
            $reponse->GetById($data["Id"]);

            $reponseForm->Populate($reponse);
            $reponse->Save();

            return true;
        }

        return false;
    }
    
    /**
     * Obtient une réponse par son ID
     * @param object $core Core object
     * @param int $reponseId Response ID
     * @param boolean $toArray Convert to array or not
     * @return mixed Response entity or array
     */
    public static function GetReponse($core, $reponseId, $toArray = false)
    {
        $reponse = new ForumReponse($core);
        $reponse->GetById($reponseId);
        
        if($toArray) {
            return $reponse->ToArray();
        }
        
        return $reponse;
    }
    
    /**
     * Obtient la dernière réponse d'un message
     * @param object $core Core object
     * @param int $messageId Message ID
     * @return mixed Response entity or null
     */
    public static function GetLastReponse($core, $messageId)
    {
        $reponse = new ForumReponse($core);
        $reponse->AddArgument(new Argument("Apps\Forum\Entity\ForumReponse", "MessageId", EQUAL, $messageId));
        $reponse->AddOrder("DateCreated DESC");
        $reponse->AddLimit(1);
        
        $reponses = $reponse->GetByArg();
        
        if(count($reponses) > 0) {
            return $reponses[0];
        }
        
        return null;
    }
    
    /**
     * Compte le nombre de réponses pour un message
     * @param object $core Core object
     * @param int $messageId Message ID
     * @return int Number of responses
     */
    public static function GetNumberReponse($core, $messageId)
    {
        $reponses = self::GetReponsesByMessage($core, $messageId);
        return count($reponses);
    }
}
?>
