'use strict';
angular.module('resetPassword', [])
    .controller('ResetPassword', ['$scope', '$state', 'ngToast', '$timeout', 'configServer',
        '$stateParams', 'Util', 'AppUtil', 'session',
        function ($scope, $state, ngToast, $timeout, configServer, $stateParams, Util, AppUtil, session) {
            $scope.doBack = function () {
                $state.go("login");
            };

            $scope.getPasswordPolicySetting = function () {
                var params = {
                    auth_token: $stateParams.token,
                };

                Util.createRequest(configServer.API_URL + "/team/settings/password_policy", "GET", params, function (response) {
                    if (response.status === 200) {
                        $scope.passwordPolicies = JSON.parse(response.data.password_setting);
                        $scope.password_pattern = $scope.passwordPolicies.regex;
                        $scope.notificationError = AppUtil.getPasswordPolicyError($scope.passwordPolicies);
                    } else {
                        Util.createToast({
                            className: 'danger',
                            content: 'SERVER_ERROR.' + response.status
                        });
                        $state.go("login");
                    }
                }, function (status) {
                    Util.createToast({
                        className: 'danger',
                        content: 'SERVER_ERROR.' + status
                    });
                    $state.go("login");

                });
            };
            $scope.getPasswordPolicySetting();

            // Do submit
            $scope.submit = false;
            $scope.resetPass = function (valid) {
                if (!valid)
                    return;

                // Prevent submit when no change
                $scope.submit = true;
                var params = {
                  //   new_password: AESEncrypter.encode($scope.newPass),
                  //   confirm_password: AESEncrypter.encode($scope.confirmPass),
                  new_password: Util.MD5($scope.newPass),
                  confirm_password: Util.MD5($scope.confirmPass),
                  auth_token: $stateParams.token,
                };
                Util.createRequest(configServer.API_URL + "/auth/user/password", "PUT", params, function (response) {
                    if (response.status === 200) {
                        var emailLogin = $stateParams.hash;
                        var keepMe = $stateParams.keep;
                        var teamId = $stateParams.team;
                        if (emailLogin && teamId) {

                            var paramsLogin = {
                              team_id: teamId,
                              email: emailLogin,
                            //   password_hash: AESEncrypter.encode(Util.MD5($scope.newPass)),
                              password_hash: Util.MD5($scope.newPass),
                              keep_login: keepMe,
                            };

                            session.userLogin(paramsLogin, function (data, path) {
                                $state.go("dashboard");
                            }, function (error) {
                                Util.createToast({
                                    className: 'danger',
                                    content: 'SERVER_ERROR.' + error.status
                                });
                            });
                        } else {
                            $state.go("login");
                        }

                        Util.createToast({
                            className: 'success',
                            content: 'COMMON.MESSAGE.SAVE_SUCCESS'
                        });
                    } else {
                        Util.createToast({
                            className: 'danger',
                            content: 'SERVER_ERROR.' + response.status
                        });
                        if (response.status === 102) {
                            $state.go("login");
                        }
                        $scope.submit = false;
                    }
                }, function (error) {
                    Util.createToast({
                        className: 'danger',
                        content: 'SERVER_ERROR.' + error.status
                    });
                    if (error.status === 102) {
                        $state.go("login");
                    }
                    $scope.submit = false;
                });
            };

        }
    ]);
