'use strict';

angular.module('user-change-password', [])

    .controller('UserChangePassword', ['$scope', 'AppUtil', 'APIName', '$state', 'APIStatus', 'util', 'session',
        'userConfig', 'app', 'ConsoleType',
        function ($scope, AppUtil, APIName, $state, APIStatus, util, session, userConfig, app, ConsoleType) {

            $scope.getPasswordPolicySetting = function () {
                var params = {
                    team_id: session.getUser().team_id,
                    type: 3,
                };
                var objUrlPasswordPolicy = {
                    method: APIName.GET_TEAM_SETTING_BY_TYPE.method,
                    url: APIName.GET_TEAM_SETTING_BY_TYPE.name
                };
                AppUtil.createRequest(objUrlPasswordPolicy, params, function (data) {
                    var passwordPolicy = JSON.parse(data.data.password_setting);
                    $scope.password_pattern = passwordPolicy.regex;
                    $scope.pw_max_length = passwordPolicy.max;
                    $scope.password_error = AppUtil.getPasswordPolicyError(passwordPolicy);
                }, function (error) {
                    AppUtil.showError({text: util.translate("sever_errors." + error.status)});
                    $window.history.back();
                });
            };

            if (session.getUser().role_id !== userConfig.SUPER_ADMIN.role
                && session.getUser().role_id !== userConfig.COMPANY_ADMIN.role) {
                $scope.getPasswordPolicySetting();
            } else {
                $scope.password_pattern = app.PASSWORD_PATTERN_DEFAULT;
                $scope.pw_max_length = 64;
                $scope.password_error = util.translate("errors.password_policy.default", {
                    min: 6,
                    max: 64
                });
            }


            // User info
            $scope.userData = {};
            $scope.currentUserId = session.getUser().user_id;
            // Do submit
            $scope.submit = false;
            $scope.userChangePassword = function (valid) {

                if (!valid)
                    return;

                // Prevent submit when no change
                $scope.submit = true;
                var params = {
                    old_password: AESEncrypter.encode($scope.current_password),
                    new_password: AESEncrypter.encode($scope.new_password),
                    confirm_password: AESEncrypter.encode($scope.confirm_password),
                    service_key: ConsoleType.Reseller.type,
                };

                // call Create Project ajax
                AppUtil.createRequest({
                    url: APIName.API_USER.name + $scope.currentUserId + APIName.UPDATE_PASSWORD.name,
                    method: APIName.UPDATE_PASSWORD.method
                }, params, function (data) {

                    if (data.status === 200) {
                        AppUtil.createToast({
                            text: util.translate("default_toast.save_success"),
                            type: 'success'
                        });
                    }
                    $scope.submit = false;
                    $state.go("userProfile")
                }, function (error) {
                    AppUtil.showError({
                        text: util.translate("sever_errors." + error.status),
                        type: 'error'
                    });
                    $scope.submit = false;
                });
            };
            $('#link_password').on('keypress', function (e) {
                if (e.which === 32) return false;
            });
        }]);
