<?php


namespace USER;

class USER{

    private $user_id;
    private $username;
    private $firstname;
    private $lastname;
    private $email;
    private $sesion;
    private $db;
    private $permission = array();

    public function __construct($db) {
        session_start();
        $this->db = $db;
        //print_r($_SESSION);die();
        if (isset($_SESSION['userId'])) {
            $user_query = $db->query("SELECT * FROM " . DBPREFIX . "user WHERE userId = '" . (int)$_SESSION['userId'] . "' AND status = '1'");

            if ($user_query->num_rows) {
                $this->user_id = $user_query->row['userId'];
                $this->username = $user_query->row['email'];
                $this->firstname = $user_query->row['name'];
                $this->lastname = '';//$user_query->row['lastName'];
                $this->email = $user_query->row['email'];
/*
                $this->db->query("UPDATE " . DBPREFIX . "user SET ip = '" . $this->db->escape($this->request->server['REMOTE_ADDR']) . "' WHERE user_id = '" . (int)$this->session->data['user_id'] . "'");

                $user_group_query = $this->db->query("SELECT permission FROM " . DBPREFIX . "user_group WHERE user_group_id = '" . (int)$user_query->row['user_group_id'] . "'");

                $permissions = json_decode($user_group_query->row['permission'], true);

                if (is_array($permissions)) {
                    foreach ($permissions as $key => $value) {
                        $this->permission[$key] = $value;
                    }
                }
*/
            } else {
                $this->logout();
            }
        }
    }

    public function login($username, $password) {
        $user_query = $this->db->query("SELECT * FROM " . DBPREFIX . "user WHERE email = '" . $this->db->escape($username) . "' AND password = '" . $this->db->escape(md5($password)) . "' AND status = '1'");
//print_r($user_query);die();
        if ($user_query->num_rows) {
            $_SESSION['userId'] = $user_query->row['userId'];
            //print_r($_SESSION);die();
            $this->user_id = $user_query->row['userId'];
            $this->username = $user_query->row['email'];

            //print_r( $this->user_id);die();
           /* $this->user_group_id = $user_query->row['user_group_id'];

            $user_group_query = $this->db->query("SELECT permission FROM " . DBPREFIX . "user_group WHERE user_group_id = '" . (int)$user_query->row['user_group_id'] . "'");

            $permissions = json_decode($user_group_query->row['permission'], true);

            if (is_array($permissions)) {
                foreach ($permissions as $key => $value) {
                    $this->permission[$key] = $value;
                }
            }
            */
            return true;
        } else {
            return false;
        }
    }

    public function logout() {
        unset( $_SESSION['userId']);

        $this->user_id = '';
        $this->username = '';
    }

    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 getEmail() {
        return $this->email;
    }

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

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

    public function getFullName() {
        return $this->firstname.' '.$this->lastname;
    }

    public function getGroupId() {
        return $this->user_group_id;
    }

    public function __destruct()
    {

        // TODO: Implement __destruct() method.
    }

}