<?php

namespace Magetop\Agency\Model;

use Magetop\Agency\Api\Data\ItemsInterface;
use Magento\Framework\DataObject\IdentityInterface;

class Items extends \Magento\Framework\Model\AbstractModel implements ItemsInterface, IdentityInterface
{
    /**
     * No route page id.
     */
    const NOROUTE_ENTITY_ID = 'no-route';

    /**
     * Assign Product Items cache tag.
     */
    const CACHE_TAG = 'assignproduct_items';

    /**
     * @var string
     */
    protected $_cacheTag = 'assignproduct_items';

    /**
     * Prefix of model events names.
     *
     * @var string
     */
    protected $_eventPrefix = 'assignproduct_items';

    /**
     * Initialize resource model.
     */
    protected function _construct()
    {
        $this->_init('Magetop\Agency\Model\ResourceModel\Items');
    }

    /**
     * Load object data.
     *
     * @param int|null $id
     * @param string   $field
     *
     * @return $this
     */
    public function load($id, $field = null)
    {
        if ($id === null) {
            return $this->noRoutePreorder();
        }

        return parent::load($id, $field);
    }

    /**
     * Load No-Route Items.
     *
     * @return \Magetop\Agency\Model\Items
     */
    public function noRouteItems()
    {
        return $this->load(self::NOROUTE_ENTITY_ID, $this->getIdFieldName());
    }

    /**
     * Get identities.
     *
     * @return array
     */
    public function getIdentities()
    {
        return [self::CACHE_TAG.'_'.$this->getId()];
    }

    /**
     * Get ID.
     *
     * @return int
     */
    public function getId()
    {
        return parent::getData(self::ENTITY_ID);
    }

    /**
     * Set ID.
     *
     * @param int $id
     *
     * @return \Magetop\Agency\Api\Data\ItemsInterface
     */
    public function setId($id)
    {
        return $this->setData(self::ENTITY_ID, $id);
    }
}
