<?php
/**
 * Description of Login
 *
 * @author carlos
 */
class Login extends LoginRepository {
    private $user = array();
    private $isLogged = null;
    
    public function __construct($options = null) {        
        if(null === $options){
            if(isset($_SESSION['usuario'])){
                $this->user = $_SESSION['usuario'];
                $this->isLogged = true;
            }
        }else{
            $this->isLoginCorrect($options['usuario'], $options['contrasena']);
        }
    } 
       
    public function isLoginCorrect($user, $password) {
        $data = parent::isLoginCorrect($user, $password);

        if($data){
            $data['controller'] = null;
            $this->setUser($data);
            $this->isLogged = true;
        }
         return null;
    }    
    
    public function setUser($data){
        $this->user = $data;
        $this->setVersionSystem();
        $this->setDefaultController();
        $_SESSION['usuario'] = $this->user;
        
        $translator = new Translator();
        $translator->setLenguage($this->getIdioma());
    }
    
    private function setVersionSystem(){
        $settings = new SettingsRepository();
        $this->user['versionSystem'] = $settings->_get('version_system');
    }
    
    public function getVersionSystem(){
        return $this->user['versionSystem'];
    }
    
    private function setDefaultController(){
        switch($this->getRole()){
            default:
                    $this->user['defaultController'] = 'Home.php';
                break;
        }
    }
    
    public function getDefaultController(){
        return $this->user['defaultController'];
    }
    
    
    public function getId(){
        return $this->user['id'];
    }
    
    public function getUusario(){
        return $this->user['usuario'];
    }
    
    public function getNombreCompleto(){
        return $this->user['nombre']." ".$this->user['apellido'];
    }
    
    public function getRole(){
        return $this->user['role'];
    }    
        
    public function getCustomer(){
        return $this->user['id_customer'];
    }
    
    public function isLogged(){
        return $this->isLogged;
    }
    
    public function setCurrentController($controller){
        $_SESSION['usuario']['controller'] = $controller;
    }
    
    public function getCurrentController(){
        return  $this->user['controller'];
    }
    
    public function getIdioma(){
        return $_SESSION['usuario']['language'];
    }
    
    public function getDefaultLocationForOutsourcing(){
        return  $this->user['default_location_for_outsourcing'];
    }
}