'use strict';

(function () {

angular
  .module('bitcoincom.controllers')
  .controller('buyBitcoinKycPersonalInfoController',
  buyBitcoinKycPersonalInfoController);

  function buyBitcoinKycPersonalInfoController(
    bitAnalyticsService
    , gettextCatalog
    , $ionicHistory
    , $log
    , kycFlowService
    , moonPayService
    , ongoingProcess
    , popupService
    , moment
    , $scope
  ) {
    var currentState = {};
    var vm = this;

    // Functions
    vm.goBack = goBack;
    vm.goNext = goNext;

    $scope.$on("$ionicView.beforeEnter", onBeforeEnter);
    $scope.$on("$ionicView.beforeLeave", onBeforeLeave);

    function _validateAllFields() {
      return _validateAge() 
      && vm.firstName
      && vm.lastName
      && vm.streetAddress1
      && vm.city
      && vm.postalCode
    }

    function _validateAge() {
      if (vm.dob) {
        var dob = moment(vm.dob, 'DD/MM/YYYY');
        if (moment().diff(dob, 'years') >= 18) {
          return true
        }
      }
      console.log('Age is: ', moment().diff(dob, 'years'));
      return false;
    }

    function _initVariables() {

      vm.submitted = false;

      currentState = kycFlowService.getCurrentStateClone();
      console.log('buyBitcoinKycPersonalInfoController onBeforeEnter after back kycflow', currentState);

      vm.firstName = currentState.firstName ? currentState.firstName : 'Satoshi';
      vm.lastName = currentState.lastName ? currentState.lastName : 'Nakamoto';
      vm.dob = currentState.dob ? currentState.dob : '10/12/1990';
      vm.streetAddress1 = currentState.streetAddress1 ? currentState.streetAddress1 : '123 Maple street';
      vm.streetAddress2 = currentState.streetAddress2 ? currentState.streetAddress2 : '';
      vm.city = currentState.city ? currentState.city : 'Tokyo'; 
      vm.postalCode = currentState.postalCode ? currentState.postalCode : '1234';
      vm.country = currentState.country ? currentState.country : '';

      vm.countries = [];
      vm.countriesAreLoading = true;

      // Fetch Countries and Documents
      console.log('Fetching Countries!');
      moonPayService.getAllCountries().then(
        function onGetAllCountriesSuccess(countries) {
          vm.countries = countries;
          vm.countriesAreLoading = false;
          console.log('Fetching Countries - SUCCESS!');
          console.log(countries);
        },
        function onGetAllCountriesError(err) {
          console.log('Failed to get countries.', err);
          vm.countriesAreLoading = false;
        }
      );
    }

    function goNext() {
      vm.submitted = true;
      if (!_validateAllFields()) {
        $log.debug('Form incomplete.');
        return;
      }
      // Save current state
      currentState.firstName = vm.firstName
      currentState.lastName = vm.lastName
      currentState.dob = vm.dob
      currentState.streetAddress1 = vm.streetAddress1
      currentState.streetAddress2 = vm.streetAddress2
      currentState.city = vm.city
      currentState.postalCode = vm.postalCode
      currentState.country = vm.country;

      kycFlowService.goNext(currentState);
    }

    function goBack() {
      kycFlowService.goBack();
    }

    function onBeforeEnter(event, data) {
      if (data.direction == "back") {
        kycFlowService.popState();
      }
      
      _initVariables();
      bitAnalyticsService.postEvent('buy_bitcoin_personal_info_screen_open' ,[], ['leanplum']);
    }

    function onBeforeLeave(event, data) {
      bitAnalyticsService.postEvent('buy_bitcoin_personal_info_screen_close' ,[], ['leanplum']);
    }
  }
})();

@@ -244,128 +244,47 @@
   , 
-gettextCatalog%0A    , $ionicHistory%0A    , $log%0A    , kycFlowService%0A    , moonPayService%0A    , ongoingProcess%0A    , popup
+$log%0A    , kycFlowService%0A    , moonPay
 Serv
@@ -2685,24 +2685,25 @@
 vm.firstName
+;
 %0A      curre
@@ -2728,24 +2728,25 @@
  vm.lastName
+;
 %0A      curre
@@ -2765,16 +2765,17 @@
 = vm.dob
+;
 %0A      c
@@ -2816,24 +2816,25 @@
 reetAddress1
+;
 %0A      curre
@@ -2875,16 +2875,17 @@
 Address2
+;
 %0A      c
@@ -2906,24 +2906,25 @@
 ty = vm.city
+;
 %0A      curre
@@ -2953,24 +2953,25 @@
 m.postalCode
+;
 %0A      curre

Fix syntax