<?php

class User
{
  private $user_id;
  private $username;
  private $usergroup;
  private $mlnoticies;
  private $permission = array();
  private $firstname;
  private $lastname;
  private $email;
  private $checkinfo;
  private $theme;
  private $db;
  private $registry;
  private $request;
  private $session;

  public function __construct($registry)
  {
    $this->db = $registry->get('db');
    $this->registry = $registry;
    $this->request = $registry->get('request');
    $this->session = $registry->get('session');

    $jwt = new JWT($this->registry, 'admin');

    if ($jwt->getCustomerLoged("sub")) {
      $user_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "user WHERE user_id = '" . (int)$jwt->getCustomerLoged("sub") . "' AND status = '1'");

      if ($user_query->num_rows) {
        $this->user_id = $user_query->row['user_id'];
        $this->username = $user_query->row['username'];
        $this->usergroup = $user_query->row['user_group_id'];
        $this->firstname = $user_query->row['firstname'];
        $this->lastname = $user_query->row['lastname'];
        $this->email = $user_query->row['email'];
        $this->checkinfo = $user_query->row['checkinfo'];
        $this->theme = $user_query->row['theme'];

        $this->db->query("UPDATE " . DB_PREFIX . "user SET ip = '" . $this->db->escape($this->request->server['REMOTE_ADDR']) . "' WHERE user_id = '" . (int)$jwt->getCustomerLoged("sub") . "'");

        $user_group_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "user_group_permission WHERE user_group_id = '" . (int)$user_query->row['user_group_id'] . "'");
        $permissions = $user_group_query->rows;

        if (is_array($permissions)) {
          $this->permission['access'] = array_column(array_filter($permissions, function ($value) {return $value['access'];}), 'permission');
          $this->permission['modify'] = array_column(array_filter($permissions, function ($value) {return $value['modify'];}), 'permission');
        }
      } else {
        $this->logout();
      }
    }
  }

  public function login($username, $password)
  {
    $user_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "user WHERE username = '" . $this->db->escape($username) . "' AND (password = SHA1(CONCAT(salt, SHA1(CONCAT(salt, SHA1('" . $this->db->escape($password) . "'))))) OR password = '" . $this->db->escape(md5($password)) . "') AND status = '1'");

    if($user_query->num_rows){
        $jwt = new JWT($this->registry,'admin');
        $jwt->sessionJwt($user_query->row['user_id'],$this->request->server['HTTP_HOST'],$this->request->server['REMOTE_ADDR'],md5(mt_rand()));
    }

    if ($user_query->num_rows) {
        // $this->session->data['user_id'] = $user_query->row['user_id'];

      $this->user_id = $user_query->row['user_id'];
      $this->username = $user_query->row['username'];
      $this->usergroup = $user_query->row['user_group_id'];
      $this->firstname = $user_query->row['firstname'];
      $this->lastname = $user_query->row['lastname'];
      $this->email = $user_query->row['email'];
      $this->checkinfo = $user_query->row['checkinfo'];
      $this->theme = $user_query->row['theme'];

      $user_group_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "user_group_permission WHERE user_group_id = '" . (int)$user_query->row['user_group_id'] . "'");
      $permissions = $user_group_query->rows;

      if (is_array($permissions)) {
        $this->permission['access'] = array_column(array_filter($permissions, function ($value) {return $value['access'];}), 'permission');
        $this->permission['modify'] = array_column(array_filter($permissions, function ($value) {return $value['modify'];}), 'permission');
      }

      $this->session->data['admin_login_trys'] = 0;
      return true;
    } else {
      isset($this->session->data['admin_login_trys']) ? $this->session->data['admin_login_trys']++ : $this->session->data['admin_login_trys'] = 1;
      return false;
    }
  }

  public function logout($destroy = true)
  {
    unset($this->session->data['token']);
    unset($this->session->data['token_jws']);

    $this->user_id = '';
    $this->username = '';
    $this->usergroup = '';
    $this->mlnoticies = '';
    $this->firstname = '';
    $this->lastname = '';
    $this->email = '';
    $this->checkinfo = '';
    $this->theme = '';

    if ($destroy) {
      session_destroy();
    }

  }

  public function hasPermission($key, $value)
  {
    if (isset($this->permission[$key])) {
      return in_array($value, $this->permission[$key]);
    } else {
      return false;
    }
  }

  public function isLogged()
  {
    return $this->user_id;
  }

  public function getId()
  {
    return $this->user_id;
  }

  public function getUserName()
  {
    return $this->username;
  }

  public function getUserGroup()
  {
    return $this->usergroup;
  }

  public function getMlNoticies()
  {
    return $this->mlnoticies;
  }

  public function getFirstname()
  {
    return $this->firstname;
  }

  public function getLastname()
  {
    return $this->lastname;
  }

  public function getEmail()
  {
    return $this->email;
  }

  public function getCheckInfo()
  {
    return $this->checkinfo;
  }

  public function getTheme()
  {
    return $this->theme;
  }

}
