<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\AuthController;
use App\Http\Controllers\SiteController;
use App\Http\Controllers\UserController;

use Illuminate\Support\Facades\Log;

class HomeController extends Controller {
  private $objAuth;
  private $objSite;
  private $objUser;

  private $menu;
  private $cook;
  private $id;
  private $user;

  public function __construct() {
    $this->objAuth = new AuthController();
    $this->objSite = new SiteController();
    $this->objUser = new UserController();
    $this->menu = $this->objSite->getPagesInMenu();
    $this->cook = (isset($_COOKIE['kero_token']) ? $_COOKIE['kero_token'] : '');
    $this->id = $this->objAuth->checkLegit($this->cook);
    $this->user = $this->objUser->getLoggedUser($this->id, $this->cook);
  }

  public function index () {
    $res = $this->objSite->getPosts(1);
    return view('pages.site.index', ['res' => $res, 'menu' => $this->menu, 'user' => $this->user]);
  }

  public function post ($slug) {
    $res = $this->objSite->getPost($slug, $this->cook);
    if (!$res) return view('pages.site.notfound', ['res' => $res, 'menu' => $this->menu, 'user' => $this->user]);
    return view('pages.site.post', ['res' => $res, 'menu' => $this->menu, 'user' => $this->user]);
  }

  public function page ($slug) {
    $res = $this->objSite->getPage($slug, $this->cook);
    if (!$res) return view('pages.site.notfound', ['res' => $res, 'menu' => $this->menu, 'user' => $this->user]);
    return view('pages.site.page', ['res' => $res, 'menu' => $this->menu, 'user' => $this->user]);
  }

  public function profile ($id) {
    $res = $this->objUser->getUser($id, $this->cook);
    return view('pages.site.profile', ['res' => $res, 'menu' => $this->menu, 'user' => $this->user]);
  }

  public function addContent (Request $r) {
    if ($this->user && ($this->user['blg_addpost'] || $this->user['blg_addpage'])) {
      $bdl = array();
      $res = $this->objSite->addContent($bdl);

      if ($res) {
        return redirect('/blog/'.$r->slug);
      }

      $err = $res['err'];
      return view('pages.site.contentadd', ['res' => $res, 'menu' => $this->menu, 'user' => $this->user, 'err' => $err]);
    }

    return redirect('');
  }

  public function login (Request $r) {
    if (isset($_COOKIE['kero_token'])) return redirect('');
    $res = array();
    $err = '';

    if (isset($r->username) && isset($r->password)) {
      $res = $this->objAuth->login($r);
      if (isset($res['kero_token'])) return redirect('');
      $err = $res['err'];
    }

    return view('pages.site.login', ['res' => $res, 'menu' => $this->menu, 'user' => $this->user, 'err' => $err]);
  }

  public function register (Request $r) {
    if (isset($_COOKIE['kero_token'])) return redirect('');
    $res = $this->objUser->getCountries();
    $err = '';

    if (isset($r->username) && isset($r->password) && isset($r->email) && isset($r->password_check)) {
      $reg = $this->objAuth->register($r);
      if (isset($reg['kero_token'])) return redirect('');
      $err = $reg['err'];
    }

    return view('pages.site.register', ['res' => $res, 'menu' => $this->menu, 'user' => $this->user, 'err' => $err]);
  }

  public function notfound () {
    return view('pages.site.notfound', ['menu' => $this->menu, 'user', $this->user]);
  }
}
