<?php
/**
 * Badges Model
 *
 * @module Autogedal_Badges
 * @author Malith Priyashan
 * @package Autogedal\Badges\Model
 * @licence OSL 3.0
 */

namespace Autogedal\Badges\Model;

use Magento\Framework\Model\ResourceModel\AbstractResource;
use Magento\Framework\DataObject\IdentityInterface;
use Magento\Framework\Data\Collection\AbstractDb;
use Magento\Framework\Model\AbstractModel;
use Magento\Framework\Model\Context;
use Magento\Framework\UrlInterface;
use Magento\Framework\Registry;
use Autogedal\Badges\Api\Data\BadgeInterface;

/**
 * Class Badge
 */
class Badge  extends AbstractModel implements BadgeInterface, IdentityInterface
{
    /**
     * Badge's Statuses
     */
    const STATUS_ENABLED = 1;
    const STATUS_DISABLED = 0;

    /**
     * CMS page cache tag
     */
    const CACHE_TAG = 'Badges_Badge';

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

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

    /**
     * @var UrlInterface
     */
    protected $_urlBuilder;

    /**
     * Badge Constructor.
     *
     * @param Context $context
     * @param Registry $registry
     * @param AbstractResource|null $resource
     * @param AbstractDb|null $resourceCollection
     * @param UrlInterface $urlBuilder
     * @param array $data
     */
    function __construct(
        UrlInterface $urlBuilder,
        Context $context,
        Registry $registry,
        AbstractResource $resource = null,
        AbstractDb $resourceCollection = null,
        array $data = []
    ) {
        $this->_urlBuilder = $urlBuilder;

        parent::__construct(
            $context,
            $registry,
            $resource,
            $resourceCollection,
            $data
        );
    }

    /**
     * Initialize resource model
     *
     * @return void
     */
    protected function _construct()
    {
        $this->_init('Autogedal\Badges\Model\ResourceModel\Badge');
    }

    /**
     * Check if Badge url key exists
     * return Badge id if Badge exists
     *
     * @param string $url_key
     *
     * @return int
     *
     * @TODO Avoid using deprecated class
     */
    public function checkUrlKey($url_key)
    {
        return $this->_getResource()->checkUrlKey($url_key);
    }

    /**
     * Prepare Badge's statuses.
     * Available event Badges_Badge_get_available_statuses to customize statuses.
     *
     * @return array
     */
    public function getAvailableStatuses()
    {
        return [self::STATUS_ENABLED => __('Enabled'), self::STATUS_DISABLED => __('Disabled')];
    }

    /**
     * Return unique ID(s) for each object in system
     *
     * @return array
     */
    public function getIdentities()
    {
        return [self::CACHE_TAG . '_' . $this->getId()];
    }

    /**
     * Get ID
     *
     * @return int|null
     */
    public function getId()
    {
        return $this->getData(self::BADGE_ID);
    }

    /**
     * Get URL Key
     *
     * @return string
     */
    public function getUrlKey()
    {
        return $this->getData(self::URL_KEY);
    }

    /**
     * Return the desired URL of a Badge
     *  eg: /badges/view/index/id/1/
     * @TODO Move to a BadgeUrl model, and make use of the
     * @TODO rewrite system, using url_key to build url.
     * @TODO desired url: /badges/my-test-badges-badge-title
     *
     * @return string
     */
    public function getUrl()
    {
        return $this->_urlBuilder->getUrl('badges/' . $this->getUrlKey());
    }

    /**
     * Get title
     *
     * @return string|null
     */
    public function getTitle()
    {
        return $this->getData(self::TITLE);
    }

    /**
     * Get content
     *
     * @return string|null
     */
    public function getContent()
    {
        return $this->getData(self::CONTENT);
    }

    /**
     * Get image
     *
     * @return string|null
     */
    public function getImage()
    {
        return $this->getData(self::IMAGE);
    }

    /**
     * Get info
     *
     * @return string|null
     */
    public function getInfo()
    {
        return $this->getData(self::INFO);
    }

    /**
     * Get creation time
     *
     * @return string|null
     */
    public function getCreationTime()
    {
        return $this->getData(self::CREATION_TIME);
    }

    /**
     * Get update time
     *
     * @return string|null
     */
    public function getUpdateTime()
    {
        return $this->getData(self::UPDATE_TIME);
    }

    /**
     * Is active
     *
     * @return bool|null
     */
    public function isActive()
    {
        return (bool) $this->getData(self::IS_ACTIVE);
    }

    /**
     * Set ID
     *
     * @param int $id
     *
     * @return BadgeInterface
     */
    public function setId($id)
    {
        return $this->setData(self::BADGE_ID, $id);
    }

    /**
     * Set URL Key
     *
     * @param string $url_key
     *
     * @return BadgeInterface
     */
    public function setUrlKey($url_key)
    {
        return $this->setData(self::URL_KEY, $url_key);
    }

    /**
     * Set title
     *
     * @param string $title
     *
     * @return BadgeInterface
     */
    public function setTitle($title)
    {
        return $this->setData(self::TITLE, $title);
    }

    /**
     * Set content
     *
     * @param string $content
     *
     * @return BadgeInterface
     */
    public function setContent($content)
    {
        return $this->setData(self::CONTENT, $content);
    }

    /**
     * Set image
     *
     * @param string $image
     *
     * @return BadgeInterface
     */
    public function setImage($image)
    {
        return $this->setData(self::IMAGE, $image);
    }

    /**
     * Set info
     *
     * @param string $info
     *
     * @return BadgeInterface
     */
    public function setInfo($info)
    {
        return $this->setData(self::INFO, $info);
    }

    /**
     * Set creation time
     *
     * @param string $creation_time
     *
     * @return BadgeInterface
     */
    public function setCreationTime($creation_time)
    {
        return $this->setData(self::CREATION_TIME, $creation_time);
    }

    /**
     * Set update time
     *
     * @param string $update_time
     *
     * @return BadgeInterface
     */
    public function setUpdateTime($update_time)
    {
        return $this->setData(self::UPDATE_TIME, $update_time);
    }

    /**
     * Set is active
     *
     * @param int|bool $is_active
     *
     * @return BadgeInterface
     */
    public function setIsActive($is_active)
    {
        return $this->setData(self::IS_ACTIVE, $is_active);
    }
}
