<?php

namespace Hura8\Components\Page\Controller;


use Hura8\Components\Page\Model\PageLanguageModel;
use Hura8\Components\Page\Model\PageModel;
use Hura8\System\Controller\aEntityBaseController;

class bPageController extends aEntityBaseController
{

    static $image_folder = "media/static";

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


    protected  $objPageModel;


    public function __construct()
    {
        $this->objPageModel = new PageModel();

        if(!$this->isDefaultLanguage()) {
            //$this->objPageLanguageModel->createTableLang();
            parent::__construct($this->objPageModel, new PageLanguageModel());
        }else{
            parent::__construct($this->objPageModel);
        }
    }


    // get full info- basic with description
    public function getFullInfo($id) : ?array
    {
        if(!$id) return null;

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

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

            if($this->iEntityLanguageModel && $info ) {
                $item_language_info = $this->iEntityLanguageModel->getInfo($id);
                if($item_language_info) {
                    return $this->formatItemInfo(array_merge($info, $item_language_info));
                }
            }

            return $this->formatItemInfo($info);

        });
    }


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

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

        return $info;
    }


    protected function formatItemInList(array $item_info)
    {
        return $this->formatItemInfo($item_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;
    }

}
