'use strict';
/**
 * @ngdoc controller
 * @name ChangeEmailController
 * @description
 *  email 변경 화면
 */
app.controller('ChangeEmailController', function ($scope, $state, $window, toastr, User, Auth) {
  $scope.save = function () {

    var password = $scope.formData.password;
    var newEmail = $scope.formData.newEmail;
    var jsonData = JSON.stringify( { password : password, email : newEmail });
    
    // 사용자 email 변경
    User.changeEmail(jsonData).then( function() { 
      var user = JSON.parse($window.localStorage.getItem('hcm_editor'));
      angular.extend(user, {'email': newEmail});
      Auth.setCurrentUser(user);
      toastr.success('Successfully changed email!');
      $state.transitionTo('account-settings.main', null, { reload: true, inherit: true, notify: true  });
    }).
    catch(function (error) {
      toastr.error(error);
    });
  };
});

/**
 * @ngdoc controller
 * @name ChangePasswordController
 * @description
 *  password 변경 화면
 */
app.controller('ChangePasswordController', function ($scope, $state, toastr, User) {
  $scope.save = function () {

    var oldPassData = $scope.formData.currentPassword;
    var newPassData = $scope.formData.newPassword;
    var jsonData = JSON.stringify( { old_password : oldPassData, new_password : newPassData });

    // 사용자 password 변경
    User.changePassword(jsonData).then(function (data) {
      toastr.success('Successfully changed password!');
      $state.transitionTo('account-settings.main', null, { reload: true, inherit: true, notify: true  });     
    }).
    catch(function (error) {
      toastr.error(error);
    });
  };
});


