<?php

use Illuminate\Support\Facades\DB;

function getPagesInMenu () {
  $get = DB::table('blg_content')->select('title', 'slug')->where('public_status', 0)->where('isPost', 0)->where('isMenu', 1)->orderBy('sortorder', 'asc')->get();

  $res = array();
  $key = 0;

  foreach ($get as $i) {
    array_push($res, ['key' => $key, 'title' => $i->title, 'slug' => $i->slug]);
    $key++;
  }

  return $res;
}

function getCss () {
  $engine = new \App\Http\Controllers\Engine;
  return $engine->css;
}

function getPerms () {
  $engine = new \App\Http\Controllers\Engine;
  return $engine->getPermissions((isset($_COOKIE['kero_token']) ? $_COOKIE['kero_token'] : ''));
}

function getUser () {
  $engine = new \App\Http\Controllers\Engine;
  return $engine->user;
}

function getGroupColours () {
  return DB::table('usr_perm_module')->select('id', 'colour_m', 'colour_f', 'colour_u')->get()->toArray();
}

function checkLegit ($t) {
  if (!isset($t) || empty($t) || is_null($t)) return 0;
  $check = DB::table('users')->select('id')->where('kero_token', $t)->first();
  return $check->id;
}

function ptComeSrc ($v) {
  $res = new \stdClass();
  $res->bg = '#';
  $res->fg = '#fcfcfc';
  $res->tx = '不明';

  switch ($v) {
    case 'PT':
      $res->bg .= 'ffad5c';
      $res->tx = 'PeerTube';
      break;
    case 'TS':
      $res->bg .= 'cb81ea';
      $res->tx = 'テク諏訪';
      break;
    case 'YT':
      $res->bg .= 'f50057';
      $res->tx = 'YouTube';
      break;
    case 'OD':
      $res->bg .= '33b58f';
      $res->tx = 'LBRY';
      break;
    default:
      $res->bg = 'transparent';
      break;
  }

  return $res;
}

function ptGetCatId ($i) {
  $res = '';
  $i = (int)$i;

  switch ($i) {
    case 1:  $res = "音楽"; break;
    case 2:  $res = "映画"; break;
    case 3:  $res = "乗り物"; break;
    case 4:  $res = "芸術"; break;
    case 5:  $res = "スポーツ"; break;
    case 6:  $res = "旅行"; break;
    case 7:  $res = "ゲーム"; break;
    case 8:  $res = "人物"; break;
    case 9:  $res = "コメディ"; break;
    case 10: $res = "エンターテインメント"; break;
    case 11: $res = "ニュースや政治"; break;
    case 12: $res = "ハウツー"; break;
    case 13: $res = "教育"; break;
    case 14: $res = "アクティビズム"; break;
    case 15: $res = "科学やテクノロジー"; break;
    case 16: $res = "動物"; break;
    case 17: $res = "子ども向け"; break;
    case 18: $res = "食べ物"; break;
    default: break;
  }

  return $res;
}

function ptFullHandle ($v) {
  return $v->name.($v->host != str_replace('https://', '', env('PEER_URI')) ? '@'.$v->host : '');
}

function ptDuration ($v) {
  $seconds = $v;
  $hours = floor($seconds / 3600);
  $seconds -= $hours * 3600;
  $minutes = sprintf("%02d", floor($seconds / 60));
  $seconds -= sprintf("%02d", $minutes * 60);

  return $hours != 0 ? $hours.':' : ''.$minutes != 0 ? $minutes.':' : '0:'.$seconds;
}

function ptAvatar ($v) {
  $placeholder = '/img/noicon.jpg';
  $res = '';

  if (isset($v->avatar)) {
    if (is_null($v->avatar)) return $placeholder;
    $ava = $v->avatar->path;
    $ava = str_replace(env('APP_URL'), '', $ava);
    $res = str_starts_with(env('APP_URL'), $ava) ? env('PEER_URI') : '';
    if ($ava == '/') return '/img/noicon.jpg';
    else if (str_starts_with($ava, '/lazy-static')) $res = env('PEER_URI');

    return $res.$ava;
  }

  return '/img/noicon.jpg';
}

function userDetail ($id, $kero=null) {
  if ($kero || $id) {
    $log_username = null;
    if (!is_null($id)) $log_username = DB::table('users')->select('id', 'username')->where('id', $id)->first();
    else $log_username = DB::table('users')->select('id', 'username')->where('kero_token', $kero)->first();
    $perm_id = DB::table('usr_perm_id')->select('perm_id')->where('user_id', $log_username->id)->first()->perm_id;
    $p3 = DB::table('usr_profile')->select('gender', 'avatar', 'name_style', 'display_name')->where('user_id', $log_username->id)->first();
    $user_id = $log_username->id;
    $log_username = $log_username->username;
    $showname = '';
    $showcol = '';
    $ucol = DB::table('usr_perm_module')->select('id', 'colour_m', 'colour_f', 'colour_u')->get();

    if (!empty($p3->display_name)) $showname = $p3->display_name;
    else $showname = $log_username;

    if (!empty($p3->name_style)) $showcol = $p3->name_style;
    else {
      foreach ($ucol as $j3) {
        if ($j3->id == $perm_id) {
          if ($p3->gender == 1) $showcol = $j3->colour_m;
          else if ($p3->gender == 2) $showcol = $j3->colour_f;
          else $showcol = $j3->colour_u;
        }
      }
    }

    $p3->avatar = ($p3->avatar ? '/'.$p3->avatar : '/img/noicon.jpg');
    return array('user_id' => $user_id, 'showname' => $showname, 'showcol' => $showcol, 'avatar' => $p3->avatar);
  }

  return new \stdClass();
}

function getCountries () {
  $flags = DB::table('nhn_country')->orderBy('id', 'asc')->get();
  $res = array();

  foreach ($flags as $flag) {
    $protocol = isset($_SERVER["HTTPS"]) ? 'https' : 'http';

    $res[] = array(
      'value' => $flag->nameEng,
      'label' => $flag->name
    );
  }

  return $res;
}

function makeToken () {
  $c  = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$!%&()';
  $clen = strlen($c);
  $token = '';

  for ($i = 0; $i < 128; $i++) {
    $token .= $c[rand(0, $clen - 1)];
  }

  return $token;
}

?>
