<?php

/*
 * PuzzleApp
 * Webemyos
 * Jérôme Oliva
 * GNU Licence
 */

namespace Core\Security;

use Core\Core\Core;
use Core\Entity\User\User;

use Apps\Base\Helper\EmailHelper;
use Apps\Base\Helper\WorkFlowHelper;

/**
 * Description of Authentication
 *
 * @author OLIVA
 */
class Authentication 
{
    /*
     * Connecte the user
     */
    public static function Connect($login, $password, $verifyPassWord = true)
    {
        $core = Core::getInstance();
        
        $user = new User($core);
        $user = $user->GetByEmail($login);
        
        if($user !== false)
        {
            if(!$verifyPassWord || ($verifyPassWord && $user->PassWord->Value == md5($password) ))
            {
                //Connect the User
                $core->Connect($user);
                
                return true;
            }
            else
            {
                return $core->GetCode("Base.LoginOrPassWordInCorrect");
            }
        }
        else
        {
            return $core->GetCode("Base.UnkwonUser");
        }
    }
    
    /*
     * Create User
     */
    public static function CreateUser($core, $email, $pass, $verify)
    {
        if($pass != $verify)
        {
            return $core->GetCode("Base.PassNotEqual");
        }
        
        $User = new User($core);
        $User->Email->Value = $email;
        
        if($User->Exist($email))
        {
            return $core->GetCode("Base.UserExist");
        }
        
        $User->PassWord->Value = $pass;
        $User->GroupeId->Value = 2;
        $User->Save();
        
        
         //Send Email              
        EmailHelper::SendEmail($core,
                "Cms-Register",
                $email,
                [],
                $core->GetCode("Base.LinkWelcomeTitle"),
                $core->GetCode("Base.LinkWelcomeMessage"),
        );

        // Execute action of new User If Exist           
        WorkFlowHelper::Run($core,
                "Cms-NewUser",
                array("New User" => $email,
                    "{userEmail}" => $email
        ));
        
        
        return true;
    }

    /**
     * Met à jour le password User
     */
    public static function UpdatePassword($core, $user, $password, $verify)
    {
        if($password == $verify)
        {
            $request = "Update ee_user set PassWord = '" .md5($password)."' where Id=" . $user->IdEntite ;
            $core->Db->Execute($request);

            return "Ok";
        }
        else
        {
            return $core->GetCode("PassNotEqual");
        }   
    }
}
