<?php

session_start();

class RegisterUser
{

    public function showRegistrationForm()
    {
        $view    = 'form';
        $nanoTpl = new nanoTpl($view);
        $output  = $nanoTpl->output();

        return Response::respondData($output);
    }

    public function registerUser()
    {

        if(!isset($_POST)) {
            return Response::respondError(4);
        }

        if(  !$_POST['data']['first_name'] ||
            !$_POST['data']['last_name']  ||
            !$_POST['data']['email']      ||
            !$_POST['data']['city']       ||
            !$_POST['data']['address']
        ) {
            return Response::respondError(3);
        }

        $args = $_POST['data'];

        $id = (int) $_SESSION['userId'];

        $user = User::find($id);
        $user->first_name     = $args['first_name'];
        $user->last_name      = $args['last_name'];
        $user->last_name      = $args['last_name'];
        $user->email          = $args['email'];
        $user->city           = $args['city'];
        $user->phone          = isset($args['phone']) ? $args['phone'] : '';
        $user->phone          = $args['phone'];
        $user->address        = $args['address'];
        $user->arrival_status = 1;

        $user->save();

        $view    = 'chooseTransport';
        $nanoTpl = new nanoTpl($view);
        $output  = $nanoTpl->output();

        return Response::respondData($output);

    }

    public function updateTransport()
    {
        // Check if user has answered the question.
        if(!isset($_POST['data']['transport'])) {
            return Response::respondError(4);
        }

        $transport = $_POST['data']['transport'];

        // Save users answer
        User::updateTransport($transport, $_SESSION['userId']);

        $view    = 'changeAvatar';
        $nanoTpl = new nanoTpl($view);

        $sopstveniPrevoz = '<p class = "transport-text">Prijem svih gostiju se očekuje od 19:00 do 20:00 časova.</p>
                            <p class = "transport-text">Personalizovana karta će Vam biti dostavljena na adresu najkasnije do 22.07.2014.</p>
                            <p class = "transport-text">Dobrodošli na Digital Night.</p>';

        $bus             = '<p class = "transport-text">Organizovan je besplatan autobuski prevoz do Club-a RESET.</p>
                            <p class = "transport-text">Autobusi kreću u 18:30 ispred Sava centra.</p>
                            <p class = "transport-text">Dobrodošli na Digital Night.</p>';

        $nanoTpl->set('transport'       ,$transport == 1 ? $sopstveniPrevoz : $bus);

        $output  = $nanoTpl->output();

        session_destroy();
        return Response::respondData($output);
    }

    public function checkEmail()
    {
        $email = $_POST['email'];
        if(User::checkEmail($email)) {
            return 'false';
        }

        return 'true';
    }
}
