<?php

class Login extends Page {

    function setMenu()
    {
        if($_SESSION['user'])
        {
            $this->menu_text = $_SESSION['userid'];
        }
        else
            $this->menu_text = 'Login';
        $this->menu_image = 'far fa-user';
        $this->menu_priority = 99;
    }

    function setSubmenu()
    {
        if($_SESSION['user'])
        {
            $this->addSubmenuItem('Profil', '/profile', 'fas fa-user');
            $this->addSubmenuItem('Settings', '/settings', 'fas fa-cog');
            $this->addSubmenuItem('Logout', '/login/logout', 'fas fa-sign-out-alt', 'bg-red-500');
        }
    }

    

    function index()
    {
        
        $this->set('template', 'login.html');
        //return print_r($_REQUEST, true);
    }

    function test()
    {
        return nl2br(print_r([
            'uuid' => gen_ulid(),
            'timestamp' => ulid_to_timestamp("01HD9XN98F8SGT01X527KBNHRN"),
            'freshtime' => ulid_to_timestamp(gen_ulid())
        ],true));
    }

    function logout()
    {
        //delete cookie
        setcookie('token', '', time() - 3600, "/");
        session_destroy();
        $this->redirect('/');
    }

    function validate()
    {
        $email = trim($_REQUEST['email']);
        $password = trim($_REQUEST['password']);
        $remember = $_REQUEST['remember'];

        $error = false;

        $u = new User();

        if(!$email || !$password)
            $error = 'Bitte gib deine E-Mail-Adresse und dein Passwort ein';
        else if(!filter_var($email, FILTER_VALIDATE_EMAIL))
            $error = 'Bitte gib eine gültige E-Mail-Adresse ein';
        else if(!$u->exists($email))
            $error = 'Benutzer nicht gefunden. Schon registriert?';
        else {
            
            try{
                $u->load($email);
            }
            catch(Exception $e){
                $error = $e->getMessage();
            }
            if(!password_verify($password, $u->data['password']))
                $error = 'E-Mail-Adresse oder Passwort falsch';
            else if($u->data['active'] == 0)
                $error = 'Dein Account ist noch nicht aktiviert';
            else
            {
                //if $remmeber is true, create and set cookie so the user will be automatically logged in next time
                if($remember)
                {
                    //check if user has a valid cookie
                    if(isset($_COOKIE['token']) && $u->token == $_COOKIE['token'])
                    {
                        $token =$u->token;
                        setcookie('token', $token, time() + (86400 * 30), "/");
                    }
                    else
                    {
                        //if no, create a new token
                        $token = uuid4();
                        $u->token = $token;
                        $u->save();
                        setcookie('token', $token, time() + (86400 * 30), "/");
                    }
                }
                    
                $u->login();
                if($_SERVER['HTTP_HX_CURRENT_URL'] && !endsWith($_SERVER['HTTP_HX_CURRENT_URL'],'/login'))
                    $this->redirect($_SERVER['HTTP_HX_CURRENT_URL']);
                else
                    $this->redirect('/');
            }
        }

        if($error)
        {
            $this->set('template', '/templates/partials/error.html');
            $this->set('errorTitle', 'Error');
            $this->set('errorMessage', $error);
        }
        

        //return print_r(['email'=>$email,'password'=>$password,'remember'=>$remember], true);
    }

}