<?php

namespace Hura8\Components\Article\Controller;


use Hura8\Components\Article\Model\ArticleLanguageModel;
use Hura8\Components\Article\Model\ArticleModel;
use Hura8\System\Controller\aEntityBaseController;

class bArticleController extends aEntityBaseController
{
    static $image_folder = "media/article";

    static $resized_sizes = array(
        't' => ['width' => 200,] ,
        'l' => ['width' => 600,] ,
    );

    /* @var ArticleModel $objArticleModel */
    protected $objArticleModel;

    public function __construct()
    {
        $this->objArticleModel = new ArticleModel();

        if(!$this->isDefaultLanguage()) {
            parent::__construct(
                $this->objArticleModel,
                new ArticleLanguageModel()
            );

        } else {
            parent::__construct($this->objArticleModel);
        }
    }


    public function getFullInfo($id)
    {

        if(!$id) return null;

        return self::getCache("getFullInfo-".$id."-".$this->view_language, function () use ($id){

            $info = $this->objArticleModel->getFullInfo($id);

            if($this->iEntityLanguageModel && $info ) {
                $item_language_info = $this->iEntityLanguageModel->getInfo($id) ?? ["not_translated" => true];
                return $this->formatItemInfo(array_merge($info, $item_language_info));
            }

            return ($info) ? $this->formatItemInfo($info) : null;

        });
    }


    protected function formatItemInList(array $item_info)
    {
        return $this->formatItemInfo($item_info);
    }


    protected function formatItemInfo(array $item_info)
    {
        if(!$item_info) return null;

        $info = $item_info;
        $info['image'] = self::getResizedImageCollection($info['thumbnail']);

        return $info;
    }


    public static function getResizedImageCollection($image_name) {
        $image = [];

        $size_in_full = [
            't' => 'thumb' ,
            's' => 'small' ,
            'l' => 'large' ,
        ];

        foreach (static::$resized_sizes as $size => $value) {
            $image[$size_in_full[$size]] = ($image_name) ? STATIC_DOMAIN . "/". static::$image_folder . "/". $size. IMAGE_FILE_SEPARATOR . $image_name : '';
        }

        return $image;
    }

}
