<?php

namespace App\Http\Controllers\User;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use App\Http\Controllers\Engine;
// use Illuminate\Support\Facades\Log;

class Register {
  public function __construct () {}

  public function index (Request $r) {
    if (isset($_COOKIE['kero_token'])) return redirect('');
    $err = '';
    $save = array('username' => '', 'email' => '', 'country' => '', 'gender' => 0);

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

    return view('pages.site.register', ['sav' => $save, 'err' => $err]);
  }

  public function register (Request $r) {
    if (!empty($r)) {
      $existUser = DB::table('users')->select('id')->where('username', $r->username)->count();
      if ($existUser != 0) return array('err' => 'ユーザ名は既に登録しています。');
      if (empty($r->username)) return array('err' => 'ユーザ名は空です。');
      if (preg_match("/^[a-zA-Z0-9]+$/", $r->username) == 0) return array('err' => '英文字ばかりご入力下さい。');
      if (empty($r->password)) return array('err' => 'パスワードは空です。');
      if (empty($r->password_check)) return array('err' => 'パスワード（確認）は空です。');
      if (strlen($r->password) < 8) return array('err' => 'パスワードは8文以上をご入力下さい。');
      if ($r->password != $r->password_check) return array('err' => 'パスワードとパスワード（確認）は違います。');
      $existEmail = DB::table('users')->select('id')->where('email', $r->email)->count();
      if ($existEmail != 0) return array('err' => 'メールアドレスは既に登録しています。');
      if (empty($r->email)) return array('err' => 'メールアドレスは空です。');
      if (!filter_var($r->email, FILTER_VALIDATE_EMAIL)) return array('err' => 'メールアドレスを正しくご入力下さい。');

      $salt = dechex(mt_rand(0, 2147483647)) . dechex(mt_rand(0, 2147483647));
      $passwd = hash('sha256', $r->password . $salt);

      for ($round = 0; $round < 65536; $round++) { $passwd = hash('sha256', $passwd . $salt); }

      $addUser = DB::table('users')->insertGetId(['username' => $r->username, 'email' => $r->email, 'password' => $passwd, 'salt' => $salt, 'remember_token' => '', 'kero_token' => makeToken()]);
      DB::table('usr_details')->insert(['user_id' => $addUser, 'total_posts' => 0, 'total_threads' => 0, 'reg_date' => time(), 'last_post_date' => 0, 'last_post_location' => 0, 'ontime' => 0, 'strikes' => 0]);
      DB::table('usr_contacts')->insert(['user_id' => $addUser, 'website_link' => '', 'website_name' => '', 'youtube_link' => '', 'youtube_name' => '', 'bitchute' => '', 'niconico' => '', 'pixiv' => '', 'discord' => '', 'mastodon' => '', 'twitter' => '', 'facebook' => '', 'instagram' => '']);
      DB::table('usr_profile')->insert(['user_id' => $addUser, 'gender' => ($r->gender ? $r->gender : 0), 'member_title' => '', 'website_address' => '', 'website_name' => '', 'location' => '', 'birthday' => 0, 'bio' => '', 'avatar' => '', 'ostatus' => 1, 'header' => '', 'footer' => '', 'post_style' => '', 'signature' => '', 'name_style' => '', 'display_name' => '', 'yt_channel' => '', 'country' => ($r->country ? $r->country : 'Japan'), 'date_format' => '', 'isClock24' => 1, 'isShowSeconds' => 1, 'isShowTimezone' => 1]);
      DB::table('usr_perm_id')->insert(['user_id' => $addUser, 'perm_id' => 4, 'usr_per_id' => 4, 'img_per_id' => 4, 'blg_per_id' => 4, 'for_per_id' => 4, 'sbx_per_id' => 4, 'str_per_id' => 4, 'doc_per_id' => 4, 'odb_per_id' => 4, 'inv_per_id' => 4]);

      $checkName = DB::table('users')->select('kero_token')->where('id', $addUser)->first();

      setcookie('kero_token', $checkName->kero_token, time()+157788000, '/', $_SERVER['HTTP_HOST'], 0, 1);
      $login = new Login;
      return $login->login($r);
    }

    return array();
  }
}
