/**
 * UserController
 *
 * @description :: Server-side logic for managing Users
 * @help        :: See http://links.sailsjs.org/docs/controllers
 * @todo        :: See if req.session.user actually needs set
 */

//var log = require('captains-log')();

module.exports = {

    findemail: function(req, res) {

        UserLoginService.findEmail(req.param('email'), function(err, foundEmail) {

            if (err) return err;

            return res.send(foundEmail);

        });
    },

    login: function(req, res) {

        UserLoginService.loginUser(req.param('email'), req.param('password'), function(err, login) {

            if (err) return err;

            if (login.verified)
            {
                req.session.authenticated = true;
                req.session.user = login.user;
                return res.send({ "verified": login.verified, "message": login.message}, login.status);
            }
            else
            {
                req.session.authenticated = false;
                if (req.session.user) req.session.user = null;
                return res.send({ "verified": login.verified, "message": login.message}, login.status);
            }

        });

    },

    logout: function(req, res) {

        //log user out
        req.session.authenticated = false;
        if (req.session.user) req.session.user = null;

        res.send("logged out", 200);
    },

    logged_in: function(req, res) {

        //check if user is logged in
        if (req.session.authenticated == true)
        {
            res.send(true, 200);
        }
        else
        {
            res.send(false, 200);
        }

    },

    add_address: function(req, res) {

        User.find();

    }

    /*
    create_admin_user: function(req, res) {

        User.create({

            email: "admin@sails.com",
            password: "123456",
            role: 1

        })
        .exec(function(err, admin) {

            if(err) return err;

            res.send(admin, 200);

        });

    }
    */

};
