<?php

namespace Litecms\Representative;

use User;

class Representative
{
    /**
     * $representatives object.
     */
    protected $representatives;

    /**
     * Constructor.
     */
    public function __construct(\Litecms\Representative\Interfaces\RepresentativesRepositoryInterface $representatives)
    {
        $this->representatives = $representatives;
    }

    /**
     * Returns count of representative.
     *
     * @param array $filter
     *
     * @return int
     */
    public function count()
    {
        return  0;
    }

    /**
     * Make gadget View
     *
     * @param string $view
     *
     * @param int $count
     *
     * @return View
     */
    public function gadget($view = 'admin.representatives.gadget', $count = 10)
    {

        if (User::hasRole('user')) {
            $this->representatives->pushCriteria(new \Litepie\Litecms\Repositories\Criteria\RepresentativesUserCriteria());
        }

        $representatives = $this->representatives->scopeQuery(function ($query) use ($count) {
            return $query->orderBy('id', 'DESC')->take($count);
        })->all();

        return view('representative::' . $view, compact('representatives'))->render();
    }
}