<?php

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

class Account extends Common {
  private $common;

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

  public function index ($id, $cat='video-channels') {
    $res = [
      'page' => 'account',
      'cat' => $cat,
      'userinfo' => $this->common->user,
    ];
    $res['owner'] = $this->getOwner($id);
    if (!empty($res['owner'])) $res['owner']->totalVideo = 0;
    $res['channel'] = $this->getChannel($id);
    if (!empty($res['owner'])) {
      foreach ($res['channel']->data as $k => $v) {
        $res['channel']->data[$k]->video = $this->getVideo($v->name.'@'.$v->host);
        $res['owner']->totalVideo += $res['channel']->data[$k]->video->total;
      }
    }
    return view('pages.peertube.a', ['res' => $res]);
  }

  function getOwner ($id) {
    return $this->ptapi_get('/api/v1/accounts/'.$id);
  }

  function getChannel ($id) {
    return $this->ptapi_get('/api/v1/accounts/'.$id.'/video-channels?start=0&count=20&sort=-updatedAt&withStats=false');
  }

  function getVideo ($id) {
    return $this->ptapi_get('/api/v1/video-channels/'.$id.'/videos?start=0&count=5&sort=-publishedAt&nsfw=both');
  }
}
