<?php

namespace Hura8\Components\Media\Controller;

use Hura8\Components\Media\Model\ItemMediaModel;
use Hura8\System\Controller\aEntityBaseController;


class bItemMediaController  extends aEntityBaseController
{
    static $image_folder = "media/item";

    static $resized_sizes = array(
        's' => ['width' => 300,]
    );

    /* @var ItemMediaModel $objItemMediaModel */
    protected $objItemMediaModel;

    protected $item_type;
    protected $item_id;

    public function __construct(string $item_type = '', $item_id = 0)
    {
        $this->objItemMediaModel = new ItemMediaModel($item_type, $item_id);
        parent::__construct($this->objItemMediaModel);
    }


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


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

        $item_info['display_file'] = STATIC_DOMAIN . "/". static::$image_folder. "/". $item_info['file_url'];
        $item_info['image'] = self::getResizedImageCollection($item_info['file_url']);

        return $item_info;
    }


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

        $size_in_full = [
            's' => 'small' ,
        ];

        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;
    }

}
