<?php namespace {{namespace}};

use jorenvanhocht\Blogify\Models\Category;
use jorenvanhocht\Blogify\Models\Post;
use jorenvanhocht\Blogify\Models\Tag;
use Carbon\Carbon;
use Illuminate\Database\DatabaseManager;

class BlogController extends Controller
{
    /**
     * @var Post
     */
    protected $post;

    /**
     * @var Category
     */
    protected $category;

    /**
     * @var Tag
     */
    protected $tag;

    /**
     * @var DatabaseManager
     */
    protected $db;

    /**
     * @var object
     */
    protected $config;

    /**
     * @param Post $post
     * @param Category $category
     * @param Tag $tag
     * @param DatabaseManager $db
     */
    public function __construct(Post $post, Category $category, Tag $tag, DatabaseManager $db)
    {
        $this->post = $post;
        $this->category = $category;
        $this->tag = $tag;
        $this->db = $db;
        $this->config = objectify(config('blogify'));

        $this->middleware('jorenvanhocht\Blogify\Middleware\ProtectedPost', ['only' => 'show']);
    }

    ///////////////////////////////////////////////////////////////////////////
    // View methods
    ///////////////////////////////////////////////////////////////////////////

    /**
     * @return \Illuminate\View\View
     */
    public function index()
    {
        $data = $this->getOverviewData();
        $data['posts'] = $this->post->forPublic()->orderBy('publish_date', 'DESC')->paginate($this->config->items_per_page);

        return view('blogify.index', $data);
    }

    /**
     * @param $slug
     * @return \Illuminate\View\View
     */
    public function show($slug)
    {
        $data = $this->getOverviewData();

        $data['post'] = $this->post->bySlug($slug);
        return view('blogify.show', $data);
    }

    /**
     * @param $year
     * @param $month
     * @return \Illuminate\View\View
     */
    public function archive($year, $month)
    {
        $data = $this->getOverviewData();
        $data['posts'] = $this->post
                                ->forPublic()
                                ->where($this->db->raw('MONTHNAME(publish_date)'), '=', $month)
                                ->where($this->db->raw('YEAR(publish_date)'), '=', $year)
                                ->orderBy('publish_date', 'DESC')
                                ->paginate($this->config->items_per_page);

        return view('blogify.index', $data);
    }

    /**
     * @param $category
     * @return \Illuminate\View\View
     */
    public function category($category)
    {
        $category_id = $this->category->whereName($category)->first()->id;
        $data = $this->getOverviewData();

        $data['posts'] = $this->post->whereCategoryId($category_id)->forPublic()->orderBy('publish_date', 'DESC')->paginate($this->config->items_per_page);

        return view('blogify.index', $data);
    }

    /**
     * @param $slug
     * @return \Illuminate\View\View
     */
    public function askPassword($slug)
    {
        $data = $this->getOverviewData();
        $data['slug'] = $slug;
        return view('blogify.password', $data);
    }

    ///////////////////////////////////////////////////////////////////////////
    // Helper methods
    ///////////////////////////////////////////////////////////////////////////

    /**
     * @return array
     */
    private function getOverviewData()
    {
        $archive = $this->post->where('publish_date', '<=', date('Y-m-d H:i:s'))->orderBy('publish_date', 'DESC')->get()->groupBy(function($query){
            return Carbon::parse($query->publish_date)->format('F Y');
        });

        $data = [
            'categories' => $this->category->all(),
            'tags'  => $this->tag->all(),
            'archives' => $archive,
        ];

        return $data;
    }

}