<?php

namespace Hura8\System\Controller;


use Hura8\System\Model\WebUserModel;

class ViewHistoryController
{
    protected $history = []; //type => [id1, id2]

    protected  $objWebUserModel;

    public function __construct()
    {
        $this->objWebUserModel = new WebUserModel(WebUserController::getUserId());
    }


    public function addHistory($item_type, $item_id) {
        $current_list = $this->getHistory($item_type);

        // if exist, remove it
        $search_key = array_search($item_id, $current_list, true);
        if($search_key !== false) {
            array_splice($current_list, $search_key, 1);
        }

        // add to front
        array_unshift($current_list, $item_id);

        $this->history[$item_type] = $current_list;

        // save to db
        $this->objWebUserModel->setValue("view-history", $this->history);
    }


    public function getHistory($item_type) {
        $history = $this->objWebUserModel->getValue("view-history");
        return (isset($history[$item_type])) ? $history[$item_type] : [];
    }

}
