<?php
/**
 * Created by PhpStorm.
 * User: Joseg
 * Date: 30/4/2018
 * Time: 9:30 PM
 */

include '../comun/conexionLogin.php';

class validator
{
    public function Validar($cor,$clave){
        $ValidarUsuario=R::find('usuario', 'correo =:correo AND clave =:clave', array('correo' => $cor, 'clave' => $clave));
        return ($ValidarUsuario? true : false);
    }

    public function HasToken($id){
        $HasToken  = R::findOne( 'sessiontoken', 'idusuario = ?', [$id]);
        return ($HasToken? true : false);
    }

    public function seeToken($id){
        $seeToken  = R::findOne( 'sessiontoken', 'idusuario = ?', [$id] );
        return ($seeToken? $seeToken->token : false);
    }

    public function generateToken($id){
        include "../uniqueNameGenerator/generateRamdonToken.php";
        $Tokeng = new generateRamdonToken();
        $sessiontoken = R::dispense('sessiontoken');
        $sessiontoken->idusuario = $id;
        $sessiontoken->ip = $_SERVER['REMOTE_ADDR'];
        $sessiontoken->token = $Tokeng->id();
        $id = R::store($sessiontoken);
        return $sessiontoken->token;
    }

    public function retornarId($correo){
        $seeToken  = R::findOne( 'usuario', 'correo = ?', [$correo] );
        return $seeToken->id;
    }
    public function retornarIdToken($token){
        $seeToken  = R::findOne( 'sessiontoken', 'token = ?', [$token] );
        return $seeToken->idusuario;
    }

    public function checkToken($tok){
        $checkToken  = R::findOne( 'sessiontoken', 'token = ?', [$tok] );
        return ($checkToken? true : false);
    }
    public function deleteToken($tok){
        $checkToken  = R::findOne( 'sessiontoken', ' token = ? ', [$tok] );
        R::trash( $checkToken );
        return true;
    }
    public function retornarDatos($id){
        $seeToken  = R::load('usuario', $id);
        $usuario = array (
            "nombre"  => $seeToken->nombre,
            "correo" => $seeToken->correo,
            "tipo_usuario"   => $seeToken->tipo_usuario,
            "condominio_id"   => $seeToken->condominio_id,
            "tipo_usuario"   => $seeToken->tipo_usuario,

        );
        return $usuario;
    }
    public function validarPermiso($id,$permiso){
        $usuario  = R::load('usuario', $id);
        return ($usuario->tipo_usuario==$permiso? true : false);
    }
    public function time_elapsed_string($datetime, $full = false) {
        $now = new DateTime;
        $ago = new DateTime($datetime);
        $diff = $now->diff($ago);

        $diff->w = floor($diff->d / 7);
        $diff->d -= $diff->w * 7;

        $string = array(
            'y' => 'año',
            'm' => 'mes',
            'w' => 'semana',
            'd' => 'día',
            'h' => 'hora',
            'i' => 'minuto',
            's' => 'segundo',
        );
        foreach ($string as $k => &$v) {
            if ($diff->$k) {
                $v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? $v==='mes' ? 'es': 's' : '');
            } else {
                unset($string[$k]);
            }
        }

        if (!$full) $string = array_slice($string, 0, 1);
        return $string ? 'Hace '. implode(', ', $string)   : 'Justo ahora';
    }
}