<?php

namespace Hura8\System\Controller;

use Hura8\Interfaces\iPublicEntityController;

abstract class aPublicEntityBaseController extends aEntityBaseController implements iPublicEntityController
{

    public function getListByIds(array $list_id, array $condition = array()) : array
    {
        $item_list = array_map(function ($item){
            return $this->formatItemInList($item);
        }, $this->iEntityModel->getListByIds($list_id, $condition));

        if($this->iEntityLanguageModel) {
            $item_list_language_info = $this->iEntityLanguageModel->getListByIds($list_id);

            $final_list = [];
            foreach ($item_list as $item) {
                $item_language_info = isset($item_list_language_info[$item['id']]) ? $item_list_language_info[$item['id']] : ["not_translated" => true];
                $final_list[] = array_merge($item, $item_language_info);
            }

            return $final_list;
        }

        return $item_list;

    }


    public function getList(array $condition) : array
    {
        $copy = $condition;

        // public items must have status=1
        $copy['status'] = 1;

        return parent::getList($copy);
    }


    public function getTotal(array $condition) : int
    {
        $copy = $condition;

        // public items must have status=1
        $copy['status'] = 1;

        return parent::getTotal($copy);
    }


    public function getInfo($id): ?array
    {
        if(!$id) return null;

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

            $info = $this->iEntityModel->getInfo($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));
                }else{
                    $info["not_translated"] = true;

                    return $this->formatItemInfo($info);
                }
            }

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

        });

    }


}
