<?php

namespace App\Http\Controllers\Peertube;

use App\Http\Controllers\Controller, Session;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use App\Http\Controllers\Engine;
use Illuminate\Support\Facades\Log;

class Common extends Engine {
  // use AuthTrait;

  public $user = [];
  private $engine;
  private $access_token;
  private $refresh_token;

  public function __construct () {
    $this->access_token = isset($_COOKIE['access_token']) ? $_COOKIE['access_token'] : null;
    $this->refresh_token = isset($_COOKIE['refresh_token']) ? $_COOKIE['refresh_token'] : null;

    if (!Session::has('ptsess')) {
      $user = [];
      $user['local'] = $this->getLocal();
      $user['me'] = $this->getMe($user['local']);
      $user['config'] = $this->getConfig();
      $user['notify'] = $this->getNotify();
      Session::put('ptsess', $user);
      $this->user = $user;
    }
    else {
      $this->user = Session::get('ptsess');
    }

    $this->engine = new Engine;
  }

  public function getLocal () {
    return $this->ptapi('/api/v1/oauth-clients/local');
  }

  public function getMe ($param) {
    if (!isset($_COOKIE['access_token']) && isset($_COOKIE['refresh_token'])) {
      $r = new Request;
      $login = new \App\Http\Controllers\Peertube\Login;
      $r->client_id = $param->client_id;
      $r->client_secret = $param->client_secret;
      $r->refresh_token = $this->refresh_token;
      $this->login($r);
      $this->access_token = $_COOKIE['access_token'];
      $this->refresh_token = $_COOKIE['refresh_token'];
    }

    if (isset($_COOKIE['access_token']) && isset($_COOKIE['refresh_token'])) return $this->ptapi('/api/v1/users/me');
    return null;
  }

  public function getConfig () {
    return $this->ptapi('/api/v1/config/');
  }

  public function getNotify () {
    if (isset($_COOKIE['access_token']) && isset($_COOKIE['refresh_token'])) return $this->ptapi('/api/v1/users/me/notifications?start=0&count=0&unread=true');
    return null;
  }

  public function ptapi ($url, $param='', $method='get', $contenttype='json') {
    set_time_limit(0);
    $this->access_token = isset($_COOKIE['access_token']) ? $_COOKIE['access_token'] : null;
    $this->refresh_token = isset($_COOKIE['refresh_token']) ? $_COOKIE['refresh_token'] : null;
    $header = ['Content-Type: application/'.$contenttype, 'Host: '.str_replace('https://', '', env('PEER_URI'))];

    if (!is_null($this->access_token)) {
      $header[] = 'Authorization: Bearer '.$this->access_token;
    }

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, env('PEER_URI').$url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    if ($method == 'post') {
      curl_setopt($ch, CURLOPT_POST, 1);
      if ($param != '') curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
    }
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    $get = curl_exec($ch);
    $err = curl_error($ch);
    curl_close($ch);

    if (!$get) return $err;
    $get = json_decode($get);
    if (isset($get->status) && $get->status == 404) return [];

    return $get;
  }

  public function vidlist ($get) {
    $res = [
      'today' => [],
      'week' => [],
      'month' => [],
      'lastmonth' => [],
      'moreearly' => [],
    ];

    foreach ($get->data as $g) {
      $ud = strtotime($g->createdAt);

      if ($ud > time() - 86400 && $ud < time() + 86400) $res['today'][] = $g;
      else if ($ud > time() - 604800 && $ud < time() + 604800) $res['week'][] = $g;
      else if ($ud > time() - 2629800 && $ud < time() + 2629800) $res['month'][] = $g;
      else if ($ud > time() - 5259600 && $ud < time() + 5259600) $res['lastmonth'][] = $g;
      else $res['moreearly'][] = $g;
    }

    return $res;
  }
}
