﻿"use strict";

// login controller
app.controller("forgotPasswordController", ["$rootScope", "$scope", "$location", "authService", "judgeRulesService",
    function ($rootScope, $scope, $location, authService, judgeRulesService) {
    $scope.randomNumber = Math.random();
    $scope.emailAddress = "";

    $scope.sendEmail = function () {
        if ($rootScope.requireResetForgotPassword) {
            $scope.sendResetPasswordEmail();
        }
        else {
            $scope.sendForgotPasswordEmail();
        }
    };
    $scope.sendResetPasswordEmail = function () {
        if ($scope.validateEmail($scope.emailAddress)) {
            authService.sendResetPasswordEmail($scope.emailAddress).then(function (response) {
                if (response && response.data) {
                    $.awards.alert({
                        title: "Message",
                        message: "A reset password email has been sent to <b>{0}</b>".format($scope.emailAddress),
                        buttonCssClass: "btn btn-info",
                        headerCssClass: "bg-info",
                        okText: "OK"
                    });
                    $scope.emailAddress = "";
                    $location.path("/");
                }
                else {
                    $.awards.alert({
                        title: "Error",
                        message: "Failed to send reset password email. Please try again or contact support",
                        okText: "OK"
                    });
                }
            },
            function (err) {
                $.awards.alert({
                    title: "Error",
                    message: "Failed to send reset password email. Please try again or contact support",
                    okText: "OK"
                });
            });
        }
        else {
            $.awards.alert({
                title: "Error",
                message: "You have entered invalid email address",
                okText: "OK"
            });
        }
    };
    $scope.sendForgotPasswordEmail = function () {
        if ($scope.validateEmail($scope.emailAddress)) {
            authService.sendForgotPasswordEmail($scope.emailAddress).then(function (response) {
                if (response.data) {
                    $.awards.alert({
                        title: "Message",
                        message: "An email has been sent to <b>{0}</b>".format($scope.emailAddress),
                        buttonCssClass: "btn btn-info",
                        headerCssClass: "bg-info",
                        okText: "OK"
                    });
                    $scope.emailAddress = "";
                    $location.path("/");
                }
                else {
                    $.awards.alert({
                        title: "Error",
                        message: "Failed to send forgot password email. Please try again or contact support",
                        okText: "OK"
                    });
                }
            },
            function (err) {
                $.awards.alert({
                    title: "Error",
                    message: "Failed to send forgot password email. Please try again or contact support",
                    okText: "OK"
                });
            });
        }
        else {
            $.awards.alert({
                title: "Error",
                message: "You have entered invalid email address",
                okText: "OK"
            });
        }
    };


    function _showLoginError() {

    }

    $scope.validateEmail = function (email) {
        var re = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
        return re.test(email);
    }

    $scope.clearQueryString = function () {
        $location.search("ts", null);
        $location.search("cid", null);
    };

    $scope.init = function () {
        $scope.clearQueryString();

        if (typeof judgeRulesService.hideForgotPassword !== "undefined") {
            if (judgeRulesService.hideForgotPassword) {
                // do not allow access to this view
                // send them to error page

                $location.path("/error");
            }
        }
    };

    $scope.init();
}]);