<?php

namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;

class Engine {
  public $user = 0;
  public $version = '0.1';
  public $err = '';
  public $warn = '';
  public $info = '';
  public $suc = '';
  public $css = '';

  public function __construct () {
    $this->err = '';
    $this->warn = '';
    $this->info = '';
    $this->suc = '';

    if (isset($_COOKIE['language'])) app()->setLocale($_COOKIE['language']);
    $this->id = checkLegit((isset($_COOKIE['kero_token']) ? $_COOKIE['kero_token'] : ''));
    $this->user = $this->getLoggedUser((isset($_COOKIE['kero_token']) ? $_COOKIE['kero_token'] : ''));

    $this->css = env('CSSENGINE');
    if ($this->user && $this->user->profile->gender != 0) $this->css .= '_'.($this->user->profile->gender == 1 ? 'm' : 'f');
    if (env('CSSMINIFY')) $this->css .= '.min';
    $this->css .= '.css';
  }

  function getLoggedUser ($kero) {
    $check = checkLegit($kero);
    if ($check == 0) return 0;

    // $cols = getGroupColours();
    $get = $this->getUser($check);

    return $get;
  }

  public function getUser ($id) {
    $get = DB::table('users')->where('id', $id)->first();
    unset($get->password); unset($get->salt); unset($get->remember_token);
    $get->profile = DB::table('usr_profile')->where('user_id', $get->id)->first();
    $get->profile->showname = (!empty($get->profile->display_name) && !is_null($get->profile->display_name) ? $get->profile->display_name : $get->username);
    $perm_id = DB::table('usr_perm_id')->select('perm_id')->where('user_id', $get->id)->first()->perm_id;
    $get->perm_module = DB::table('usr_perm_module')->where('usr_perm_id', $perm_id)->first();
    $get->details = DB::table('usr_details')->where('user_id', $get->id)->first();
    $get->contacts = DB::table('usr_contacts')->where('user_id', $get->id)->first();
    $ucol = DB::table('usr_perm_module')->select('id', 'name', 'colour_m', 'colour_f', 'colour_u')->get();

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

    if ($get->profile->gender == 1) $get->profile->gender_name = '男性';
    else if ($get->profile->gender == 2) $get->profile->gender_name = '女性';
    else $get->profile->gender_name = '不明';

    $get->perm = $this->getPermissions($get->kero_token);
    if (empty($get->profile->avatar) || $get->profile->avatar == '') $get->profile->avatar = 'img/noicon.jpg';
    $get->profile->avatar = '/'.$get->profile->avatar;

    return $get;
  }

  public function getPermissionGroup($mdl, $id) { return DB::table($mdl.'_permissions')->where('id', $id)->get(); }
  public function getPermissionUser($mdl, $id) { return DB::table($mdl.'_user_permissions')->where('user_id', $id)->get(); }

  public function getPermissions ($token) {
    $check = checkLegit($token);
    $perm = DB::table('usr_perm_id')->select('perm_id')->where('user_id', $check)->first();
    $perm = json_decode(json_encode($perm), true);

    if ($check != 0) {
      $grouppermblg = $this->getPermissionGroup('blg', $perm['perm_id']);
      $userpermblg = $this->getPermissionUser('blg', $check);

      $grouppermusr = $this->getPermissionGroup('usr', $perm['perm_id']);
      $userpermusr = $this->getPermissionUser('usr', $check);

      $blgarr = array();
      $usrarr = array();

      if (!empty($userpermblg[0])) $blgarr = (array)$userpermblg[0];
      else $blgarr = (array)$grouppermblg[0];
      $blgarr = array_combine(array_map(function($k){ return 'blg_'.$k; }, array_keys($blgarr)), $blgarr);

      if (!empty($userpermusr[0])) $usrarr = (array)$userpermusr[0];
      else $usrarr = (array)$grouppermusr[0];
      $usrarr = array_combine(array_map(function($k){ return 'usr_'.$k; }, array_keys($usrarr)), $usrarr);

      return array_merge($blgarr, $usrarr);
    }
    else {
      $grouppermblg = $this->getPermissionGroup('blg', 6);
      $grouppermusr = $this->getPermissionGroup('usr', 6);

      (array)$grouppermblg[0] = array_combine(array_map(function($k){ return 'blg_'.$k; }, array_keys((array)$grouppermblg[0])), (array)$grouppermblg[0]);
      (array)$grouppermusr[0] = array_combine(array_map(function($k){ return 'usr_'.$k; }, array_keys((array)$grouppermusr[0])), (array)$grouppermusr[0]);

      return array_merge((array)$grouppermblg[0], (array)$grouppermusr[0]);
    }
  }
}
