﻿"use strict";

// change password controller
app.controller("changePasswordController", ["$rootScope", "$scope", "$location", "authService", "judgeRulesService", "categoryDataService", function ($rootScope, $scope, $location, authService, judgeRulesService, categoryDataService) {
    $scope.validNewPassword = false;
    $scope.validRepeatNewPassword = false;
    $scope.categories = [];

    $scope.revalidatePassword = function () {
        if ($scope.newPassword) {
            var match = $scope.newPassword.match(/^(?=.*[A-Z])(?=.*[!@#$&*])(?=.*[a-z]).{8,30}$/);
            $scope.validNewPassword = match !== null && match.length;
            $scope.validRepeatNewPassword = $scope.newPassword === $scope.repeatNewPassword;

            $scope.setPasswordStrength();
        }
    };

    $scope.$watch("newPassword", $scope.revalidatePassword);
    $scope.$watch("repeatNewPassword", $scope.revalidatePassword);

    $scope.setPasswordStrength = function () {
        $scope.passwordStrength = "";
        $scope.passwordStrengthClass = "";
        if ($scope.validNewPassword) {
            var strongRegex = new RegExp("^(?=.{10,})(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*\\W).*$", "g");
            var mediumRegex = new RegExp("^(?=.{8,})(((?=.*[A-Z])(?=.*[a-z]))|((?=.*[A-Z])(?=.*[0-9]))|((?=.*[a-z])(?=.*[0-9]))).*$", "g");

            if (strongRegex.test($scope.newPassword)) {
                $scope.passwordStrength = "[ Strong Password ]";
                $scope.passwordStrengthClass = "text-success";
            }
            else if (mediumRegex.test($scope.newPassword)) {
                $scope.passwordStrength = "[ Medium Password ]";
                $scope.passwordStrengthClass = "text-danger";
            }
            else {
                $scope.passwordStrength = "";
                $scope.passwordStrengthClass = "";
            }
        }
    };

    $scope.changePassword = function () {
        if ($.awards.is.nullOrEmpty($scope.oldPassword)) {
            $scope.alertMessage = "You must enter old password";
        }
        else {
            var result = authService.changePassword(authService.authentication.userName, $scope.newPassword, $scope.oldPassword);
            result.then(function (response) {
                if (response.data === "true") {
                    $scope.oldPassword = "";
                    $scope.newPassword = "";
                    $scope.repeatNewPassword = "";
                    $scope.alertMessage = "";
                    $scope.setPasswordStrength();

                    $.awards.alert({
                        message: "Your Password is successfully changed.",
                        buttonCssClass: "btn btn-info",
                        headerCssClass: "bg-info",
                        okText: "OK"
                    });
                }
                else {
                    $.awards.alert({
                        message: "Failed to update password. Please check your old password and try again. Else, please contact support.",
                        okText: "OK"
                    });
                }
            },
            function () {
                $.awards.alert({
                    message: "Failed to update password. Please check your old password and try again. Else, please contact support.",
                    okText: "OK"
                });
            });
        }
    };

    $scope.getCategories = function () {
        categoryDataService.getCategories().then(function (results) {
            $scope.categories = results.data;
        },
        function (error) {
            console.error('error on getCategories');
        });
    };

    $scope.init = function () {
        if (judgeRulesService.hideChangePassword) {
            $location.path("/error");
        }
    };

    $scope.init();
}]);