<?php

namespace App\Http\Controllers\Peertube\Myaccount;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use App\Http\Controllers\Peertube\Common;
// use Illuminate\Support\Facades\Log;

class Notification extends Common {
  private $common;
  private $count;

  public function __construct () {
    $this->common = new Common;
    $this->count = 20;
  }

  public function index ($page=0) {
    if (!isset($this->common->user['me']) || is_null($this->common->user['me'])) {
      return redirect('/peertube/login');
    }

    $res = [
      'page' => 'notification',
      'style' => 'myaccount',
      'paginate' => $page,
      'pagetotal' => 500,
      'userinfo' => $this->common->user,
    ];

    $res['notification'] = $this->getNotification(($page*$this->count), $this->count);
    return view('pages.peertube.my-account.notifications', ['res' => $res]);
  }

  function getNotification ($start, $count) {
    return $this->ptapi('/api/v1/users/me/notifications?start='.$start.'&count='.$count.'&sort=-createdAt');
  }

  public function read (Request $r) {
    $this->ptapi('/api/v1/users/me/notifications/read', json_encode(['ids' => [(int)$r->id]]), 'post');
    return redirect('/peertube/my-account/notifications');
  }

  public function readAll () {
    $this->ptapi('/api/v1/users/me/notifications/read-all', '', 'post');
    return redirect('/peertube/my-account/notifications');
  }
}
