<?php
namespace Claromentis\Core\Like\Controller;

$GLOBALS["__65da1b1ec2d6653e8a0a" . "b7848e536215"] = (@$GLOBALS["__65da1b1ec2d6" . "653e8a0ab7848e" . "536215"] ?: array()) + array("_5784e706ef84e048186a7" . "0a164fcc358" => base64_decode("T2JqZWN0IElEIGlzIG5vdCBzcGVjaWZpZW" . "Qgb3IgaW52YWxpZA=="), "_37317e99fd1ac0bd2cca360fcc5fd6" . "12" => base64_decode("dXNlcl9uYW" . "1lLmJvZHk="), "_f4eafa5f493306ffdb51a688b21dd" . "507" => base64_decode("dG90Y" . "Ww="), "_5e8488c301cda0085b5" . "ca6c78c50687d" => base64_decode("cHJvZmlsZV9waG90by5" . "zcmM="), "_62d701c3c53404" . "f06345eb35720a4" . "7b4" => base64_decode("aXNfbnVtZXJ" . "pYw=="), "_2409bed918465b496e1f712f2c" . "651403" => base64_decode("bGlrZXJzLm" . "RhdGFzcmM="), "_5203b6250b088a619af7" . "41a56f3a0c26" => base64_decode("Y29tbW9uL2xpa2UvbGlrZXJzX2xp" . "c3QuaHRtbA=="), "_aa45d9aa411eac890badddf" . "edf2ade38" => base64_decode("T2JqZWN0IH" . "R5cGUgaXMg" . "bm90IHNwZW" . "NpZmllZA=="), "_71f564ea75084c6f261db81c655d2" . "f73" => base64_decode("cHJvZmlsZV9saW5rLmhyZWY" . "="), "_532af466941018fd395" . "4ccc8aed6a235" => base64_decode("bGlr" . "ZWQ="), "use" . "r_i" . "d" => base64_decode("dXNlcl9p" . "ZA=="));
use Claromentis\Core\Application;
use Claromentis\Core\Http\JsonPrettyResponse;
use Claromentis\Core\Http\StatusCode;
use Psr\Http\Message\ServerRequestInterface as Request;
use Claromentis\Core\Http\TemplaterCallResponse;
use RestFormat;






class RestV1Controller
{
    
    
    
    
    protected $formatter;
    
    
    
    
    public function __construct(RestFormat $formatter)
    {
        $this->formatter = $formatter;
    }
    
    
    
    
    
    
    
    
    public function AddLike(Application $app, $object_type, $object_id)
    {
        list($aggregation, $object_id) = $this->CheckObject($object_type, $object_id, $app);
        $app->likes->Add($aggregation, $object_id, $app->security->GetUserId());
        $_1f24efd4735b7a11b391147435e7d35d = [$GLOBALS["__65da1b1ec2" . "d6653e8a0ab7" . "848e536215"]["_f4eafa5f4" . "93306ffdb5" . "1a688b21dd" . "507"] => (int) $app->likes->GetCount($aggregation, $object_id)];
        return new JsonPrettyResponse($_1f24efd4735b7a11b391147435e7d35d, StatusCode::OK);
    }
    
    
    
    
    
    
    
    
    public function GetMeLiked(Application $app, $object_type, $object_id)
    {
        list($aggregation, $object_id) = $this->CheckObject($object_type, $object_id, $app);
        $_1f24efd4735b7a11b391147435e7d35d = [$GLOBALS["__65da1b1ec2d665" . "3e8a0ab7848e5362" . "15"]["_532af466941018fd3954cc" . "c8aed6a235"] => (bool) $app->likes->IsLiked($aggregation, $object_id, $app->security->GetUserId())];
        return new JsonPrettyResponse($_1f24efd4735b7a11b391147435e7d35d);
    }
    
    
    
    
    
    
    public function GetLikesCount(Application $app, $object_type, $object_id)
    {
        list($aggregation, $object_id) = $this->CheckObject($object_type, $object_id, $app);
        $_1f24efd4735b7a11b391147435e7d35d = [$GLOBALS["__65da1b1ec2d6653" . "e8a0ab7848e536215"]["_f4eafa5f49" . "3306ffdb51a" . "688b21dd507"] => (int) $app->likes->GetCount($aggregation, $object_id)];
        return new JsonPrettyResponse($_1f24efd4735b7a11b391147435e7d35d);
    }
    
    
    
    
    
    
    
    public function GetLikes(Application $app, Request $request, $object_type, $object_id)
    {
        list($aggregation, $object_id) = $this->CheckObject($object_type, $object_id, $app);
        list($_f11369824f533e8206f9ccc0c9aab3a0, $_1f50e30be8c2fefa6881834e71d504a2) = $this->formatter->PagingGetLimits($request->getQueryParams());
        $_f4eafa5f493306ffdb51a688b21dd507 = $app->likes->GetCount($aggregation, $object_id);
        if ($_f4eafa5f493306ffdb51a688b21dd507 > 0) {
            $likes = $app->likes->GetLikes($aggregation, $object_id, $_f11369824f533e8206f9ccc0c9aab3a0, $_1f50e30be8c2fefa6881834e71d504a2);
        } else {
            $likes = [];
        }
        $_1f24efd4735b7a11b391147435e7d35d = $this->formatter->PagingContainer($likes, $_f11369824f533e8206f9ccc0c9aab3a0, $_1f50e30be8c2fefa6881834e71d504a2, $_f4eafa5f493306ffdb51a688b21dd507);
        return new JsonPrettyResponse($_1f24efd4735b7a11b391147435e7d35d);
    }
    
    
    
    
    
    
    
    public function GetLikesHtml(Application $app, Request $request, $object_type, $object_id)
    {
        list($aggregation, $object_id) = $this->CheckObject($object_type, $object_id, $app);
        list($_f11369824f533e8206f9ccc0c9aab3a0, $_1f50e30be8c2fefa6881834e71d504a2) = $this->formatter->PagingGetLimits($request->getQueryParams());
        $likes = $app->likes->GetLikes($aggregation, $object_id, $_f11369824f533e8206f9ccc0c9aab3a0, $_1f50e30be8c2fefa6881834e71d504a2);
        $_e7438083ff42e99584f6022253250369 = [];
        foreach ($likes as $_9ecb1a728fdb50fbae4581f824a09366) {
            $user_id = $_9ecb1a728fdb50fbae4581f824a09366[$GLOBALS["__65da1b1ec2d6653e8a0ab7848e" . "536215"]["user" . "_id"]];
            $_343a395319323dd7609bcbc465b4d034 = [$GLOBALS["__65da1b1ec2d665" . "3e8a0ab7848e5362" . "15"]["_71f564ea75084c6" . "f261db81c655d2f7" . "3"] => \User::GetProfileUrl($user_id), $GLOBALS["__65da1b1ec2d6653" . "e8a0ab7848e536215"]["_5e8488c301cda0085b5ca6c78c50687" . "d"] => \User::GetPhotoUrl($user_id), $GLOBALS["__65da1b1ec2d66" . "53e8a0ab7848e53" . "6215"]["_37317e99fd1ac0bd2" . "cca360fcc5fd612"] => \User::GetNameById($user_id)];
            $_e7438083ff42e99584f6022253250369[] = $_343a395319323dd7609bcbc465b4d034;
        }
        $args = [$GLOBALS["__65da1b1ec2d6653e8a0ab" . "7848e536215"]["_2409bed918465b496e1f712f2c65" . "1403"] => $_e7438083ff42e99584f6022253250369];
        $template = $GLOBALS["__65da1b1ec2d6653e8a0ab7848e" . "536215"]["_5203b6250b088a619af741a56f3a0c" . "26"];
        return new TemplaterCallResponse($template, $args);
    }
    
    
    
    
    
    
    private function CheckObject($object_type, $object_id, Application $app)
    {
        if (!$GLOBALS["__65da1b1ec2d6653e8a0ab784" . "8e536215"]["_62d701c3c53404f06345eb35720" . "a47b4"]($object_type)) {
            $aggregation = \ClaAggregations::GetNumByCode($object_type);
        } elseif ($GLOBALS["__65da1b1" . "ec2d6653e" . "8a0ab7848" . "e536215"]["_62d701c3c53404f" . "06345eb35720a47b" . "4"]($object_type)) {
            $aggregation = (int) $object_type;
        }
        if (empty($aggregation)) {
            throw new \RestExBadRequest($GLOBALS["__65da1b1ec2d6653e8a0a" . "b7848e536215"]["_aa45d9aa411eac" . "890badddfedf2ad" . "e38"]);
        }
        if (!$GLOBALS["__65da1b1ec2d6653e8a" . "0ab7848e536215"]["_62d701c3c53404f06345eb35720" . "a47b4"]($object_id)) {
            throw new \RestExBadRequest($GLOBALS["__65da1b1ec2d6653e8a0ab7848e5362" . "15"]["_5784e706ef84e048" . "186a70a164fcc358"]);
        }
        
        
        return array($aggregation, $object_id);
    }
}