'use strict';
angular.module('forgotPassword', [])
    .controller('ForgotPasswordCtrl', ['$scope', '$state', 'ngToast', '$timeout', 'configServer', 'Patterns', 'Util',
        'serviceKeyType',
        function ($scope, $state, ngToast, $timeout, configServer, Patterns, Util, serviceKeyType) {
            $scope.userInfor = {
                username: ""
            };
            $scope.selectedTeam = {
                id: ''
            };
            $scope.email_pattern = Patterns.EMAIL_PATTERN;
            $scope.forgotStep1 = true;
            $scope.messageForgotPassword = false;
            $scope.multipleTeam = false;
            $scope.submitting = false;


            $scope.checkEmailTeam = function (valid) {
                if (!valid || $scope.userInfor.username === "") {
                    return;
                }
                $scope.submitting = true;
                var params = {
                //   email: AESEncrypter.encode($scope.userInfor.username),
                  email: $scope.userInfor.username,
                  service_key: serviceKeyType.SECURE_MAIL,
                };
                Util.createRequest(configServer.API_URL + "/auth/user/teams", "GET", params, function (response) {
                    var status = response.status;
                    if (status === 200) {
                        $scope.forgotStep1 = false;
                        if (response.data.length > 1) {
                            $scope.teams = response.data;
                            $scope.multipleTeam = true;
                        } else {
                            $scope.selectedTeam.id = response.data[0].team_id;
                            $scope.doForgotPassword();
                            $scope.messageForgotPassword = true;
                        }
                    } else {
                        Util.createToast({className: 'danger', content: 'SERVER_ERROR.' + status});
                    }
                }, function (error) {
                    Util.createToast({
                        className: 'danger',
                        content: 'SERVER_ERROR.' + error.status
                    });
                });
                $scope.submitting = false;
            };

            $scope.doForgotPassword = function () {

                if ($scope.selectedTeam.id === '') {
                    Util.createToast({className: 'danger', content: 'LOGIN.ERROR_CHOOSE_TEAM'});
                    return;
                }
                var params = {
                    team_id: $scope.selectedTeam.id,
                    email: $scope.userInfor.username,
                    outlook: true
                };
                Util.createRequest(configServer.API_URL + "/auth/user/password", "POST", params, function (response) {
                    if (response.status === 200) {
                        $scope.multipleTeam = false;
                        $scope.messageForgotPassword = true;
                    } else {
                        Util.createToast({
                            className: 'danger',
                            content: 'SERVER_ERROR.' + response.status
                        });
                    }

                }, function (error) {
                    Util.createToast({
                        className: 'danger',
                        content: 'SERVER_ERROR.' + error.status
                    });
                });

            };

        }
    ]);
